diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 693684f3..99990302 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -197,7 +197,23 @@ var removeElement = (nums, val) => { }; ``` +TypeScript: + +```typescript +function removeElement(nums: number[], val: number): number { + let slowIndex: number = 0, fastIndex: number = 0; + while (fastIndex < nums.length) { + if (nums[fastIndex] !== val) { + nums[slowIndex++] = nums[fastIndex]; + } + fastIndex++; + } + return slowIndex; +}; +``` + Ruby: + ```ruby def remove_element(nums, val) i = 0 diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index ef7008bc..f0b56719 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -649,6 +649,41 @@ class Solution { } ``` +```Java +class Solution { + //前缀表(不减一)Java实现 + public int strStr(String haystack, String needle) { + if (needle.length() == 0) return 0; + int[] next = new int[needle.length()]; + getNext(next, needle); + + int j = 0; + for (int i = 0; i < haystack.length(); i++) { + while (j > 0 && needle.charAt(j) != haystack.charAt(i)) + j = next[j - 1]; + if (needle.charAt(j) == haystack.charAt(i)) + j++; + if (j == needle.length()) + return i - needle.length() + 1; + } + return -1; + + } + + private void getNext(int[] next, String s) { + int j = 0; + next[0] = 0; + for (int i = 1; i < s.length(); i++) { + while (j > 0 && s.charAt(j) != s.charAt(i)) + j = next[j - 1]; + if (s.charAt(j) == s.charAt(i)) + j++; + next[i] = j; + } + } +} +``` + Python3: ```python diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 8e944eb3..2bd7a287 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -422,13 +422,13 @@ class Solution: def backtrack(n,k,startIndex): if len(path) == k: res.append(path[:]) - return + return for i in range(startIndex,n - (k - len(path)) + 2): #优化的地方 path.append(i) #处理节点 backtrack(n,k,i+1) #递归 path.pop() #回溯,撤销处理的节点 - backtrack(n,k,1) - return res + backtrack(n,k,1) + return res ``` diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index bd941e03..e995fd18 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -182,13 +182,13 @@ class Solution: def backtrack(n,k,startIndex): if len(path) == k: res.append(path[:]) - return + return for i in range(startIndex,n-(k-len(path))+2): #优化的地方 path.append(i) #处理节点 backtrack(n,k,i+1) #递归 path.pop() #回溯,撤销处理的节点 - backtrack(n,k,1) - return res + backtrack(n,k,1) + return res ``` Go: ```Go diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 80822436..17422ca0 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -107,7 +107,7 @@ public: }; ``` -时间复杂度:$O(n)$ +时间复杂度:$O(n)$ 空间复杂度:$O(1)$ **一些录友会疑惑为什么时间复杂度是$O(n)$**。 @@ -121,7 +121,6 @@ public: - ## 其他语言版本 @@ -214,6 +213,28 @@ var minSubArrayLen = function(target, nums) { }; ``` +Typescript: + +```typescript +function minSubArrayLen(target: number, nums: number[]): number { + let left: number = 0, right: number = 0; + let res: number = nums.length + 1; + let sum: number = 0; + while (right < nums.length) { + sum += nums[right]; + if (sum >= target) { + // 不断移动左指针,直到不能再缩小为止 + while (sum - nums[left] >= target) { + sum -= nums[left++]; + } + res = Math.min(res, right - left + 1); + } + right++; + } + return res === nums.length + 1 ? 0 : res; +}; +``` + Swift: ```swift @@ -291,5 +312,23 @@ class Solution { } ``` +Ruby: + +```ruby +def min_sub_array_len(target, nums) + res = Float::INFINITY # 无穷大 + i, sum = 0, 0 + nums.length.times do |j| + sum += nums[j] + while sum >= target + res = [res, j - i + 1].min + sum -= nums[i] + i += 1 + end + end + res == Float::INFINITY ? 0 : res +end +``` + -----------------------
diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 26c630b9..0bb42192 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -323,7 +323,6 @@ class Solution: self.backtracking(k, n, i + 1) self.path.pop() self.sum_now -= i - return ``` ## Go diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 18c940d4..2dc1e874 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -93,7 +93,7 @@ dp[i][3] = dp[i - 1][2]; 综上分析,递推代码如下: ```CPP -dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i]; +dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i]); dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); dp[i][2] = dp[i - 1][0] + prices[i]; dp[i][3] = dp[i - 1][2]; diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index e6ad751b..a2825573 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -142,7 +142,7 @@ class Solution { Set> entries = map.entrySet(); // 根据map的value值正序排,相当于一个小顶堆 - PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue()); + PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); for (Map.Entry entry : entries) { queue.offer(entry); if (queue.size() > k) { diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 98e09bf8..51d04e92 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -154,6 +154,8 @@ public: Java: + +> 动态规划: ```java /** * 1.dp[i] 代表当前下标最大连续值 @@ -180,6 +182,25 @@ Java: } ``` +> 贪心法: + +```Java +public static int findLengthOfLCIS(int[] nums) { + if (nums.length == 0) return 0; + int res = 1; // 连续子序列最少也是1 + int count = 1; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] > nums[i]) { // 连续记录 + count++; + } else { // 不连续,count从头开始 + count = 1; + } + if (count > res) res = count; + } + return res; +} +``` + Python: > 动态规划: diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index d025e73c..d7489028 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -119,7 +119,7 @@ C++代码如下: class Solution { public: vector dailyTemperatures(vector& T) { - // 递减栈 + // 递增栈 stack st; vector result(T.size(), 0); st.push(0); @@ -150,7 +150,7 @@ public: class Solution { public: vector dailyTemperatures(vector& T) { - stack st; // 递减栈 + stack st; // 递增栈 vector result(T.size(), 0); for (int i = 0; i < T.size(); i++) { while (!st.empty() && T[i] > T[st.top()]) { // 注意栈不能为空 @@ -178,7 +178,7 @@ public: Java: ```java /** - * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑 + * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到小 *

* 入站元素要和当前栈内栈首元素进行比较 * 若大于栈首则 则与元素下标做差 diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index 0085de0b..b11fa7ef 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -221,6 +221,35 @@ var sortedSquares = function(nums) { }; ``` +Typescript: + +双指针法: + +```typescript +function sortedSquares(nums: number[]): number[] { + let left: number = 0, right: number = nums.length - 1; + let resArr: number[] = new Array(nums.length); + let resArrIndex: number = resArr.length - 1; + while (left <= right) { + if (Math.abs(nums[left]) < Math.abs(nums[right])) { + resArr[resArrIndex] = nums[right--] ** 2; + } else { + resArr[resArrIndex] = nums[left++] ** 2; + } + resArrIndex--; + } + return resArr; +}; +``` + +骚操作法(暴力思路): + +```typescript +function sortedSquares(nums: number[]): number[] { + return nums.map(i => i * i).sort((a, b) => a - b); +}; +``` + Swift: ```swift