From e894f44b607b0a3433cae8284a9e2c0c85f86645 Mon Sep 17 00:00:00 2001 From: Steve <841532108@qq.com> Date: Fri, 31 Dec 2021 19:23:47 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20typescript=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 From 2e16036f1abcd6a42b4f0dc66f402fd0ddf0cd78 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 31 Dec 2021 20:16:20 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880977.=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E6=B7=BB=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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 From 082302078bd1e4a354f21a4b8f77a9a6b389940b Mon Sep 17 00:00:00 2001 From: tlylt Date: Sat, 1 Jan 2022 11:17:07 +0800 Subject: [PATCH 03/11] fix Qn0077 python indent --- problems/0077.组合.md | 2 +- problems/0077.组合优化.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 4ec154e1..c00f1efb 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -422,7 +422,7 @@ 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) #递归 diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 5fe56e82..4eec1608 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -182,7 +182,7 @@ 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) #递归 From eb94c291b26f43470d874a405b78c914d3bdb9c3 Mon Sep 17 00:00:00 2001 From: tlylt Date: Sat, 1 Jan 2022 11:38:38 +0800 Subject: [PATCH 04/11] fix Qn0077 & Qn0216 python indent errors --- problems/0077.组合.md | 4 ++-- problems/0077.组合优化.md | 4 ++-- problems/0216.组合总和III.md | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index c00f1efb..c00ff551 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -427,8 +427,8 @@ class Solution: 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 4eec1608..24bfc086 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -187,8 +187,8 @@ class Solution: 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/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 From f4ea34989e8a114259a5a9ca5501d831101e8475 Mon Sep 17 00:00:00 2001 From: joeCarf <52153761+joeCarf@users.noreply.github.com> Date: Sat, 1 Jan 2022 23:08:04 +0800 Subject: [PATCH 05/11] =?UTF-8?q?[+]=20LeetCode=2028:=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E8=A1=A8=EF=BC=88=E4=B8=8D=E5=87=8F=E4=B8=80=EF=BC=89Java?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0028.实现strStr.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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 From 4e2c449827e3bc6a7b2e9c52803076118314f6b3 Mon Sep 17 00:00:00 2001 From: reoooh Date: Sun, 2 Jan 2022 22:36:47 +0800 Subject: [PATCH 06/11] 2022-0102: 0209 add ruby version --- problems/0209.长度最小的子数组.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 80822436..7c13961d 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: - ## 其他语言版本 @@ -291,5 +290,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 +``` + -----------------------
From f6d8350d24d6d24561db7af26a123ea0101cf4ae Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 2 Jan 2022 23:24:52 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880209.=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0209.长度最小的子数组.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 80822436..2c6ea775 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -214,6 +214,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 From e3f88873f66c432c7571f5278a2db2e00a5d0a88 Mon Sep 17 00:00:00 2001 From: Frogrey <35210929+Frogrey@users.noreply.github.com> Date: Mon, 3 Jan 2022 15:44:28 +0800 Subject: [PATCH 08/11] =?UTF-8?q?309.=E6=9C=80=E4=BD=B3=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB=E5=86=B7=E5=86=BB?= =?UTF-8?q?=E6=9C=9F=20=E9=80=92=E6=8E=A8=E4=BB=A3=E7=A0=81=E5=B0=91?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=B8=AA=E6=8B=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0309.最佳买卖股票时机含冷冻期.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 82555f80..10cdd5fd 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -95,7 +95,7 @@ p[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]; From b36f6755998b4ed3ca3e827e547bca95dce224da Mon Sep 17 00:00:00 2001 From: Dewittt <43514251+Dewittt@users.noreply.github.com> Date: Mon, 3 Jan 2022 15:56:46 +0800 Subject: [PATCH 09/11] =?UTF-8?q?Update=200347.=E5=89=8DK=E4=B8=AA?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 前k个,应该传入倒序的comparator --- problems/0347.前K个高频元素.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From 8399eed5204963a8968b7872cf4aa3c3ef486f8b Mon Sep 17 00:00:00 2001 From: Frogrey <35210929+Frogrey@users.noreply.github.com> Date: Mon, 3 Jan 2022 17:13:00 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20674.=20=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97?= =?UTF-8?q?=20Java=20=E8=B4=AA=E5=BF=83=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0674.最长连续递增序列.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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: > 动态规划: From a828e5f247c8095171d60b74eefdbeb1a6b3a9a5 Mon Sep 17 00:00:00 2001 From: Martin Hsu <31008681+Martin-Hsu@users.noreply.github.com> Date: Tue, 4 Jan 2022 14:08:00 +0800 Subject: [PATCH 11/11] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0739.每日温度.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 /** - * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑 + * 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到小 *

* 入站元素要和当前栈内栈首元素进行比较 * 若大于栈首则 则与元素下标做差