From fe0d0f8c5be965b69f2db0c966c2a8b7135e9f35 Mon Sep 17 00:00:00 2001 From: hbingzhi Date: Tue, 3 Jan 2023 17:46:01 +0800 Subject: [PATCH 01/33] =?UTF-8?q?232.=E4=BF=AE=E6=94=B9=E6=96=87=E5=AD=97?= =?UTF-8?q?=E5=8F=99=E8=BF=B0=E9=94=99=E8=AF=AF=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 4983c93a64..efeb10463e 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -38,7 +38,7 @@ queue.empty(); // 返回 false ## 思路 -《代码随想录》算法公开课:[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对链表的理解。 +《代码随想录》算法公开课:[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对栈和队列的理解。 这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。 @@ -662,3 +662,4 @@ impl MyQueue { + From 256ca509ab75b5108a3c315ef6449431b0921def Mon Sep 17 00:00:00 2001 From: Zeeland Date: Fri, 6 Jan 2023 17:23:51 +0800 Subject: [PATCH 02/33] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96Python?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E9=AB=98=E4=BA=AE=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0042.\346\216\245\351\233\250\346\260\264.md" | 4 ++-- ...32\204\346\211\200\346\234\211\350\267\257\345\276\204.md" | 4 ++-- ...36\204\345\273\272\344\272\214\345\217\211\346\240\221.md" | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index e8dd3690ba..ac6f20f957 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -471,7 +471,7 @@ class Solution { ### Python: 双指针法 -```python3 +```Python class Solution: def trap(self, height: List[int]) -> int: res = 0 @@ -510,7 +510,7 @@ class Solution: return result ``` 单调栈 -```python3 +```Python class Solution: def trap(self, height: List[int]) -> int: # 单调栈 diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 2d7966715d..d0c190a0f6 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -468,7 +468,7 @@ class Solution { --- ## Python: 递归法+隐形回溯 -```Python3 +```Python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): @@ -499,7 +499,7 @@ class Solution: 迭代法: -```python3 +```Python from collections import deque diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" index 01d5b25519..42bf7af0ba 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" @@ -280,7 +280,7 @@ public class Solution { ## Python -```Python3 +```Python class TreeNode: def __init__(self, val = 0, left = None, right = None): self.val = val From 9b07639364f2586815b10fcec5cf209b169241a8 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sat, 7 Jan 2023 14:08:19 -0600 Subject: [PATCH 03/33] fix minor code style --- ...\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index a12d8f76bd..dc12ae2336 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -442,7 +442,7 @@ class Solution: while left <= right and s[left] == ' ': #去除开头的空格 left += 1 while left <= right and s[right] == ' ': #去除结尾的空格 - right = right-1 + right -= 1 tmp = [] while left <= right: #去除单词中间多余的空格 if s[left] != ' ': From 2d2babdaf0ce7ceb3f61c26c7e959ed2776bda62 Mon Sep 17 00:00:00 2001 From: hbingzhi Date: Sun, 8 Jan 2023 12:58:54 +0800 Subject: [PATCH 04/33] =?UTF-8?q?349.=E6=9C=80=E5=90=8E=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=B7=BB=E5=8A=A0=E5=8F=A6=E4=B8=80=E7=A7=8D?= =?UTF-8?q?=E5=86=99=E6=B3=95=EF=BC=8C=E6=9B=B4=E5=A4=9A=E4=BA=BA=E5=AE=B9?= =?UTF-8?q?=E6=98=93=E7=90=86=E8=A7=A3=E7=9C=8B=E6=87=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\273\204\347\232\204\344\272\244\351\233\206.md" | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 2e98ef6f22..6f5a34e796 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -137,8 +137,18 @@ class Solution { resSet.add(i); } } - //将结果几何转为数组 + + //方法1:直接将结果几何转为数组 return resSet.stream().mapToInt(x -> x).toArray(); + + //方法2:另外申请一个数组存放setRes中的元素,最后返回数组 + int[] arr = new int[setRes.size()]; + int j = 0; + for(int i : setRes){ + arr[j++] = i; + } + + return arr; } } ``` @@ -423,3 +433,4 @@ C#: + From a008a740694b4fd99a307dab9e05a1f9276605f2 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 8 Jan 2023 08:21:42 -0600 Subject: [PATCH 05/33] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/\345\211\215\345\272\217/vim.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/vim.md" "b/problems/\345\211\215\345\272\217/vim.md" index f84e70aca7..5c5910d206 100644 --- "a/problems/\345\211\215\345\272\217/vim.md" +++ "b/problems/\345\211\215\345\272\217/vim.md" @@ -66,7 +66,7 @@ IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开 ## 安装 -PowerVim的安防非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。 +PowerVim的安装非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。 安装过程非常简单: ```bash From aad6b8cee27afa237dca3997fa720705410eac7a Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 8 Jan 2023 16:37:47 -0600 Subject: [PATCH 06/33] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BA=86python=20versi?= =?UTF-8?q?on=E7=9A=84=E9=80=BB=E8=BE=91=E3=80=82=E5=81=9A=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=8B=E7=AE=80=E5=8C=96?= 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" | 2 +- ...72\347\264\257\345\212\240\346\240\221.md" | 29 +++++++++---------- 2 files changed, 15 insertions(+), 16 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 406242a786..2842528191 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" @@ -23,7 +23,7 @@ * 111.二叉树的最小深度 -![我要打十个](https://tva1.sinaimg.cn/large/008eGmZEly1gnadnltbpjg309603w4qp.gif) +![我要打十个](https://tva1.sinaimg.cn/large/008eGmZEly1gPnadnltbpjg309603w4qp.gif) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index ec12c525c8..d2e98dc82b 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -209,29 +209,28 @@ class Solution { # self.right = right class Solution: def __init__(self): - self.pre = TreeNode() + self.count = 0 def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root == None: + return ''' 倒序累加替换: - [2, 5, 13] -> [[2]+[1]+[0], [2]+[1], [2]] -> [20, 18, 13] ''' - self.traversal(root) - return root - - def traversal(self, root: TreeNode) -> None: - # 因为要遍历整棵树,所以递归函数不需要返回值 - # Base Case - if not root: - return None - # 单层递归逻辑:中序遍历的反译 - 右中左 - self.traversal(root.right) # 右 + # 右 + self.convertBST(root.right) + # 中 # 中节点:用当前root的值加上pre的值 - root.val += self.pre.val # 中 - self.pre = root + self.count += root.val + + root.val = self.count + + # 左 + self.convertBST(root.left) - self.traversal(root.left) # 左 + return root + ``` ## Go From 67b7c7f2b802feb27acb5a33221faa96bc8f03c2 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 9 Jan 2023 17:27:48 -0500 Subject: [PATCH 07/33] =?UTF-8?q?Update=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入了bilibili视频链接 --- ...0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 97178cd5b1..124b365fbf 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -40,6 +40,9 @@ * 0 <= s.length <= 3000 * s 仅由数字组成 +# 算法公开课 + +**《代码随想录》算法视频公开课:[93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 # 思路 From 5bd02484387de4c317cfeb114cf1de6b04464aca Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 9 Jan 2023 18:10:32 -0500 Subject: [PATCH 08/33] =?UTF-8?q?Update=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入了python3,更类似于LeetCode131题代码的写法,看起来更简洁些 --- ...\345\216\237IP\345\234\260\345\235\200.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 97178cd5b1..4e32af994c 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -424,6 +424,30 @@ class Solution: return True ``` +python3; 简单拼接版本(类似Leetcode131写法): +```python +class Solution: + def restoreIpAddresses(self, s: str) -> List[str]: + global results, path + results = [] + path = [] + self.backtracking(s,0) + return results + + def backtracking(self,s,index): + global results,path + if index == len(s) and len(path)==4: + results.append('.'.join(path)) # 在连接时需要中间间隔符号的话就在''中间写上对应的间隔符 + return + for i in range(index,len(s)): + if len(path)>3: break # 剪枝 + temp = s[index:i+1] + if (int(temp)<256 and int(temp)>0 and temp[0]!='0') or (temp=='0'): + path.append(temp) + self.backtracking(s,i+1) + path.pop() +``` + ## Go ```go From 4856f9786b82c9ca54f2759679e1ef7c9e7a34b0 Mon Sep 17 00:00:00 2001 From: PeixiZ <96801981+PeixiZ@users.noreply.github.com> Date: Tue, 10 Jan 2023 15:49:23 +0800 Subject: [PATCH 09/33] =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=E4=B8=89=E6=8C=87=E9=92=88=E6=84=9F?= =?UTF-8?q?=E8=A7=89=E4=BC=9A=E6=9B=B4=E5=BD=A2=E8=B1=A1=E8=A1=A8=E8=BF=B0?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=BF=BB=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\350\275\254\351\223\276\350\241\250.md" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 44146bb4fd..7db80fe1e3 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -628,6 +628,43 @@ impl Solution { } } ``` +C#: +三指针法, 感觉会更直观: + +```cs +public LinkNumbers Reverse() +{ + ///用三指针,写的过程中能够弥补二指针在翻转过程中的想象 + LinkNumbers pre = null; + var move = root; + var next = root; + + while (next != null) + { + next = next.linknext; + move.linknext = pre; + pre = move; + move = next; + } + root = pre; + return root; +} + +///LinkNumbers的定义 +public class LinkNumbers +{ + /// + /// 链表值 + /// + public int value { get; set; } + + /// + /// 链表指针 + /// + public LinkNumbers linknext { get; set; } +} +``` +

From d010b09dbd6c65fea927dabf39d10045a5a741da Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Tue, 10 Jan 2023 15:18:11 -0700 Subject: [PATCH 10/33] =?UTF-8?q?Update=200031.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python版本剪枝去重,避免对递减序列的比较 --- ...270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" index 88fbb2fc65..34aa1086c0 100644 --- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" +++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" @@ -168,7 +168,8 @@ class Solution: Do not return anything, modify nums in-place instead. """ length = len(nums) - for i in range(length - 1, -1, -1): + for i in range(length - 2, -1, -1): # 从倒数第二个开始 + if nums[i]>=nums[i+1]: continue # 剪枝去重 for j in range(length - 1, i, -1): if nums[j] > nums[i]: nums[j], nums[i] = nums[i], nums[j] From 241f3af9a8c74a5a42f53e9f39689fd1ab501655 Mon Sep 17 00:00:00 2001 From: shangcode <61669790+shangcode@users.noreply.github.com> Date: Wed, 11 Jan 2023 09:25:03 +0800 Subject: [PATCH 11/33] =?UTF-8?q?Update=200034.=E5=9C=A8=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 去掉了一个 Python 解法中的分号。 --- ...20\216\344\270\200\344\270\252\344\275\215\347\275\256.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" index c7ff6dce1e..7e58a870d8 100644 --- "a/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" +++ "b/problems/0034.\345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\345\205\203\347\264\240\347\232\204\347\254\254\344\270\200\344\270\252\345\222\214\346\234\200\345\220\216\344\270\200\344\270\252\344\275\215\347\275\256.md" @@ -355,8 +355,8 @@ class Solution: while left <= right: middle = left + (right-left) // 2 if nums[middle] >= target: # 寻找左边界,nums[middle] == target的时候更新right - right = middle - 1; - leftBoder = right; + right = middle - 1 + leftBoder = right else: left = middle + 1 return leftBoder From a8339121e6f98e9b5241169aef93a04b7ca2ad87 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:08:06 -0600 Subject: [PATCH 12/33] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 eec471af34..0a85d04a95 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" @@ -148,7 +148,7 @@ class Solution { ### Python ```python class Solution: - # 思路1:优先考虑胃饼干 + # 思路1:优先考虑小胃口 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() @@ -160,7 +160,7 @@ class Solution: ``` ```python class Solution: - # 思路2:优先考虑胃口 + # 思路2:优先考虑大胃口 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() From 91f3da4b43f44d60a0f7c60436ec30869788ec04 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:53:05 -0600 Subject: [PATCH 13/33] add Python solution --- ...40\344\272\214\345\217\211\346\240\221.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index f8109f853e..c2b2872b6c 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -397,6 +397,9 @@ public: }; ``` +## Python + + # 105.从前序与中序遍历序列构造二叉树 [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) @@ -650,6 +653,37 @@ class Solution { ``` ## Python +```python +class Solution: + def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: + # 第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件 + if not postorder: + return + + # 第二步: 后序遍历的最后一个就是当前的中间节点 + root_val = postorder[-1] + root = TreeNode(root_val) + + # 第三步: 找切割点. + root_index = inorder.index(root_val) + + # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. + left_inorder = inorder[:root_index] + right_inorder = inorder[root_index + 1:] + + # 第五步: 切割postorder数组. 得到postorder数组的左,右半边. + # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. + left_postorder = postorder[:len(left_inorder)] + right_postorder = postorder[len(left_inorder): len(postorder) - 1] + + + # 第六步: 递归 + root.left = self.buildTree(left_inorder, left_postorder) + root.right = self.buildTree(right_inorder, right_postorder) + + # 第七步: 返回答案 + return root +``` 105.从前序与中序遍历序列构造二叉树 From 7d822fe65a587f883a5d63c1b72c139239f2cff7 Mon Sep 17 00:00:00 2001 From: El nino <69737612+el-nino2020@users.noreply.github.com> Date: Sat, 14 Jan 2023 09:18:08 +0800 Subject: [PATCH 14/33] =?UTF-8?q?Update=2020201003=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typo? --- ...\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index 18bbf37f97..5f59b04022 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -2,7 +2,7 @@ 本周赶上了十一国庆,估计大家已经对本周末没什么概念了,但是我们该做总结还是要做总结的。 -本周的主题其实是**简单但并不简单**,本周所选的题目大多是看一下就会的题目,但是大家看完本周的文章估计也发现了,二叉树的简答题目其实里面都藏了很多细节。 这些细节我都给大家展现了出来。 +本周的主题其实是**简单但并不简单**,本周所选的题目大多是看一下就会的题目,但是大家看完本周的文章估计也发现了,二叉树的简单题目其实里面都藏了很多细节。 这些细节我都给大家展现了出来。 ## 周一 From c201b947a5bb4510b5c0214ec652c6300a2c6760 Mon Sep 17 00:00:00 2001 From: El nino <69737612+el-nino2020@users.noreply.github.com> Date: Sat, 14 Jan 2023 15:06:56 +0800 Subject: [PATCH 15/33] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原有的代码leetcode上无法通过编译 --- ...112.\350\267\257\345\276\204\346\200\273\345\222\214.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 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 7aa5f2a165..b1f0133663 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" @@ -155,14 +155,14 @@ public: 以上代码精简之后如下: ```cpp -class solution { +class Solution { public: bool hasPathSum(TreeNode* root, int sum) { - if (root == null) return false; + if (!root) return false; if (!root->left && !root->right && sum == root->val) { return true; } - return haspathsum(root->left, sum - root->val) || haspathsum(root->right, sum - root->val); + return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); } }; ``` From 1a44d7e7dfd032cf13a42ee4998b6445077c5ebb Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sun, 15 Jan 2023 16:34:26 -0600 Subject: [PATCH 16/33] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0134.\345\212\240\346\262\271\347\253\231.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index ade847730c..192e58a684 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -88,7 +88,7 @@ public: * 情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的 * 情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。 -* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。 +* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能把这个负数填平,能把这个负数填平的节点就是出发节点。 C++代码如下: From 1d19ac72300e6de7c785b4eb5f64f8eea3d5e4b6 Mon Sep 17 00:00:00 2001 From: Zeeland Date: Mon, 16 Jan 2023 10:48:43 +0800 Subject: [PATCH 17/33] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E9=94=99?= =?UTF-8?q?=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index 2e98ef6f22..f8f41097bb 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -137,7 +137,7 @@ class Solution { resSet.add(i); } } - //将结果几何转为数组 + //将结果集合转为数组 return resSet.stream().mapToInt(x -> x).toArray(); } } From 835c9222deae3ffef30cbe4cf66bb536f7d9dd90 Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Wed, 18 Jan 2023 18:14:38 -0500 Subject: [PATCH 18/33] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8=E5=B9=B3?= =?UTF-8?q?=E6=96=B9=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 部分,版本2,第233行少写了一个“:” --- ...\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b6a97be3ff..53f6b6d084 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" @@ -230,7 +230,7 @@ class Solution: # 遍历物品 for num in nums: # 遍历背包 - for j in range(num, n + 1) + for j in range(num, n + 1): dp[j] = min(dp[j], dp[j - num] + 1) return dp[n] ``` From a11c78a19d95f7091681651e071f87fb169009e3 Mon Sep 17 00:00:00 2001 From: SUNLIFAN Date: Thu, 19 Jan 2023 09:58:56 +0800 Subject: [PATCH 19/33] =?UTF-8?q?=E6=B7=BB=E5=8A=A00107=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=86II=20?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E5=8F=8D=E8=BD=AC=E7=AD=94=E6=A1=88?= =?UTF-8?q?=E7=9A=84=20Java=20=E7=89=88=E6=9C=AC?= 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" | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 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 220232a2b1..21f5147dd3 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" @@ -562,6 +562,45 @@ public class N0107 { } ``` +```java +/** + * 思路和模板相同, 对收集答案的方式做了优化, 最后不需要反转 + */ +class Solution { + public List> levelOrderBottom(TreeNode root) { + // 利用链表可以进行 O(1) 头部插入, 这样最后答案不需要再反转 + LinkedList> ans = new LinkedList<>(); + + Queue q = new LinkedList<>(); + + if (root != null) q.offer(root); + + while (!q.isEmpty()) { + int size = q.size(); + + List temp = new ArrayList<>(); + + for (int i = 0; i < size; i ++) { + TreeNode node = q.poll(); + + temp.add(node.val); + + if (node.left != null) q.offer(node.left); + + if (node.right != null) q.offer(node.right); + } + + // 新遍历到的层插到头部, 这样就满足按照层次反序的要求 + ans.addFirst(temp); + } + + return ans; + } +} +``` + + + go: ```GO @@ -3013,4 +3052,3 @@ impl Solution { - From 5bf54160131703bff0029272f98626d8717bf41f Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:40:15 +0800 Subject: [PATCH 20/33] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md=20=E6=B7=BB=E5=8A=A0=E6=99=AE=E9=80=9A=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=88=A0=E9=99=A4=E6=96=B9=E5=BC=8F?= =?UTF-8?q?python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...255\347\232\204\350\212\202\347\202\271.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index facdb2f938..0655f8c5d0 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -348,6 +348,24 @@ class Solution: return root ``` +**普通二叉树的删除方式** +```python +class Solution: + def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: + if not root: return root + if root.val == key: + if not root.right: # 这里第二次操作目标值:最终删除的作用 + return root.left + tmp = root.right + while tmp.left: + tmp = tmp.left + root.val, tmp.val = tmp.val, root.val # 这里第一次操作目标值:交换目标值其右子树最左面节点。 + + root.left = self.deleteNode(root.left, key) + root.right = self.deleteNode(root.right, key) + return root +``` + **迭代法** ```python class Solution: From cbc75c008452a9ac0de0b580a30a4a1d9ec969b5 Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:47:41 +0800 Subject: [PATCH 21/33] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index ac36509f21..1fd6fce098 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -299,6 +299,32 @@ class Solution: return root ``` +**迭代** +```python +class Solution: + def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: + if not root: return root + # 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开 + while root and (root.val < low or root.val > high): + if root.val < low: # 小于L往右走 + root = root.right + else: # 大于R往左走 + root = root.left + # 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况 + cur = root + while cur: + while cur.left and cur.left.val < low: + cur.left = cur.left.right + cur = cur.left + # 此时root已经在[L, R] 范围内,处理右孩子大于R的情况 + cur = root + while cur: + while cur.right and cur.right.val > high: + cur.right = cur.right.left + cur = cur.right + return root +``` + ## Go ```go From f96384951fb35dc196beea26151ab992a551b08e Mon Sep 17 00:00:00 2001 From: Zehua Ren <48848908+Renzehua1998@users.noreply.github.com> Date: Thu, 19 Jan 2023 18:00:55 +0800 Subject: [PATCH 22/33] =?UTF-8?q?Update=200108.=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91.md=20=E6=B7=BB=E5=8A=A0=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index c75f87d1e0..f0455a834f 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -352,6 +352,38 @@ class Solution: return mid_root ``` +**迭代**(左闭右开) +```python +class Solution: + def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: + if len(nums) == 0: return None + root = TreeNode() # 初始化 + nodeSt = [root] + leftSt = [0] + rightSt = [len(nums)] + + while nodeSt: + node = nodeSt.pop() # 处理根节点 + left = leftSt.pop() + right = rightSt.pop() + mid = left + (right - left) // 2 + node.val = nums[mid] + + if left < mid: # 处理左区间 + node.left = TreeNode() + nodeSt.append(node.left) + leftSt.append(left) + rightSt.append(mid) + + if right > mid + 1: # 处理右区间 + node.right = TreeNode() + nodeSt.append(node.right) + leftSt.append(mid + 1) + rightSt.append(right) + + return root +``` + ## Go 递归(隐含回溯) From 63183226bf48024e7669abd431ac746f6221321f Mon Sep 17 00:00:00 2001 From: Zeeland Date: Thu, 19 Jan 2023 23:56:57 +0800 Subject: [PATCH 23/33] =?UTF-8?q?Update=20=E6=B7=BB=E5=8A=A0=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?Python=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\347\273\204\345\220\210.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 4f39f60a70..df038806a5 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" @@ -354,6 +354,28 @@ class Solution: for letter in letters: self.backtracking(digits, index + 1, answer + letter) # 递归至下一层 + 回溯 ``` +**使用itertools** +```python +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + import itertools + if not digits: + return list() + + phoneMap = { + "2": "abc", + "3": "def", + "4": "ghi", + "5": "jkl", + "6": "mno", + "7": "pqrs", + "8": "tuv", + "9": "wxyz", + } + + groups = (phoneMap[digit] for digit in digits) + return ["".join(combination) for combination in itertools.product(*groups)] +``` ## Go From b355d79c468998be184d69f8ef519430f4936563 Mon Sep 17 00:00:00 2001 From: El nino <69737612+el-nino2020@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:04:39 +0800 Subject: [PATCH 24/33] =?UTF-8?q?Update=2020201126=E8=B4=AA=E5=BF=83?= =?UTF-8?q?=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typo --- ...77\203\345\221\250\346\234\253\346\200\273\347\273\223.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" index e310c0f8e0..1d5ca6afb6 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201126\350\264\252\345\277\203\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -18,7 +18,7 @@ 数学就不在讲解范围内了,感兴趣的同学可以自己去查一查资料。 -正式因为贪心算法有时候会感觉这是常识,本就应该这么做! 所以大家经常看到网上有人说这是一道贪心题目,有人是这不是。 +正是因为贪心算法有时候会感觉这是常识,本就应该这么做! 所以大家经常看到网上有人说这是一道贪心题目,有人说这不是。 这里说一下我的依据:**如果找到局部最优,然后推出整体最优,那么就是贪心**,大家可以参考哈。 @@ -37,7 +37,7 @@ **因为用小饼干优先喂饱小胃口的 这样可以尽量保证最后省下来的是大饼干(虽然题目没有这个要求)!** -所有还是小饼干优先先喂饱小胃口更好一些,也比较直观。 +所以还是小饼干优先先喂饱小胃口更好一些,也比较直观。 一些录友不清楚[贪心算法:分发饼干](https://programmercarl.com/0455.分发饼干.html)中时间复杂度是怎么来的? From 026f8ca7b4c189dac475ea2b8f93923c8abfed89 Mon Sep 17 00:00:00 2001 From: hbingzhi Date: Sat, 21 Jan 2023 17:25:14 +0800 Subject: [PATCH 25/33] =?UTF-8?q?257.=E7=BB=99Java=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=85=A8=E9=9D=A2=E6=B8=85=E6=99=B0?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E9=87=8A=EF=BC=8C=E5=8F=A6=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=80=E5=A4=84=E9=87=8D=E5=A4=8D=E7=9A=84=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\234\211\350\267\257\345\276\204.md" | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index d0c190a0f6..68434479f5 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -34,7 +34,7 @@ ## 递归 -1. 递归函数函数参数以及返回值 +1. 递归函数参数以及返回值 要传入根节点,记录每一条路径的path,和存放结果集的result,这里递归不需要返回值,代码如下: @@ -259,7 +259,7 @@ if (cur->right) { path.pop_back(); // 回溯 '>' path.pop_back(); // 回溯 '-' } -``` +``` 整体代码如下: @@ -395,33 +395,34 @@ class Solution { * 递归法 */ public List binaryTreePaths(TreeNode root) { - List res = new ArrayList<>(); + List res = new ArrayList<>();// 存最终的结果 if (root == null) { return res; } - List paths = new ArrayList<>(); + List paths = new ArrayList<>();// 作为结果中的路径 traversal(root, paths, res); return res; } private void traversal(TreeNode root, List paths, List res) { - paths.add(root.val); - // 叶子结点 + paths.add(root.val);// 前序遍历,中 + // 遇到叶子结点 if (root.left == null && root.right == null) { // 输出 - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder();// StringBuilder用来拼接字符串,速度更快 for (int i = 0; i < paths.size() - 1; i++) { sb.append(paths.get(i)).append("->"); } - sb.append(paths.get(paths.size() - 1)); - res.add(sb.toString()); + sb.append(paths.get(paths.size() - 1));// 记录最后一个节点 + res.add(sb.toString());// 收集一个路径 return; } - if (root.left != null) { + // 递归和回溯是同时进行,所以要放在同一个花括号里 + if (root.left != null) { // 左 traversal(root.left, paths, res); paths.remove(paths.size() - 1);// 回溯 } - if (root.right != null) { + if (root.right != null) { // 右 traversal(root.right, paths, res); paths.remove(paths.size() - 1);// 回溯 } @@ -794,7 +795,7 @@ object Solution { } } ``` - + rust: ```rust @@ -826,3 +827,4 @@ impl Solution { + From aeb80ae90a24ecd67593867b20507c07dd7dab50 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Sat, 21 Jan 2023 13:22:09 -0600 Subject: [PATCH 26/33] dp python --- ...42\351\202\243\345\245\221\346\225\260.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 1fa5ca1838..69306801f5 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -214,6 +214,33 @@ class Solution: a, b = b, c return c +# 动态规划 (注释版。无修饰) +class Solution: + def fib(self, n: int) -> int: + + # 排除 Corner Case + if n == 1: + return 1 + + if n == 0: + return 0 + + # 创建 dp table + dp = [0] * (n + 1) + + # 初始化 dp 数组 + dp[0] = 0 + dp[1] = 1 + + # 遍历顺序: 由前向后。因为后面要用到前面的状态 + for i in range(2, n + 1): + + # 确定递归公式/状态转移公式 + dp[i] = dp[i - 1] + dp[i - 2] + + # 返回答案 + return dp[n] + # 递归实现 class Solution: def fib(self, n: int) -> int: From 1174602e9170d0615b631a0e452d576c6af825a0 Mon Sep 17 00:00:00 2001 From: El nino <69737612+el-nino2020@users.noreply.github.com> Date: Sun, 22 Jan 2023 13:37:36 +0800 Subject: [PATCH 27/33] =?UTF-8?q?Update=200714.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E6=89=8B=E7=BB=AD=E8=B4=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正注释中的用词错误 --- ...\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index 005f9c3829..3f44ec1764 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -182,7 +182,7 @@ class Solution { // 动态规划 int[][] dp = new int[prices.length][2]; - // bad case + // base case dp[0][0] = 0; dp[0][1] = -prices[0]; From 51d11d8e81b9217131d668ad940312614c898d46 Mon Sep 17 00:00:00 2001 From: Li Yuxuan <77979684+liyuxuan7762@users.noreply.github.com> Date: Mon, 23 Jan 2023 12:49:42 +0800 Subject: [PATCH 28/33] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E5=8F=8D=E8=BD=AC=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新使用栈实现反转链表 --- ...73\350\275\254\351\223\276\350\241\250.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index 7db80fe1e3..4920b5b3be 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -665,6 +665,42 @@ public class LinkNumbers } ``` + + +## 使用栈解决反转链表的问题 +* 首先将所有的结点入栈 +* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 + +```java +public ListNode reverseList(ListNode head) { + // 如果链表为空,则返回空 + if (head == null) return null; + // 如果链表中只有只有一个元素,则直接返回 + if (head.next == null) return head; + // 创建栈 每一个结点都入栈 + Stack stack = new Stack<>(); + ListNode cur = head; + while (cur != null) { + stack.push(cur); + cur = cur.next; + } + // 创建一个虚拟头结点 + ListNode pHead = new ListNode(0); + cur = pHead; + while (!stack.isEmpty()) { + ListNode node = stack.pop(); + cur.next = node; + cur = cur.next; + } + // 最后一个元素的next要赋值为空 + cur.next = null; + return pHead.next; +} +``` + +> 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` +![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) +

From 88750b119003658587b9e03ff3b6f0e525d665c2 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Mon, 23 Jan 2023 17:43:15 -0600 Subject: [PATCH 29/33] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8C=E7=BB=B4?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95=20+=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\345\222\214\345\255\220\351\233\206.md" | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index ecee1d83fe..936ba27055 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -291,18 +291,63 @@ false true false false false true true false false false true true ### Python: ```python +# 一维度数组解法 class Solution: def canPartition(self, nums: List[int]) -> bool: target = sum(nums) if target % 2 == 1: return False target //= 2 - dp = [0] * 10001 + dp = [0] * (len(nums) + 1) for i in range(len(nums)): for j in range(target, nums[i] - 1, -1): dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) return target == dp[target] ``` +```python +# 二维度数组解法 +class Solution: + def canPartition(self, nums: List[int]) -> bool: + target = sum(nums) + nums = sorted(nums) + + # 做最初的判断 + if target % 2 != 0: + return False + + # 找到 target value 可以认为这个是背包的体积 + target = target // 2 + + row = len(nums) + col = target + 1 + + # 定义 dp table + dp = [[0 for _ in range(col)] for _ in range(row)] + + # 初始 dp value + for i in range(row): + dp[i][0] = 0 + + for j in range(1, target): + if nums[0] <= j: + dp[0][j] = nums[0] + + # 遍历 先遍历物品再遍历背包 + for i in range(1, row): + + cur_weight = nums[i] + cur_value = nums[i] + + for j in range(1, col): + if cur_weight > j: + dp[i][j] = dp[i - 1][j] + else: + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight] + cur_value) + + # 输出结果 + return dp[-1][col - 1] == target +``` + ### Go: ```go // 分割等和子集 动态规划 From 0df3cb05501a0fc9cbc7e3867ccba4e6f5ba33f4 Mon Sep 17 00:00:00 2001 From: MWang <48111863+mouWorks@users.noreply.github.com> Date: Wed, 25 Jan 2023 12:35:45 +0800 Subject: [PATCH 30/33] Fix : TypeScript answer * Fixing TypeScript traversal answers - preoder / inorder / postorder has exactly same code, fixed. --- ...\237\344\270\200\350\277\255\344\273\243\346\263\225.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\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index c44bbb9937..274642696c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -536,9 +536,9 @@ function preorderTraversal(root: TreeNode | null): number[] { curNode = helperStack.pop()!; if (curNode !== null) { if (curNode.right !== null) helperStack.push(curNode.right); + if (curNode.left !== null) helperStack.push(curNode.left); helperStack.push(curNode); helperStack.push(null); - if (curNode.left !== null) helperStack.push(curNode.left); } else { curNode = helperStack.pop()!; res.push(curNode.val); @@ -579,9 +579,9 @@ function postorderTraversal(root: TreeNode | null): number[] { while (helperStack.length > 0) { curNode = helperStack.pop()!; if (curNode !== null) { - if (curNode.right !== null) helperStack.push(curNode.right); - helperStack.push(curNode); + helperStack.push(curNode); helperStack.push(null); + if (curNode.right !== null) helperStack.push(curNode.right); if (curNode.left !== null) helperStack.push(curNode.left); } else { curNode = helperStack.pop()!; From 27ba94b581236d83b1778f93778f2ae57bc7e874 Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Wed, 25 Jan 2023 12:46:13 -0600 Subject: [PATCH 31/33] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...37\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index bf4e708abb..3b2c33c125 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -224,7 +224,7 @@ class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: sumweight = sum(stones) target = sumweight // 2 - dp = [0] * 15001 + dp = [0] * (target + 1) for i in range(len(stones)): for j in range(target, stones[i] - 1, -1): dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]) From 31e6a1ad95de2a4652796d4367b84655d3653008 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 25 Jan 2023 20:10:53 +0000 Subject: [PATCH 32/33] =?UTF-8?q?=E6=B7=BB=E5=8A=A00062.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84.md=20C=E8=AF=AD=E8=A8=80=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\220\214\350\267\257\345\276\204.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 2367587c25..4c0e227993 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -436,6 +436,26 @@ int uniquePaths(int m, int n){ } ``` +滚动数组解法: +```c +int uniquePaths(int m, int n){ + int i, j; + + // 初始化dp数组 + int *dp = (int*)malloc(sizeof(int) * n); + for (i = 0; i < n; ++i) + dp[i] = 1; + + for (j = 1; j < m; ++j) { + for (i = 1; i < n; ++i) { + // dp[i]为二维数组解法中dp[i-1][j]。dp[i-1]为二维数组解法中dp[i][j-1] + dp[i] += dp[i - 1]; + } + } + return dp[n - 1]; +} +``` + ### Scala ```scala From 9d02c76777849bcf287cc3c11805fb8b98db8ce4 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Thu, 26 Jan 2023 17:24:38 +0000 Subject: [PATCH 33/33] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md=20C=E8=AF=AD=E8=A8=80=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=A9=BA=E9=97=B4=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\214\350\267\257\345\276\204II.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) 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 9aa369563d..b89486e655 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" @@ -539,6 +539,39 @@ int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obst } ``` +空间优化版本: +```c +int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obstacleGridColSize){ + int m = obstacleGridSize; + int n = obstacleGridColSize[0]; + int *dp = (int*)malloc(sizeof(int) * n); + int i, j; + + // 初始化dp为第一行起始状态。 + for (j = 0; j < n; ++j) { + if (obstacleGrid[0][j] == 1) + dp[j] = 0; + else if (j == 0) + dp[j] = 1; + else + dp[j] = dp[j - 1]; + } + + for (i = 1; i < m; ++i) { + for (j = 0; j < n; ++j) { + if (obstacleGrid[i][j] == 1) + dp[j] = 0; + // 若j为0,dp[j]表示最左边一列,无需改动 + // 此处dp[j],dp[j-1]等同于二维dp中的dp[i-1][j]和dp[i][j-1] + else if (j != 0) + dp[j] += dp[j - 1]; + } + } + + return dp[n - 1]; +} +``` + ### Scala ```scala