文章 217
评论 6
浏览 143919
Array - 26. Remove Duplicates from Sorted Array

Array - 26. Remove Duplicates from Sorted Array

26、Remove Duplicates from Sorted Array 相似题型:83(list), 26,80 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't m....

关于leetcode刷题顺序和刷题思路的声明

关于leetcode刷题顺序和刷题思路的声明

1. 刷题顺序:我leetcode的刷题顺序是根据刷题3000题的Edward老师在Youtube上的视频推荐的400题基础来刷的。 该老师有自己的网站和全程课(因为YouTube上老师只开放了50题,后面的要付费),网址:https://cspiration.com 。如果有需要购买全程课的同学可以去加老师他的微信去买全程课。 2. 代码及思路:因为Edward老师的视频在YouTube上只开放了前50,我又没买课,所以后面的有些题目思路还参考学习了花花酱老师和花嫂的视频。还有就是参考国内的博客和leetcode的Discuss。 花花酱老师的YouTube:https://www.youtube.com/user/xxfflower Edward老师的YouTube:https://www.youtube.com/channel/UCTWuRL33U8xBPqk3LehXjFw

Array - 239. Sliding Window Maximum

Array - 239. Sliding Window Maximum

239. Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the _k_numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. Example: Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3 Output: [3,3,5,5,6,7] Explanation: Window position Max [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 ** 5** 1 3 -....

Array - 164. Maximum Gap

Array - 164. Maximum Gap

164. Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. Example 1: Input: [3,6,9,1] Output: 3 Explanation: The sorted form of the array is [1,3,6,9], either   (3,6) or (6,9) has the maximum difference 3. Example 2: Input: [10] Output: 0 Explanation: The array contains less than 2 elements, therefore return 0. Note: You may assume all elements in the array are non-....

Array - 128. Longest Consecutive Sequence

Array - 128. Longest Consecutive Sequence

128. Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. 思路: 题目提示必须跑在O(n),可以使用set或者map来做,就是online和offline,offline非常简单直接,就是利用set的O(1)操作来解。 代码: java: class Solution { // online public int longestConsecutive(int[] nums)....

Array - 334. Increasing Triplet Subsequence

Array - 334. Increasing Triplet Subsequence

334. Increasing Triplet Subsequence Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists _i, j, k _ such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false. **Note: **Your algorithm should run in O(n) time complexity and O(1) space complexity. Example 1: Input: [1,2,....

Array - 123. Best Time to Buy and Sell Stock III

Array - 123. Best Time to Buy and Sell Stock III

123. Best Time to Buy and Sell Stock III Say you have an array for which the _i_th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. **Note: **You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), pr....

Array - 122. Best Time to Buy and Sell Stock II

Array - 122. Best Time to Buy and Sell Stock II

122. Best Time to Buy and Sell Stock II Say you have an array for which the _i_th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [7,1,5,3,6,4] Output: 7 Explanation: Buy on....

Array - 121. Best Time to Buy and Sell Stock

Array - 121. Best Time to Buy and Sell Stock

Say you have an array for which the _i_th element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.   Not 7-1 = 6, as selling price needs to be la....

Array - 220. Contains Duplicate III

Array - 220. Contains Duplicate III

220、 Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3, t = 0 Output: true 思路: 这道题是217和219的变形题,题意是说有一个整数数组,如果数组中有两个数的差在t内,并且这两个数的索引之差在k内,就返回true否则返回fals....

Array - 219. Contains Duplicate II

Array - 219. Contains Duplicate II

219、Contains Duplicate II Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. Example 1: Input: nums = [1,2,3,1], k = 3 Output: true Example 2: Input: nums = [1,0,1,1], k = 1 Output: true 思路: 这题是217题的变形,多加了在k范围内重复才返回true,思路也是使用map来做,遍历的同时查重,时间复杂度O(N),空间复杂度为O(N),使用滑动窗口的思想可以优化空间....

Array - 217. Contains Duplicate

Array - 217. Contains Duplicate

217、Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1: Input: [1,2,3,1] Output: true Example 2: Input: [1,2,3,4] Output: false 思路: 很简单的一题,只用找出数组中有没有重复的数字,用set或者map做就可以。 代码: java: class Solution { public boolean containsDuplicate(int[] nums) { return IntStream.of(nums).distinct().count() != nums.length; ....

Array - 299. Bulls and Cows

Array - 299. Bulls and Cows

299、Bulls and Cows You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to event....

Array - 189. Rotate Array

Array - 189. Rotate Array

189、Rotate Array Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] 思路: 翻转数组有很多种做法,这里就只说一种 time:O(n) space:O(1),思路就是经过三次翻转数组,达到只翻转到k的目的。 代码: java: class Solution { public void rotate(int[] nums, int k) { k= k % nums.length; reve....

Array - 277 Find the Celebrity

Array - 277 Find the Celebrity

277、Find the Celebrity Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them. Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: “Hi, A. Do you know B?” to get information of whether A knows B. You n....

Array - 80. Remove Duplicates from Sorted Array II

Array - 80. Remove Duplicates from Sorted Array II

80、Remove Duplicates from Sorted Array II 相似题型: 26 Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. ....

hello,solo blog!

hello,solo blog!

博客搬家了

Nothing just happens, it's all part of a plan.