From a90db6e830474a70dd445a77cef578627374ca5e Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Thu, 17 Mar 2022 13:38:45 -0500 Subject: [PATCH 1/7] =?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 从原方法更新到双指针方法 --- ...61\350\204\211\346\225\260\347\273\204.md" | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index a2444cc145..4b7a978c0e 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.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 2/7] =?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 更新优化方法(只使用一个队列) --- ...27\345\256\236\347\216\260\346\240\210.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index c45917db4d..3457c4b368 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.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 dd8bea8335794b48969ae89572aec8c7aec35231 Mon Sep 17 00:00:00 2001 From: SianXiaoCHN Date: Fri, 25 Mar 2022 20:23:39 -0500 Subject: [PATCH 3/7] =?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精简递归回溯 --- ...2.\350\267\257\345\276\204\346\200\273\345\222\214.md" | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 5db36687a9..03ec1bb7b1 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.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 4/7] =?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递归返回条件 --- ....\350\267\257\345\276\204\346\200\273\345\222\214.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 03ec1bb7b1..ff68273924 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.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 5/7] =?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添加迭代法 --- ...57\345\276\204\346\200\273\345\222\214.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index ff68273924..1904e92bd7 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.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 6/7] 0455 typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0455思路多打了一个“了” --- .../0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index 210b492df6..5c86e4785b 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.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 7/7] =?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的数量是无用的,因为不需要用来找零,可以不用维护这个变量。 --- ...37\240\346\252\254\346\260\264\346\211\276\351\233\266.md" | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index f48ecf4d60..ffd5490d6d 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.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