From 7117871beef8e1754107123a27047bd03354cd67 Mon Sep 17 00:00:00 2001 From: Yubin Li Date: Tue, 19 Mar 2024 20:40:46 +0800 Subject: [PATCH 01/41] =?UTF-8?q?Correct=20typo=20in=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 4411b5609f..457050d508 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -129,7 +129,7 @@ class Solution { return resList; } - //DFS--递归方式 + //BFS--递归方式 public void checkFun01(TreeNode node, Integer deep) { if (node == null) return; deep++; From 12bc134af8987659efeb76a973ffb3206ca9d59d Mon Sep 17 00:00:00 2001 From: Shuai Date: Sat, 23 Mar 2024 15:50:42 +0800 Subject: [PATCH 02/41] test --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 81558cc12c..3b562f5a1e 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -311,7 +311,7 @@ class Solution: for i in range(startIndex, len(candidates)): if total + candidates[i] > target: - continue + break total += candidates[i] path.append(candidates[i]) self.backtracking(candidates, target, total, i, path, result) @@ -664,4 +664,3 @@ public class Solution - From 0e1cc6d2771113ef0caeed9a26ec0e7e590e91e7 Mon Sep 17 00:00:00 2001 From: Meliodas417 <76930518+Meliodas417@users.noreply.github.com> Date: Sun, 24 Mar 2024 11:07:42 -0500 Subject: [PATCH 03/41] =?UTF-8?q?Update=200150.=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 除法逻辑不完整,假如是5/2=2没问题,但如果是-5/2,会向下取整,就变成-3了,可以单独定义一个函数。 底下最先弹出的数字实际上是最后进入的数字,也就是运算符右边的操作数,而第二个弹出的数字是第一个进入的数字,即运算符左边的操作数。stack.append(operation(op2, op1)) --- ...76\345\274\217\346\261\202\345\200\274.md" | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 663a68ea5c..fd4d072616 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -169,8 +169,12 @@ class Solution { ```python from operator import add, sub, mul -class Solution: - op_map = {'+': add, '-': sub, '*': mul, '/': lambda x, y: int(x / y)} +def div(x, y): + # 使用整数除法的向零取整方式 + return int(x / y) if x * y > 0 else -(abs(x) // abs(y)) + +class Solution(object): + op_map = {'+': add, '-': sub, '*': mul, '/': div} def evalRPN(self, tokens: List[str]) -> int: stack = [] @@ -186,18 +190,31 @@ class Solution: 另一种可行,但因为使用eval相对较慢的方法: ```python -class Solution: - def evalRPN(self, tokens: List[str]) -> int: +from operator import add, sub, mul + +def div(x, y): + # 使用整数除法的向零取整方式 + return int(x / y) if x * y > 0 else -(abs(x) // abs(y)) + +class Solution(object): + op_map = {'+': add, '-': sub, '*': mul, '/': div} + + def evalRPN(self, tokens): + """ + :type tokens: List[str] + :rtype: int + """ stack = [] - for item in tokens: - if item not in {"+", "-", "*", "/"}: - stack.append(item) + for token in tokens: + if token in self.op_map: + op1 = stack.pop() + op2 = stack.pop() + operation = self.op_map[token] + stack.append(operation(op2, op1)) else: - first_num, second_num = stack.pop(), stack.pop() - stack.append( - int(eval(f'{second_num} {item} {first_num}')) # 第一个出来的在运算符后面 - ) - return int(stack.pop()) # 如果一开始只有一个数,那么会是字符串形式的 + stack.append(int(token)) + return stack.pop() + ``` From f9ef9e9f20d97b8a1097905ca48ecad24fe8bdaf Mon Sep 17 00:00:00 2001 From: departever <2233492563@qq.com> Date: Mon, 25 Mar 2024 15:36:53 +0800 Subject: [PATCH 04/41] =?UTF-8?q?=E5=B0=86java=E7=89=88=E6=9C=AC=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=E4=B8=AD=E7=9A=84=E6=B3=A8=E9=87=8A=E7=9A=84?= =?UTF-8?q?=E6=80=BB=E5=AD=97=E4=BF=AE=E6=94=B9=E4=B8=AD=E5=AD=97=E4=BE=BF?= =?UTF-8?q?=E4=BA=8E=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index ff5aafed62..ca877f0a8b 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -133,7 +133,7 @@ class Solution { record[c - 'a'] -= 1; } - // 如果数组中存在负数,说明ransomNote字符串总存在magazine中没有的字符 + // 如果数组中存在负数,说明ransomNote字符串中存在magazine中没有的字符 for(int i : record){ if(i < 0){ return false; @@ -469,4 +469,3 @@ bool canConstruct(char* ransomNote, char* magazine) { - From 10af2bdde48a77285aaeae879fadf3b59ce123de Mon Sep 17 00:00:00 2001 From: alain Date: Mon, 25 Mar 2024 16:24:29 +0800 Subject: [PATCH 05/41] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=B3=95=20Rust=20=E7=A4=BA=E4=BE=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\210\206\346\237\245\346\211\276.md" | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 31e89ae344..8ff6a6fcdb 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -501,19 +501,19 @@ func search(nums: [Int], target: Int) -> Int { ### **Rust:** -(版本一)左闭右开区间 +(版本一)左闭右闭区间 ```rust use std::cmp::Ordering; impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { - let (mut left, mut right) = (0, nums.len()); - while left < right { + let (mut left, mut right) = (0_i32, nums.len() as i32 - 1); + while left <= right { let mid = (right + left) / 2; - match nums[mid].cmp(&target) { + match nums[mid as usize].cmp(&target) { Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid, - Ordering::Equal => return mid as i32, + Ordering::Greater => right = mid - 1, + Ordering::Equal => return mid, } } -1 @@ -521,19 +521,19 @@ impl Solution { } ``` -//(版本二)左闭右闭区间 +//(版本二)左闭右开区间 ```rust use std::cmp::Ordering; impl Solution { pub fn search(nums: Vec, target: i32) -> i32 { - let (mut left, mut right) = (0, nums.len()); - while left <= right { + let (mut left, mut right) = (0_i32, nums.len() as i32); + while left < right { let mid = (right + left) / 2; - match nums[mid].cmp(&target) { + match nums[mid as usize].cmp(&target) { Ordering::Less => left = mid + 1, - Ordering::Greater => right = mid - 1, - Ordering::Equal => return mid as i32, + Ordering::Greater => right = mid, + Ordering::Equal => return mid, } } -1 From 92850e8a47b1c7ecba1aef99fdbc38bf2d9a52e9 Mon Sep 17 00:00:00 2001 From: WilsonKuo <78621847+WilsonKuo@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:55:16 +0800 Subject: [PATCH 06/41] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Typo : std::men::replace -> std::mem::replace --- ...\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index d6d7e6c2ef..5baaef079b 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -545,7 +545,7 @@ impl Solution { let mut dummyHead = Box::new(ListNode::new(0)); dummyHead.next = head; let mut cur = dummyHead.as_mut(); - // 使用take()替换std::men::replace(&mut node.next, None)达到相同的效果,并且更普遍易读 + // 使用take()替换std::mem::replace(&mut node.next, None)达到相同的效果,并且更普遍易读 while let Some(nxt) = cur.next.take() { if nxt.val == val { cur.next = nxt.next; From e56494b0828ae4d1cee61b48f03da00d0a13bc3c Mon Sep 17 00:00:00 2001 From: alain Date: Wed, 27 Mar 2024 15:28:42 +0800 Subject: [PATCH 07/41] =?UTF-8?q?fix=200035=5F=E6=90=9C=E7=B4=A2=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E4=BD=8D=E7=BD=AE=20Rust=20=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...222\345\205\245\344\275\215\347\275\256.md" | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index 80b7e40e4a..34415524c6 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int { ```rust impl Solution { - pub fn search_insert(nums: Vec, target: i32) -> i32 { - let mut left = 0; - let mut right = nums.len(); - while left < right { + pub fn search_insert(nums: Vec, target: i32) -> i32 { + use std::cmp::Ordering::{Equal, Greater, Less}; + let (mut left, mut right) = (0, nums.len() as i32 - 1); + while left <= right { let mid = (left + right) / 2; - match nums[mid].cmp(&target) { - Ordering::Less => left = mid + 1, - Ordering::Equal => return ((left + right) / 2) as i32, - Ordering::Greater => right = mid, + match nums[mid as usize].cmp(&target) { + Less => left = mid + 1, + Equal => return mid, + Greater => right = mid - 1, } } - ((left + right) / 2) as i32 + right + 1 } } ``` From 96e9fcb3adc8676fd6ea0671d0ddbb7d9291eaeb Mon Sep 17 00:00:00 2001 From: jiyongchao Date: Sun, 31 Mar 2024 11:19:54 +0800 Subject: [PATCH 08/41] =?UTF-8?q?Update=200017.=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\204\345\255\227\346\257\215\347\273\204\345\220\210.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index cbd99f8d93..0ad26bfda3 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -260,7 +260,7 @@ class Solution { } - //每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuilder + //每次迭代获取一个字符串,所以会涉及大量的字符串拼接,所以这里选择更为高效的 StringBuilder StringBuilder temp = new StringBuilder(); //比如digits如果为"23",num 为0,则str表示2对应的 abc @@ -274,7 +274,7 @@ class Solution { String str = numString[digits.charAt(num) - '0']; for (int i = 0; i < str.length(); i++) { temp.append(str.charAt(i)); - //c + //递归,处理下一层 backTracking(digits, numString, num + 1); //剔除末尾的继续尝试 temp.deleteCharAt(temp.length() - 1); From 9437047fae6056b05e731f16eb3d7625b9c5bed5 Mon Sep 17 00:00:00 2001 From: LingFenglong <2808021998@qq.com> Date: Sun, 31 Mar 2024 21:55:19 +0800 Subject: [PATCH 09/41] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF=E4=B8=8A?= =?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix 300. 最长递增子序列 Java版本添加参数数组长度判断,现在可以AC --- ...270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index 6d82eae1e9..199ae59e4b 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -129,6 +129,7 @@ public: ```Java class Solution { public int lengthOfLIS(int[] nums) { + if (nums.length <= 1) return nums.length; int[] dp = new int[nums.length]; int res = 1; Arrays.fill(dp, 1); @@ -137,8 +138,8 @@ class Solution { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } - res = Math.max(res, dp[i]); } + res = Math.max(res, dp[i]); } return res; } From 0c2cc182304c826ef49e25dceffe4e6dc1187762 Mon Sep 17 00:00:00 2001 From: Junhao Qu <869142649qq@gmail.com> Date: Mon, 1 Apr 2024 22:25:54 -0700 Subject: [PATCH 10/41] fix java version for linked list 19 --- ...225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index c7a2cfcb3f..167b44ba8b 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -106,7 +106,7 @@ public ListNode removeNthFromEnd(ListNode head, int n){ ListNode slowIndex = dummyNode; // 只要快慢指针相差 n 个结点即可 - for (int i = 0; i < n ; i++){ + for (int i = 0; i <= n ; i++){ fastIndex = fastIndex.next; } From 3193d16f53372a6bd9d294538a83ef3e5d92a87f Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Tue, 2 Apr 2024 14:53:01 +0800 Subject: [PATCH 11/41] =?UTF-8?q?=E6=B7=BB=E5=8A=A00279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0=20Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\345\271\263\346\226\271\346\225\260.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index f7c06dbd87..1b29626f7a 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -271,7 +271,27 @@ class Solution: # 返回结果 return dp[n] +``` +```python +class Solution(object): + def numSquares(self, n): + # 先把可以选的数准备好,更好理解 + nums, num = [], 1 + while num ** 2 <= n: + nums.append(num ** 2) + num += 1 + # dp数组初始化 + dp = [float('inf')] * (n + 1) + dp[0] = 0 + # 遍历准备好的完全平方数 + for i in range(len(nums)): + # 遍历背包容量 + for j in range(nums[i], n+1): + dp[j] = min(dp[j], dp[j-nums[i]]+1) + # 返回结果 + return dp[-1] + ``` ### Go: From fb03de4c4ed82d239102718565c4734e01a211ac Mon Sep 17 00:00:00 2001 From: carlvine500 Date: Tue, 2 Apr 2024 17:46:11 +0800 Subject: [PATCH 12/41] =?UTF-8?q?Update=200236.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0236 java简洁迭代解法 --- ...54\345\205\261\347\245\226\345\205\210.md" | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 049f70c7ae..7e0a12fa01 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -247,7 +247,7 @@ public: ### Java - +递归 ```Java class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { @@ -271,6 +271,47 @@ class Solution { } } +``` +迭代 +```Java +class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + int max = Integer.MAX_VALUE; + Stack st = new Stack<>(); + TreeNode cur = root, pre = null; + while (cur != null || !st.isEmpty()) { + while (cur != null) { + st.push(cur); + cur = cur.left; + } + cur = st.pop(); + if (cur.right == null || cur.right == pre) { + // p/q是 中/左 或者 中/右 , 返回中 + if (cur == p || cur == q) { + if ((cur.left != null && cur.left.val == max) || (cur.right != null && cur.right.val == max)) { + return cur; + } + cur.val = max; + } + // p/q是 左/右 , 返回中 + if (cur.left != null && cur.left.val == max && cur.right != null && cur.right.val == max) { + return cur; + } + // MAX_VALUE 往上传递 + if ((cur.left != null && cur.left.val == max) || (cur.right != null && cur.right.val == max)) { + cur.val = max; + } + pre = cur; + cur = null; + } else { + st.push(cur); + cur = cur.right; + } + } + return null; + } +} + ``` ### Python From 1810d98f8149fa219ea9289bd014c3c3c52857e7 Mon Sep 17 00:00:00 2001 From: carlvine500 Date: Tue, 2 Apr 2024 17:59:18 +0800 Subject: [PATCH 13/41] =?UTF-8?q?Update=200037.=E8=A7=A3=E6=95=B0=E7=8B=AC?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0037.解数独 java bitmap解法 --- ...7.\350\247\243\346\225\260\347\213\254.md" | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index b5f54b1f8f..4dc15006aa 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -224,7 +224,7 @@ public: ### Java - +解法一: ```java class Solution { public void solveSudoku(char[][] board) { @@ -291,7 +291,73 @@ class Solution { } } ``` +解法二(bitmap标记) +``` +class Solution{ + int[] rowBit = new int[9]; + int[] colBit = new int[9]; + int[] square9Bit = new int[9]; + + public void solveSudoku(char[][] board) { + // 1 10 11 + for (int y = 0; y < board.length; y++) { + for (int x = 0; x < board[y].length; x++) { + int numBit = 1 << (board[y][x] - '1'); + rowBit[y] ^= numBit; + colBit[x] ^= numBit; + square9Bit[(y / 3) * 3 + x / 3] ^= numBit; + } + } + backtrack(board, 0); + } + + public boolean backtrack(char[][] board, int n) { + if (n >= 81) { + return true; + } + + // 快速算出行列编号 n/9 n%9 + int row = n / 9; + int col = n % 9; + + if (board[row][col] != '.') { + return backtrack(board, n + 1); + } + + for (char c = '1'; c <= '9'; c++) { + int numBit = 1 << (c - '1'); + if (!isValid(numBit, row, col)) continue; + { + board[row][col] = c; // 当前的数字放入到数组之中, + rowBit[row] ^= numBit; // 第一行rowBit[0],第一个元素eg: 1 , 0^1=1,第一个元素:4, 100^1=101,... + colBit[col] ^= numBit; + square9Bit[(row / 3) * 3 + col / 3] ^= numBit; + } + if (backtrack(board, n + 1)) return true; + { + board[row][col] = '.'; // 不满足条件,回退成'.' + rowBit[row] &= ~numBit; // 第一行rowBit[0],第一个元素eg: 1 , 101&=~1==>101&111111110==>100 + colBit[col] &= ~numBit; + square9Bit[(row / 3) * 3 + col / 3] &= ~numBit; + } + } + return false; + } + + boolean isValid(int numBit, int row, int col) { + // 左右 + if ((rowBit[row] & numBit) > 0) return false; + // 上下 + if ((colBit[col] & numBit) > 0) return false; + // 9宫格: 快速算出第n个九宫格,编号[0,8] , 编号=(row / 3) * 3 + col / 3 + if ((square9Bit[(row / 3) * 3 + col / 3] & numBit) > 0) return false; + return true; + } + +} + +``` ### Python ```python From 0422b137a2b04e74f757b2564a5e5bed3e921d2f Mon Sep 17 00:00:00 2001 From: ShimenTian <64670708+ShimenTian@users.noreply.github.com> Date: Tue, 2 Apr 2024 22:35:58 +0800 Subject: [PATCH 14/41] =?UTF-8?q?Update=20=E5=85=B3=E4=BA=8E=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6=EF=BC=8C=E4=BD=A0=E4=B8=8D?= =?UTF-8?q?=E7=9F=A5=E9=81=93=E7=9A=84=E9=83=BD=E5=9C=A8=E8=BF=99=E9=87=8C?= =?UTF-8?q?=EF=BC=81.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\275\345\234\250\350\277\231\351\207\214\357\274\201.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index 9d00a0f2a0..61f0a7ef6a 100644 --- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -19,7 +19,7 @@ **时间复杂度是一个函数,它定性描述该算法的运行时间**。 -我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的答题时间。 +我们在软件开发中,时间复杂度就是用来方便开发者估算出程序运行的大体时间。 那么该如何估计程序运行时间呢,通常会估算算法的操作单元数量来代表程序消耗的时间,这里默认CPU的每个单元运行消耗的时间都是相同的。 @@ -42,7 +42,7 @@ 我们主要关心的还是一般情况下的数据形式。 -**面试中说道算法的时间复杂度是多少指的都是一般情况**。但是如果面试官和我们深入探讨一个算法的实现以及性能的时候,就要时刻想着数据用例的不一样,时间复杂度也是不同的,这一点是一定要注意的。 +**面试中说的算法的时间复杂度是多少指的都是一般情况**。但是如果面试官和我们深入探讨一个算法的实现以及性能的时候,就要时刻想着数据用例的不一样,时间复杂度也是不同的,这一点是一定要注意的。 ## 不同数据规模的差异 @@ -61,7 +61,7 @@ 例如上图中20就是那个点,n只要大于20 常数项系数已经不起决定性作用了。 -**所以我们说的时间复杂度都是省略常数项系数的,是因为一般情况下都是默认数据规模足够的大,基于这样的事实,给出的算法时间复杂的的一个排行如下所示**: +**所以我们说的时间复杂度都是省略常数项系数的,是因为一般情况下都是默认数据规模足够的大,基于这样的事实,给出的算法时间复杂度的一个排行如下所示**: O(1)常数阶 < O(logn)对数阶 < O(n)线性阶 < O(nlogn)线性对数阶 < O(n^2)平方阶 < O(n^3)立方阶 < O(2^n)指数阶 From 6dd9d117ffc60cb6854f26814b05aa5be2033e84 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Tue, 2 Apr 2024 23:58:58 +0800 Subject: [PATCH 15/41] =?UTF-8?q?=E6=B7=BB=E5=8A=A00139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86=20Python=20DP=E5=89=AA=E6=9E=9D=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\350\257\215\346\213\206\345\210\206.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index a3d59ec718..5ad0fcb005 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -394,7 +394,28 @@ class Solution: dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j]) return dp[len(s)] ``` +DP(剪枝) +```python +class Solution(object): + def wordBreak(self, s, wordDict): + + # 先对单词按长度排序 + wordDict.sort(key=lambda x: len(x)) + n = len(s) + dp = [False] * (n + 1) + dp[0] = True + # 遍历背包 + for i in range(1, n + 1): + # 遍历单词 + for word in wordDict: + # 简单的 “剪枝” + if len(word) > i: + break + dp[i] = dp[i] or (dp[i - len(word)] and s[i - len(word): i] == word) + return dp[-1] + +``` ### Go: From fda18c06b7221b1c7bddd6b4115de5549e4fd5a4 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Wed, 3 Apr 2024 22:23:48 +0800 Subject: [PATCH 16/41] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=A4=9A?= =?UTF-8?q?=E9=87=8D=E8=83=8C=E5=8C=85=20Python=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\351\207\215\350\203\214\345\214\205.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 5d6440e3bf..29f157fbcc 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -204,6 +204,29 @@ class multi_pack{ ``` ### Python: +```python + +C, N = input().split(" ") +C, N = int(C), int(N) + +# value数组需要判断一下非空不然过不了 +weights = [int(x) for x in input().split(" ")] +values = [int(x) for x in input().split(" ") if x] +nums = [int(x) for x in input().split(" ")] + +dp = [0] * (C + 1) +# 遍历背包容量 +for i in range(N): + for j in range(C, weights[i] - 1, -1): + for k in range(1, nums[i] + 1): + # 遍历 k,如果已经大于背包容量直接跳出循环 + if k * weights[i] > j: + break + dp[j] = max(dp[j], dp[j - weights[i] * k] + values[i] * k) +print(dp[-1]) + +``` + ### Go: From 616258da2083e1c9943364acbc98cfdc83d0e0fc Mon Sep 17 00:00:00 2001 From: Xiong Gu Date: Thu, 4 Apr 2024 14:57:42 -0400 Subject: [PATCH 17/41] add dfs function operation in the main traversal function --- ...2\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..e22e1bf343 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -189,7 +189,7 @@ class Solution: res.append(node.val) dfs(node.left) dfs(node.right) - + dfs(root) return res ``` @@ -207,6 +207,7 @@ class Solution: res.append(node.val) dfs(node.right) + dfs(root) return res ``` ```python @@ -224,7 +225,7 @@ class Solution: dfs(node.left) dfs(node.right) res.append(node.val) - + dfs(root) return res ``` From 2d0b16c92d6aaebf49b56d996a4ea5dd7651e8ac Mon Sep 17 00:00:00 2001 From: Xiong Gu Date: Thu, 4 Apr 2024 15:00:23 -0400 Subject: [PATCH 18/41] formatting the former commit --- ...32\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index e22e1bf343..b18067e8b7 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -189,6 +189,7 @@ class Solution: res.append(node.val) dfs(node.left) dfs(node.right) + dfs(root) return res @@ -206,7 +207,7 @@ class Solution: dfs(node.left) res.append(node.val) dfs(node.right) - + dfs(root) return res ``` @@ -225,6 +226,7 @@ class Solution: dfs(node.left) dfs(node.right) res.append(node.val) + dfs(root) return res ``` From 1c4b9a252fd004a9ec3771b6dbfc707818e4f1d4 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Sat, 6 Apr 2024 10:38:12 +0800 Subject: [PATCH 19/41] =?UTF-8?q?=E6=B7=BB=E5=8A=A00583=20=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=20Python=E8=A7=A3=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\351\231\244\346\223\215\344\275\234.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 7dbb8ef542..4816c37cc2 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -234,6 +234,25 @@ class Solution: return dp[-1][-1] ``` +> 版本 2 + +```python +class Solution(object): + def minDistance(self, word1, word2): + m, n = len(word1), len(word2) + + # dp 求解两字符串最长公共子序列 + dp = [[0] * (n+1) for _ in range(m+1)] + for i in range(1, m+1): + for j in range(1, n+1): + if word1[i-1] == word2[j-1]: + dp[i][j] = dp[i-1][j-1] + 1 + else: + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + + # 删去最长公共子序列以外元素 + return m + n - 2 * dp[-1][-1] +``` ### Go: ```go From 7df5157aab58935f0dd9784e4968bd73f1facbdf Mon Sep 17 00:00:00 2001 From: Jasen Chao <68803975+JasenChao@users.noreply.github.com> Date: Sat, 6 Apr 2024 15:09:55 +0800 Subject: [PATCH 20/41] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix typos --- "problems/0494.\347\233\256\346\240\207\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 02edad4d8b..f030ae77e3 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -101,7 +101,7 @@ public: int sum = 0; for (int i = 0; i < nums.size(); i++) sum += nums[i]; if (S > sum) return 0; // 此时没有方案 - if ((S + sum) % 2) return 0; // 此时没有方案,两个int相加的时候要各位小心数值溢出的问题 + if ((S + sum) % 2) return 0; // 此时没有方案,两个int相加的时候要格外小心数值溢出的问题 int bagSize = (S + sum) / 2; // 转变为组合总和问题,bagsize就是要求的和 // 以下为回溯法代码 From 74d38fc6965445e35946d06300d60ff2c5d0a130 Mon Sep 17 00:00:00 2001 From: Jingqi Sun <71240100+xiaoqingma0@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:06:39 +0800 Subject: [PATCH 21/41] =?UTF-8?q?Update=20=E5=93=88=E5=B8=8C=E8=A1=A8?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改typo,优化句子表达。 --- ...\250\347\220\206\350\256\272\345\237\272\347\241\200.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index e426c65791..816f673e91 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -12,11 +12,11 @@ ## 哈希表 -首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 +首先什么是哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 > 哈希表是根据关键码的值而直接进行访问的数据结构。 -这么这官方的解释可能有点懵,其实直白来讲其实数组就是一张哈希表。 +这么官方的解释可能有点懵,其实直白来讲其实数组就是一张哈希表。 哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素,如下图所示: @@ -113,7 +113,7 @@ std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底 其他语言例如:java里的HashMap ,TreeMap 都是一样的原理。可以灵活贯通。 -虽然std::set、std::multiset 的底层实现是红黑树,不是哈希表,std::set、std::multiset 使用红黑树来索引和存储,不过给我们的使用方式,还是哈希法的使用方式,即key和value。所以使用这些数据结构来解决映射问题的方法,我们依然称之为哈希法。 map也是一样的道理。 +虽然std::set和std::multiset 的底层实现基于红黑树而非哈希表,它们通过红黑树来索引和存储数据。不过给我们的使用方式,还是哈希法的使用方式,即依靠键(key)来访问值(value)。所以使用这些数据结构来解决映射问题的方法,我们依然称之为哈希法。std::map也是一样的道理。 这里在说一下,一些C++的经典书籍上 例如STL源码剖析,说到了hash_set hash_map,这个与unordered_set,unordered_map又有什么关系呢? From e2b92184a88e4d5287e982cd97d4a425cf88acbc Mon Sep 17 00:00:00 2001 From: kankan-web <2728360489@qq.com> Date: Sun, 7 Apr 2024 21:25:29 +0800 Subject: [PATCH 22/41] =?UTF-8?q?Update=200770.=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\345\214\205\347\211\210\346\234\254.md" | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 622b1117e7..fd5c24fb2b 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -211,10 +211,33 @@ func main() { ``` ### JavaScript: - +```javaScript +var climbStairs = function (n) { + let dp = new Array(n + 1).fill(0); + dp[0] = 1; + // 排列题,注意循环顺序,背包在外物品在内 + for (let j = 1; j <= n; j++) {//遍历背包 + for (let i = 1; i <= 2; i++) {//遍历物品 + if (j - i >= 0) dp[j] = dp[j] + dp[j - i]; + } + } + return dp[n]; +} +``` ### TypeScript: - +```typescript +var climbStairs = function (n: number): number { + let dp: number[] = new Array(n + 1).fill(0); + dp[0] = 1; + for (let j = 1; j <= n; j++) {//遍历背包 + for (let i = 1; i <= 2; i++) {//遍历物品 + if (j - i >= 0) dp[j] = dp[j] + dp[j - i]; + } + } + return dp[n]; +} +``` ### Rust: From c5a6c3e4104bb7a17a55edc44db6cbf7c55e642e Mon Sep 17 00:00:00 2001 From: markwang Date: Mon, 8 Apr 2024 17:12:03 +0800 Subject: [PATCH 23/41] =?UTF-8?q?977.=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=9A=84=E5=B9=B3=E6=96=B9Go=E6=8E=92=E5=BA=8F=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\273\204\347\232\204\345\271\263\346\226\271.md" | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" index 5bdbcbc7e0..a39902b341 100644 --- "a/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" +++ "b/problems/0977.\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\271\263\346\226\271.md" @@ -181,6 +181,17 @@ class Solution: ### Go: ```Go +// 排序法 +func sortedSquares(nums []int) []int { + for i, val := range nums { + nums[i] *= val + } + sort.Ints(nums) + return nums +} +``` +```Go +// 双指针法 func sortedSquares(nums []int) []int { n := len(nums) i, j, k := 0, n-1, n-1 From baab80473377952f85095a4ec4aeee2febc2101c Mon Sep 17 00:00:00 2001 From: skylarkkk <116788771+skylarkkk@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:00:59 +0800 Subject: [PATCH 24/41] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\204\351\200\222\345\275\222\351\201\215\345\216\206.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..3fc1d24105 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -189,7 +189,7 @@ class Solution: res.append(node.val) dfs(node.left) dfs(node.right) - + dfs(root) return res ``` @@ -206,7 +206,7 @@ class Solution: dfs(node.left) res.append(node.val) dfs(node.right) - + dfs(root) return res ``` ```python @@ -224,7 +224,7 @@ class Solution: dfs(node.left) dfs(node.right) res.append(node.val) - + dfs(root) return res ``` From 6b8b526dbfd646b46a997d4c001c248e11b2c10f Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Thu, 11 Apr 2024 17:20:55 +0800 Subject: [PATCH 25/41] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=200209.?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84.md=20=E7=9A=84=20Typescript=20=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\346\225\260\347\273\204.md" | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 5934d5e364..d2dfe1ee9e 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -270,22 +270,21 @@ var minSubArrayLen = function(target, nums) { ```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++; + let left: number = 0, + res: number = Infinity, + subLen: number = 0, + sum: number = 0; + for (let right: number = 0; right < nums.length; right++) { + sum += nums[right]; + while (sum >= target) { + subLen = right - left + 1; + res = Math.min(res, subLen); + sum -= nums[left]; + left++; } - return res === nums.length + 1 ? 0 : res; -}; + } + return res === Infinity ? 0 : res; +} ``` ### Swift: From ff75974e30459b23a44d18a9180e8c9def80de46 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Fri, 12 Apr 2024 16:33:29 +0800 Subject: [PATCH 26/41] =?UTF-8?q?fix:=20update=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=E5=92=8C=E6=95=B0=E7=BB=84=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" | 2 +- ...\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index 7550ce0254..d6a4e5401f 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -16,7 +16,7 @@ **数组是存放在连续内存空间上的相同类型数据的集合。** -数组可以方便的通过下标索引的方式获取到下标下对应的数据。 +数组可以方便的通过下标索引的方式获取到下标对应的数据。 举一个字符数组的例子,如图所示: diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index d104c883f7..1eb5b3f5a6 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -16,7 +16,7 @@ **数组是存放在连续内存空间上的相同类型数据的集合。** -数组可以方便的通过下标索引的方式获取到下标下对应的数据。 +数组可以方便的通过下标索引的方式获取到下标对应的数据。 举一个字符数组的例子,如图所示: From 3c9d6c408ac7286f19ddc096623b4454b3e8765c Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Fri, 12 Apr 2024 16:40:11 +0800 Subject: [PATCH 27/41] =?UTF-8?q?fix:=20update=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=E5=92=8C=E6=95=B0=E7=BB=84=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" | 2 +- ...\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" index d6a4e5401f..646580368b 100644 --- "a/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\346\225\260\347\273\204\346\200\273\347\273\223\347\257\207.md" @@ -27,7 +27,7 @@ * **数组下标都是从0开始的。** * **数组内存空间的地址是连续的** -正是**因为数组的在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** +正是**因为数组在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** 例如删除下标为3的元素,需要对下标为3的元素后面的所有元素都要做移动操作,如图所示: diff --git "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" index 1eb5b3f5a6..3492f4cc8c 100644 --- "a/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\225\260\347\273\204\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -29,7 +29,7 @@ * **数组下标都是从0开始的。** * **数组内存空间的地址是连续的** -正是**因为数组的在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** +正是**因为数组在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。** 例如删除下标为3的元素,需要对下标为3的元素后面的所有元素都要做移动操作,如图所示: From bdc06a4960a6ce17c4a279e9f0b8220a2a2b059f Mon Sep 17 00:00:00 2001 From: 502y <53784463+502y@users.noreply.github.com> Date: Sat, 13 Apr 2024 09:51:06 +0800 Subject: [PATCH 28/41] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=20Dart=E7=9B=B8=E5=90=91?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index cb342586d9..5f681c5f8a 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -476,6 +476,32 @@ public class Solution { } ``` +###Dart: +```dart +int removeElement(List nums, int val) { + //相向双指针法 + var left = 0; + var right = nums.length - 1; + while (left <= right) { + //寻找左侧的val,将其被右侧非val覆盖 + if (nums[left] == val) { + while (nums[right] == val&&left<=right) { + right--; + if (right < 0) { + return 0; + } + } + nums[left] = nums[right--]; + } else { + left++; + } + } + //覆盖后可以将0至left部分视为所需部分 + return left; +} + +``` +

From a68d7c99370b6e2dd20c6485c1c408b8b0bf5327 Mon Sep 17 00:00:00 2001 From: Nicolas Leigh Date: Thu, 18 Apr 2024 13:21:39 +0800 Subject: [PATCH 29/41] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a break statement to break the for-loop in the Typescript solution. --- .../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index 80218cb535..d40cade805 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -349,6 +349,7 @@ function twoSum(nums: number[], target: number): number[] { index = helperMap.get(target - nums[i]); if (index !== undefined) { resArr = [i, index]; + break; } helperMap.set(nums[i], i); } From 62cfba1210951f64a726567170705ecbf1866886 Mon Sep 17 00:00:00 2001 From: Shuxiao Wang Date: Fri, 19 Apr 2024 09:52:36 +0200 Subject: [PATCH 30/41] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20`0063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md`=20=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BC=A9=E8=BF=9B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index 8c208ea865..2419b6a8fd 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -145,7 +145,7 @@ public: int uniquePathsWithObstacles(vector>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); - if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0 + if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0 return 0; vector> dp(m, vector(n, 0)); for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1; From 9fdaeb5b9075ed5195e828d9426c1dd98a7212ec Mon Sep 17 00:00:00 2001 From: Vox Dai Date: Sun, 21 Apr 2024 09:16:13 +0800 Subject: [PATCH 31/41] =?UTF-8?q?Update=200111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6.md=20/=20Fix=20ty?= =?UTF-8?q?po?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 部分Markdown代码块没有标注语言导致代码没有高亮 --- ...\204\346\234\200\345\260\217\346\267\261\345\272\246.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index 6d1632d593..9619b75b40 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -64,7 +64,7 @@ 代码如下: -``` +```CPP int getDepth(TreeNode* node) ``` @@ -74,14 +74,14 @@ int getDepth(TreeNode* node) 代码如下: -``` +```CPP if (node == NULL) return 0; ``` 3. 确定单层递归的逻辑 这块和求最大深度可就不一样了,一些同学可能会写如下代码: -``` +```CPP int leftDepth = getDepth(node->left); int rightDepth = getDepth(node->right); int result = 1 + min(leftDepth, rightDepth); From 19b76720a2277f7ca8a72ffdf7e1a7c810bfe951 Mon Sep 17 00:00:00 2001 From: Vox Dai Date: Sun, 21 Apr 2024 09:37:42 +0800 Subject: [PATCH 32/41] =?UTF-8?q?Update=20'''0222.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA?= =?UTF-8?q?=E6=95=B0.md'''=20Fix=20Typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复部分代码块无高亮 --- ...\204\350\212\202\347\202\271\344\270\252\346\225\260.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index d93d2a3381..69403607c3 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -54,7 +54,7 @@ 1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。 代码如下: -``` +```CPP int getNodesNum(TreeNode* cur) { ``` @@ -62,7 +62,7 @@ int getNodesNum(TreeNode* cur) { 代码如下: -``` +```CPP if (cur == NULL) return 0; ``` @@ -70,7 +70,7 @@ if (cur == NULL) return 0; 代码如下: -``` +```CPP int leftNum = getNodesNum(cur->left); // 左 int rightNum = getNodesNum(cur->right); // 右 int treeNum = leftNum + rightNum + 1; // 中 From 1f94655e9a3f2a91075b43477ef2cbe648891083 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Tue, 23 Apr 2024 15:26:56 +0800 Subject: [PATCH 33/41] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=960019.?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0?= =?UTF-8?q?=E7=AC=ACN=E4=B8=AA=E8=8A=82=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 9e180f753e..4a973edb73 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -22,10 +22,12 @@ 输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] + 示例 2: 输入:head = [1], n = 1 输出:[] + 示例 3: 输入:head = [1,2], n = 1 @@ -193,16 +195,18 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode { * @param {number} n * @return {ListNode} */ -var removeNthFromEnd = function(head, n) { - let ret = new ListNode(0, head), - slow = fast = ret; - while(n--) fast = fast.next; - while (fast.next !== null) { - fast = fast.next; - slow = slow.next - }; - slow.next = slow.next.next; - return ret.next; +var removeNthFromEnd = function (head, n) { + // 创建哨兵节点,简化解题逻辑 + let dummyHead = new ListNode(0, head); + let fast = dummyHead; + let slow = dummyHead; + while (n--) fast = fast.next; + while (fast.next !== null) { + slow = slow.next; + fast = fast.next; + } + slow.next = slow.next.next; + return dummyHead.next; }; ``` ### TypeScript: From f755ecc5fcce894cdbcc9c7eadfaf7bb123a3b1b Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Thu, 25 Apr 2024 17:34:06 +0800 Subject: [PATCH 34/41] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 111 ++++++++++++------ 1 file changed, 75 insertions(+), 36 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 4621d4a739..ac42f69fed 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -48,7 +48,7 @@ void traversal(TreeNode* cur, vector& vec) if (cur == NULL) return; ``` -3. **确定单层递归的逻辑**:前序遍历是中左右的循序,所以在单层递归的逻辑,是要先取中节点的数值,代码如下: +3. **确定单层递归的逻辑**:前序遍历是中左右的顺序,所以在单层递归的逻辑,是要先取中节点的数值,代码如下: ```cpp vec.push_back(cur->val); // 中 @@ -287,52 +287,91 @@ func postorderTraversal(root *TreeNode) (res []int) { 前序遍历: ```Javascript var preorderTraversal = function(root) { - let res=[]; - const dfs=function(root){ - if(root===null)return ; - //先序遍历所以从父节点开始 - res.push(root.val); - //递归左子树 - dfs(root.left); - //递归右子树 - dfs(root.right); - } - //只使用一个参数 使用闭包进行存储结果 - dfs(root); - return res; +// 第一种 +// let res=[]; +// const dfs=function(root){ +// if(root===null)return ; +// //先序遍历所以从父节点开始 +// res.push(root.val); +// //递归左子树 +// dfs(root.left); +// //递归右子树 +// dfs(root.right); +// } +// //只使用一个参数 使用闭包进行存储结果 +// dfs(root); +// return res; +// 第二种 + return root + ? [ + // 前序遍历:中左右 + root.val, + // 递归左子树 + ...preorderTraversal(root.left), + // 递归右子树 + ...preorderTraversal(root.right), + ] + : []; }; ``` 中序遍历 ```javascript var inorderTraversal = function(root) { - let res=[]; - const dfs=function(root){ - if(root===null){ - return ; - } - dfs(root.left); - res.push(root.val); - dfs(root.right); - } - dfs(root); - return res; +// 第一种 + + // let res=[]; + // const dfs=function(root){ + // if(root===null){ + // return ; + // } + // dfs(root.left); + // res.push(root.val); + // dfs(root.right); + // } + // dfs(root); + // return res; + +// 第二种 + return root + ? [ + // 中序遍历:左中右 + // 递归左子树 + ...inorderTraversal(root.left), + root.val, + // 递归右子树 + ...inorderTraversal(root.right), + ] + : []; }; ``` 后序遍历 ```javascript var postorderTraversal = function(root) { - let res=[]; - const dfs=function(root){ - if(root===null){ - return ; - } - dfs(root.left); - dfs(root.right); - res.push(root.val); - } - dfs(root); - return res; + // 第一种 + // let res=[]; + // const dfs=function(root){ + // if(root===null){ + // return ; + // } + // dfs(root.left); + // dfs(root.right); + // res.push(root.val); + // } + // dfs(root); + // return res; + + // 第二种 + // 后续遍历:左右中 + return root + ? [ + // 递归左子树 + ...postorderTraversal(root.left), + // 递归右子树 + ...postorderTraversal(root.right), + root.val, + ] + : []; }; ``` From 5ee2f42edb5765f6e28f2022fbcd257f27914b06 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 16:12:38 +0800 Subject: [PATCH 35/41] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 42 ++++++++++--------- ...06\350\256\272\345\237\272\347\241\200.md" | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 4411b5609f..d834eef7e1 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -692,27 +692,29 @@ func levelOrderBottom(root *TreeNode) [][]int { #### Javascript: ```javascript -var levelOrderBottom = function(root) { - let res = [], queue = []; - queue.push(root); - while(queue.length && root!==null) { - // 存放当前层级节点数组 - let curLevel = []; - // 计算当前层级节点数量 - let length = queue.length; - while(length--) { - let node = queue.shift(); - // 把当前层节点存入curLevel数组 - curLevel.push(node.val); - // 把下一层级的左右节点存入queue队列 - node.left && queue.push(node.left); - node.right && queue.push(node.right); - } - // 从数组前头插入值,避免最后反转数组,减少运算时间 - res.unshift(curLevel); - } - return res; +var levelOrderBottom = function (root) { + let res = [], + queue = []; + queue.push(root); + while (queue.length && root !== null) { + // 存放当前层级节点数组 + let curLevel = []; + // 计算当前层级节点数量 + let length = queue.length; + while (length--) { + let node = queue.shift(); + // 把当前层节点存入curLevel数组 + curLevel.push(node.val); + // 把下一层级的左右节点存入queue队列 + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + // 从数组前头插入值,避免最后反转数组,减少运算时间 + res.unshift(curLevel); + } + return res; }; + ``` #### TypeScript: diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index 7f847f8fae..15c5af3a64 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -71,7 +71,7 @@ 有递归的地方就有回溯,那么回溯在哪里呢? -就地递归函数的下面,例如如下代码: +就递归函数的下面,例如如下代码: ```cpp void dfs(参数) { From d7863c9dbfb124c93a1e4d589827b8397448553c Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 16:36:42 +0800 Subject: [PATCH 36/41] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index d834eef7e1..b9943f3982 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1142,7 +1142,7 @@ impl Solution { ### 思路 -本题就是层序遍历的时候把一层求个总和在取一个均值。 +本题就是层序遍历的时候把一层求个总和再取一个均值。 C++代码: From 4ca945f998d047eb7a0d9ea9308e53f46fd274fa Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 17:08:26 +0800 Subject: [PATCH 37/41] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index b9943f3982..0a98ab88a3 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -1297,26 +1297,26 @@ func averageOfLevels(root *TreeNode) []float64 { ```javascript var averageOfLevels = function(root) { - //层级平均值 - let res = [], queue = []; - queue.push(root); - - while(queue.length && root!==null) { - //每一层节点个数 - let length = queue.length; - //sum记录每一层的和 - let sum = 0; - for(let i=0; i < length; i++) { - let node = queue.shift(); - sum += node.val; - node.left && queue.push(node.left); - node.right && queue.push(node.right); - } - //每一层的平均值存入数组res - res.push(sum/length); + let res = [], + queue = []; + queue.push(root); + while (queue.length) { + // 每一层节点个数; + let lengthLevel = queue.length, + len = queue.length, + // sum记录每一层的和; + sum = 0; + while (lengthLevel--) { + const node = queue.shift(); + sum += node.val; + // 队列存放下一层节点 + node.left && queue.push(node.left); + node.right && queue.push(node.right); } - - return res; + // 求平均值 + res.push(sum / len); + } + return res; }; ``` @@ -1927,26 +1927,28 @@ func max(x, y int) int { #### Javascript: ```javascript -var largestValues = function(root) { - //使用层序遍历 - let res = [], queue = []; - queue.push(root); - - while(root !== null && queue.length) { - //设置max初始值就是队列的第一个元素 - let max = queue[0].val; - let length = queue.length; - while(length--) { - let node = queue.shift(); - max = max > node.val ? max : node.val; - node.left && queue.push(node.left); - node.right && queue.push(node.right); - } - //把每一层的最大值放到res数组 - res.push(max); - } - +var largestValues = function (root) { + let res = [], + queue = []; + queue.push(root); + if (root === null) { return res; + } + while (queue.length) { + let lengthLevel = queue.length, + // 初始值设为负无穷大 + max = -Infinity; + while (lengthLevel--) { + const node = queue.shift(); + // 在当前层中找到最大值 + max = Math.max(max, node.val); + // 找到下一层的节点 + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + res.push(max); + } + return res; }; ``` From 2d37cd39e02789d09387b5dc1176fd8f71adf769 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Mon, 29 Apr 2024 18:19:48 +0800 Subject: [PATCH 38/41] =?UTF-8?q?refactor:=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index 0a98ab88a3..1cb916b41c 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -2809,21 +2809,23 @@ func maxDepth(root *TreeNode) int { * @param {TreeNode} root * @return {number} */ -var maxDepth = function(root) { - // 最大的深度就是二叉树的层数 - if (root === null) return 0; - let queue = [root]; - let height = 0; - while (queue.length) { - let n = queue.length; - height++; - for (let i=0; i Date: Wed, 1 May 2024 16:08:36 +0800 Subject: [PATCH 39/41] =?UTF-8?q?refactor:=200077.=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 103fb627f5..35ed5fad49 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -469,6 +469,32 @@ func dfs(n int, k int, start int) { ``` ### Javascript +未剪枝: + +```js +var combine = function (n, k) { + // 回溯法 + let result = [], + path = []; + let backtracking = (_n, _k, startIndex) => { + // 终止条件 + if (path.length === _k) { + result.push(path.slice()); + return; + } + // 循环本层集合元素 + for (let i = startIndex; i <= _n; i++) { + path.push(i); + // 递归 + backtracking(_n, _k, i + 1); + // 回溯操作 + path.pop(); + } + }; + backtracking(n, k, 1); + return result; +}; +``` 剪枝: From d1d7cdeecd2922b86edccb7cfd135117a5926a60 Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Wed, 1 May 2024 19:10:32 +0800 Subject: [PATCH 40/41] =?UTF-8?q?refactor:=200077.=E7=BB=84=E5=90=88.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 38 ++++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 35ed5fad49..f394b3d84c 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -499,24 +499,28 @@ var combine = function (n, k) { 剪枝: ```javascript -let result = [] -let path = [] -var combine = function(n, k) { - result = [] - combineHelper(n, k, 1) - return result +var combine = function (n, k) { + // 回溯法 + let result = [], + path = []; + let backtracking = (_n, _k, startIndex) => { + // 终止条件 + if (path.length === _k) { + result.push(path.slice()); + return; + } + // 循环本层集合元素 + for (let i = startIndex; i <= _n - (_k - path.length) + 1; i++) { + path.push(i); + // 递归 + backtracking(_n, _k, i + 1); + // 回溯操作 + path.pop(); + } + }; + backtracking(n, k, 1); + return result; }; -const combineHelper = (n, k, startIndex) => { - if (path.length === k) { - result.push([...path]) - return - } - for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) { - path.push(i) - combineHelper(n, k, i + 1) - path.pop() - } -} ``` ### TypeScript From 5e6a657d90fe9a3ba7c86aa8505898e0f7ed739b Mon Sep 17 00:00:00 2001 From: qiufeihong2018 <15058301288@163.com> Date: Wed, 1 May 2024 19:51:01 +0800 Subject: [PATCH 41/41] =?UTF-8?q?refactor:=200216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8CIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\220\210\346\200\273\345\222\214III.md" | 93 ++++++++++++++----- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index ac28f9fcfa..d331726e55 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -417,6 +417,7 @@ func dfs(k, n int, start int, sum int) { ``` ### JavaScript +- 未剪枝: ```js /** @@ -424,32 +425,74 @@ func dfs(k, n int, start int, sum int) { * @param {number} n * @return {number[][]} */ -var combinationSum3 = function(k, n) { - let res = []; - let path = []; - let sum = 0; - const dfs = (path,index) => { - // 剪枝操作 - if (sum > n){ - return - } - if (path.length == k) { - if(sum == n){ - res.push([...path]); - return - } - } - for (let i = index; i <= 9 - (k-path.length) + 1;i++) { - path.push(i); - sum = sum + i; - index += 1; - dfs(path,index); - sum -= i - path.pop() - } +var combinationSum3 = function (k, n) { + // 回溯法 + let result = [], + path = []; + const backtracking = (_k, targetSum, sum, startIndex) => { + // 终止条件 + if (path.length === _k) { + if (sum === targetSum) { + result.push(path.slice()); + } + // 如果总和不相等,就直接返回 + return; } - dfs(path,1); - return res + + // 循环当前节点,因为只使用数字1到9,所以最大是9 + for (let i = startIndex; i <= 9; i++) { + path.push(i); + sum += i; + // 回调函数 + backtracking(_k, targetSum, sum, i + 1); + // 回溯 + sum -= i; + path.pop(); + } + }; + backtracking(k, n, 0, 1); + return result; +}; +``` + +- 剪枝: + +```js +/** + * @param {number} k + * @param {number} n + * @return {number[][]} + */ +var combinationSum3 = function (k, n) { + // 回溯法 + let result = [], + path = []; + const backtracking = (_k, targetSum, sum, startIndex) => { + if (sum > targetSum) { + return; + } + // 终止条件 + if (path.length === _k) { + if (sum === targetSum) { + result.push(path.slice()); + } + // 如果总和不相等,就直接返回 + return; + } + + // 循环当前节点,因为只使用数字1到9,所以最大是9 + for (let i = startIndex; i <= 9 - (_k - path.length) + 1; i++) { + path.push(i); + sum += i; + // 回调函数 + backtracking(_k, targetSum, sum, i + 1); + // 回溯 + sum -= i; + path.pop(); + } + }; + backtracking(k, n, 0, 1); + return result; }; ```