From e1f3bdd973c88cada2a59dbc28fecbe0b798d246 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 17 Mar 2022 16:30:06 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880077.=E7=BB=84?= =?UTF-8?q?=E5=90=88.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 2bd7a287..a1403c37 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -456,6 +456,27 @@ const combineHelper = (n, k, startIndex) => { } ``` +## TypeScript + +```typescript +function combine(n: number, k: number): number[][] { + let resArr: number[][] = []; + function backTracking(n: number, k: number, startIndex: number, tempArr: number[]): void { + if (tempArr.length === k) { + resArr.push(tempArr.slice()); + return; + } + for (let i = startIndex; i <= n - k + 1 + tempArr.length; i++) { + tempArr.push(i); + backTracking(n, k, i + 1, tempArr); + tempArr.pop(); + } + } + backTracking(n, k, 1, []); + return resArr; +}; +``` + ## Go From a90db6e830474a70dd445a77cef578627374ca5e Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Thu, 17 Mar 2022 13:38:45 -0500 Subject: [PATCH 02/13] =?UTF-8?q?0941.=E6=9C=89=E6=95=88=E7=9A=84=E5=B1=B1?= =?UTF-8?q?=E8=84=89=E6=95=B0=E7=BB=84=20python3=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0941.有效的山脉数组 python3 从原方法更新到双指针方法 --- problems/0941.有效的山脉数组.md | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md index a2444cc1..4b7a978c 100644 --- a/problems/0941.有效的山脉数组.md +++ b/problems/0941.有效的山脉数组.md @@ -106,22 +106,15 @@ class Solution { ```python class Solution: def validMountainArray(self, arr: List[int]) -> bool: - if len(arr) < 3 : - return False - - i = 1 - flagIncrease = False # 上升 - flagDecrease = False # 下降 - - while i < len(arr) and arr[i-1] < arr[i]: - flagIncrease = True - i += 1 - - while i < len(arr) and arr[i-1] > arr[i]: - flagDecrease = True - i += 1 - - return i == len(arr) and flagIncrease and flagDecrease + left, right = 0, len(arr)-1 + + while left < len(arr)-1 and arr[left+1] > arr[left]: + left += 1 + + while right > 0 and arr[right-1] > arr[right]: + right -= 1 + + return left == right and right != 0 and left != len(arr)-1 ``` From a95b24a2290c72197e9107e38e91e11ffa92ef3d Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Thu, 17 Mar 2022 13:43:03 -0500 Subject: [PATCH 03/13] =?UTF-8?q?0225.=E7=94=A8=E9=98=9F=E5=88=97=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=A0=88=20python3=20=E6=9B=B4=E6=96=B0=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0225.用队列实现栈 python3 更新优化方法(只使用一个队列) --- problems/0225.用队列实现栈.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index c45917db..3457c4b3 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -354,6 +354,32 @@ class MyStack: return len(self.queue_in) == 0 ``` +优化,使用一个队列实现 +```python +class MyStack: + + def __init__(self): + self.que = deque() + + def push(self, x: int) -> None: + self.que.append(x) + + def pop(self) -> int: + if self.empty(): + return None + for i in range(len(self.que)-1): + self.que.append(self.que.popleft()) + return self.que.popleft() + + def top(self) -> int: + if self.empty(): + return None + return self.que[-1] + + def empty(self) -> bool: + return not self.que +``` + Go: From c77840639653808f528af05cd90ebef0c160cbf0 Mon Sep 17 00:00:00 2001 From: IcePigZDB Date: Sat, 19 Mar 2022 20:05:04 +0800 Subject: [PATCH 04/13] fix some small typo Signed-off-by: IcePigZDB --- problems/0020.有效的括号.md | 2 +- problems/0112.路径总和.md | 2 +- problems/0239.滑动窗口最大值.md | 2 +- problems/0377.组合总和Ⅳ.md | 2 +- problems/0647.回文子串.md | 4 ++-- problems/栈与队列理论基础.md | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 01fc7b93..7bb7f746 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -125,7 +125,7 @@ public: } }; ``` -技巧性的东西没有固定的学习方法,还是要多看多练,自己总灵活运用了。 +技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。 ## 其他语言版本 diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 5db36687..f80cf59c 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -212,7 +212,7 @@ public: }; ``` -如果大家完全理解了本地的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 +如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 # 113. 路径总和ii diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index ce215a7e..f269450f 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -193,7 +193,7 @@ public: # 扩展 -大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本地中的单调队列实现就是固定的写法哈。 +大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本题中的单调队列实现就是固定的写法哈。 大家貌似对deque也有一些疑惑,C++中deque是stack和queue默认的底层实现容器(这个我们之前已经讲过啦),deque是可以两边扩展的,而且deque里元素并不是严格的连续分布的。 diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index 940d81b8..aaf27e61 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -127,7 +127,7 @@ public: ``` -C++测试用例有超过两个树相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 +C++测试用例有两个数相加超过int的数据,所以需要在if里加上dp[i] < INT_MAX - dp[i - num]。 但java就不用考虑这个限制,java里的int也是四个字节吧,也有可能leetcode后台对不同语言的测试数据不一样。 diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index d9928b8f..913aec65 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -54,7 +54,7 @@ 当s[i]与s[j]相等时,这就复杂一些了,有如下三种情况 * 情况一:下标i 与 j相同,同一个字符例如a,当然是回文子串 -* 情况二:下标i 与 j相差为1,例如aa,也是文子串 +* 情况二:下标i 与 j相差为1,例如aa,也是回文子串 * 情况三:下标:i 与 j相差大于1的时候,例如cabac,此时s[i]与s[j]已经相同了,我们看i到j区间是不是回文子串就看aba是不是回文就可以了,那么aba的区间就是 i+1 与 j-1区间,这个区间是不是回文就看dp[i + 1][j - 1]是否为true。 以上三种情况分析完了,那么递归公式如下: @@ -178,7 +178,7 @@ public: 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 -首先确定回文串,就是找中心然后想两边扩散看是不是对称的就可以了。 +首先确定回文串,就是找中心然后向两边扩散看是不是对称的就可以了。 **在遍历中心点的时候,要注意中心点有两种情况**。 diff --git a/problems/栈与队列理论基础.md b/problems/栈与队列理论基础.md index 44fcbdd5..8c76614f 100644 --- a/problems/栈与队列理论基础.md +++ b/problems/栈与队列理论基础.md @@ -15,7 +15,7 @@ 那么我这里在列出四个关于栈的问题,大家可以思考一下。以下是以C++为例,相信使用其他编程语言的同学也对应思考一下,自己使用的编程语言里栈和队列是什么样的。 1. C++中stack 是容器么? -2. 我们使用的stack是属于那个版本的STL? +2. 我们使用的stack是属于哪个版本的STL? 3. 我们使用的STL中stack是如何实现的? 4. stack 提供迭代器来遍历stack空间么? From 907204559fb56504a0035d70cbe478996152e8d3 Mon Sep 17 00:00:00 2001 From: IcePigZDB Date: Sat, 19 Mar 2022 20:15:24 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E5=AD=90=E9=9B=86II=20cpp=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81formate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: IcePigZDB --- problems/0090.子集II.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 16bd1f2c..d08707b4 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -87,7 +87,7 @@ class Solution { private: vector> result; vector path; - void backtracking(vector& nums, int startIndex, vector& used) { + void backtracking(vector& nums, int startIndex) { result.push_back(path); unordered_set uset; for (int i = startIndex; i < nums.size(); i++) { @@ -96,7 +96,7 @@ private: } uset.insert(nums[i]); path.push_back(nums[i]); - backtracking(nums, i + 1, used); + backtracking(nums, i + 1); path.pop_back(); } } @@ -105,9 +105,8 @@ public: vector> subsetsWithDup(vector& nums) { result.clear(); path.clear(); - vector used(nums.size(), false); sort(nums.begin(), nums.end()); // 去重需要排序 - backtracking(nums, 0, used); + backtracking(nums, 0); return result; } }; From 493d6ba16a1a2359ebfe110c338818de74632476 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 20 Mar 2022 20:54:32 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E4=BC=98=E5=8C=96.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node_modules/.yarn-integrity | 10 ++++++++++ problems/0077.组合优化.md | 22 ++++++++++++++++++++++ yarn.lock | 4 ++++ 3 files changed, 36 insertions(+) create mode 100644 node_modules/.yarn-integrity create mode 100644 yarn.lock diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity new file mode 100644 index 00000000..29e4357c --- /dev/null +++ b/node_modules/.yarn-integrity @@ -0,0 +1,10 @@ +{ + "systemParams": "win32-x64-83", + "modulesFolders": [], + "flags": [], + "linkedModules": [], + "topLevelPatterns": [], + "lockfileEntries": {}, + "files": [], + "artifacts": {} +} \ No newline at end of file diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 81b4304c..94608ec1 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -240,7 +240,29 @@ var combine = function(n, k) { }; ``` +TypeScript: + +```typescript +function combine(n: number, k: number): number[][] { + let resArr: number[][] = []; + function backTracking(n: number, k: number, startIndex: number, tempArr: number[]): void { + if (tempArr.length === k) { + resArr.push(tempArr.slice()); + return; + } + for (let i = startIndex; i <= n - k + 1 + tempArr.length; i++) { + tempArr.push(i); + backTracking(n, k, i + 1, tempArr); + tempArr.pop(); + } + } + backTracking(n, k, 1, []); + return resArr; +}; +``` + C: + ```c int* path; int pathTop; diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 00000000..3ff608ae --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + From dd8bea8335794b48969ae89572aec8c7aec35231 Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Fri, 25 Mar 2022 20:23:39 -0500 Subject: [PATCH 07/13] =?UTF-8?q?0112.=E8=B7=AF=E5=BE=84=E6=80=BB=E5=92=8C?= =?UTF-8?q?=20python=20=E7=B2=BE=E7=AE=80=E9=80=92=E5=BD=92=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0112.路径总和 python 为113精简递归回溯 --- problems/0112.路径总和.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 5db36687..03ec1bb7 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -504,17 +504,13 @@ class solution: if cur_node.left: path.append(cur_node.left.val) - remain -= cur_node.left.val - traversal(cur_node.left, remain) + traversal(cur_node.left, remain-cur_node.left.val) path.pop() - remain += cur_node.left.val if cur_node.right: path.append(cur_node.right.val) - remain -= cur_node.right.val - traversal(cur_node.right, remain) + traversal(cur_node.right, remain-cur_node.left.val) path.pop() - remain += cur_node.right.val result, path = [], [] if not root: From f8392906ac8463504e2707f3762e3fc39f909782 Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Fri, 25 Mar 2022 20:25:06 -0500 Subject: [PATCH 08/13] =?UTF-8?q?0112.=E8=B7=AF=E5=BE=84=E6=80=BB=E5=92=8C?= =?UTF-8?q?=20python=20=E7=B2=BE=E7=AE=80=E9=80=92=E5=BD=92=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0112.路径总和 python 精简113递归返回条件 --- problems/0112.路径总和.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 03ec1bb7..ff682739 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -496,11 +496,10 @@ class solution: def pathsum(self, root: treenode, targetsum: int) -> list[list[int]]: def traversal(cur_node, remain): - if not cur_node.left and not cur_node.right and remain == 0: - result.append(path[:]) - return - - if not cur_node.left and not cur_node.right: return + if not cur_node.left and not cur_node.right: + if remain == 0: + result.append(path[:]) + return if cur_node.left: path.append(cur_node.left.val) From be6cb9f13086cd8ea7a82a98226b02666f10cdb3 Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Fri, 25 Mar 2022 20:35:54 -0500 Subject: [PATCH 09/13] =?UTF-8?q?0112.=E8=B7=AF=E5=BE=84=E6=80=BB=E5=92=8C?= =?UTF-8?q?=20python=20113=E6=B7=BB=E5=8A=A0=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0112.路径总和 python 113添加迭代法 --- problems/0112.路径总和.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index ff682739..1904e92b 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -519,6 +519,30 @@ class solution: return result ``` +**迭代法,用第二个队列保存目前的总和与路径** +```python +class Solution: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: + if not root: + return [] + que, temp = deque([root]), deque([(root.val, [root.val])]) + result = [] + while que: + for _ in range(len(que)): + node = que.popleft() + value, path = temp.popleft() + if (not node.left) and (not node.right): + if value == targetSum: + result.append(path) + if node.left: + que.append(node.left) + temp.append((node.left.val+value, path+[node.left.val])) + if node.right: + que.append(node.right) + temp.append((node.right.val+value, path+[node.right.val])) + return result +``` + ## go 112. 路径总和 From 9b9a37b92c20fc616b7808317ba05ff03c6d8633 Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Sat, 2 Apr 2022 00:03:08 -0500 Subject: [PATCH 10/13] 0455 typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0455思路多打了一个“了” --- problems/0455.分发饼干.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 210b492d..5c86e478 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -32,7 +32,7 @@ ## 思路 -为了了满足更多的小孩,就不要造成饼干尺寸的浪费。 +为了满足更多的小孩,就不要造成饼干尺寸的浪费。 大尺寸的饼干既可以满足胃口大的孩子也可以满足胃口小的孩子,那么就应该优先满足胃口大的。 From 69a9316bebce0fc3f43835bb9245f124076f105f Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Sun, 3 Apr 2022 23:42:04 -0500 Subject: [PATCH 11/13] =?UTF-8?q?python=200860.=E6=9F=A0=E6=AA=AC=E6=B0=B4?= =?UTF-8?q?=E6=89=BE=E9=9B=B6=20=E5=8E=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python 0860.柠檬水找零 去除无用变量:在本题中,20的数量是无用的,因为不需要用来找零,可以不用维护这个变量。 --- problems/0860.柠檬水找零.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md index f48ecf4d..ffd5490d 100644 --- a/problems/0860.柠檬水找零.md +++ b/problems/0860.柠檬水找零.md @@ -157,7 +157,7 @@ class Solution { ```python class Solution: def lemonadeChange(self, bills: List[int]) -> bool: - five, ten, twenty = 0, 0, 0 + five, ten = 0, 0 for bill in bills: if bill == 5: five += 1 @@ -169,10 +169,8 @@ class Solution: if ten > 0 and five > 0: ten -= 1 five -= 1 - twenty += 1 elif five > 2: five -= 3 - twenty += 1 else: return False return True From c590dfc5f29e910a85798f144bcc7178dda2bde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Thu, 7 Apr 2022 09:34:25 +0800 Subject: [PATCH 12/13] Delete yarn.lock --- yarn.lock | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 yarn.lock diff --git a/yarn.lock b/yarn.lock deleted file mode 100644 index 3ff608ae..00000000 --- a/yarn.lock +++ /dev/null @@ -1,4 +0,0 @@ -# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -# yarn lockfile v1 - - From bbe312015eca5207064aa42f3530e254845a1e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98Carl?= Date: Thu, 7 Apr 2022 09:34:51 +0800 Subject: [PATCH 13/13] Delete .yarn-integrity --- node_modules/.yarn-integrity | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 node_modules/.yarn-integrity diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity deleted file mode 100644 index 29e4357c..00000000 --- a/node_modules/.yarn-integrity +++ /dev/null @@ -1,10 +0,0 @@ -{ - "systemParams": "win32-x64-83", - "modulesFolders": [], - "flags": [], - "linkedModules": [], - "topLevelPatterns": [], - "lockfileEntries": {}, - "files": [], - "artifacts": {} -} \ No newline at end of file