From fe71144f142063bc8d8f48b81e390bd3e2fa011e Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Fri, 25 Jun 2021 11:32:49 +0800 Subject: [PATCH 01/27] =?UTF-8?q?0509.=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91?= =?UTF-8?q?=E6=95=B0=E5=88=97.md=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index cf004b50..dddac899 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -220,7 +220,17 @@ func fib(n int) int { return c } ``` - +Javascript: +```Javascript +var fib = function(n) { + let dp = [0, 1] + for(let i = 2; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2] + } + console.log(dp) + return dp[n] +}; +``` From bce132cb329b69a2677bcf5415d7469913014dca Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Fri, 25 Jun 2021 11:49:59 +0800 Subject: [PATCH 02/27] =?UTF-8?q?0070.=E7=88=AC=E6=A5=BC=E6=A2=AF.md=20Jav?= =?UTF-8?q?ascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 680af587..96899d37 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -283,7 +283,18 @@ func climbStairs(n int) int { return dp[n] } ``` - +Javascript: +```Javascript +var climbStairs = function(n) { + // dp[i] 为第 i 阶楼梯有多少种方法爬到楼顶 + // dp[i] = dp[i - 1] + dp[i - 2] + let dp = [1 , 2] + for(let i = 2; i < n; i++) { + dp[i] = dp[i - 1] + dp[i - 2] + } + return dp[n - 1] +}; +``` ----------------------- From a921b3ba3dbd9179721cf4f408f2a5f41a34e7ad Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Sat, 26 Jun 2021 13:10:28 +0200 Subject: [PATCH 03/27] =?UTF-8?q?=E4=BC=98=E5=8C=96=200062=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 0062不同路径 python3版本 --- problems/0062.不同路径.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 47cb41af..50e70b3e 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -279,9 +279,7 @@ Python: ```python class Solution: # 动态规划 def uniquePaths(self, m: int, n: int) -> int: - dp = [[0 for i in range(n)] for j in range(m)] - for i in range(m): dp[i][0] = 1 - for j in range(n): dp[0][j] = 1 + dp = [[1 for i in range(n)] for j in range(m)] for i in range(1, m): for j in range(1, n): dp[i][j] = dp[i][j - 1] + dp[i - 1][j] From 178cf44b202bf10707fed8825d3bf23b8e9d8f8b Mon Sep 17 00:00:00 2001 From: "xeniaxie(xl)" Date: Sat, 26 Jun 2021 20:33:27 +0800 Subject: [PATCH 04/27] =?UTF-8?q?0501=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0JavaScript?= =?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/0501.二叉搜索树中的众数.md | 74 +++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 3ca7d892..74c53458 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -522,7 +522,79 @@ func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计 } } ``` - +JavaScript版本: +使用额外空间map的方法: +```javascript +var findMode = function(root) { + // 使用递归中序遍历 + let map = new Map(); + // 1. 确定递归函数以及函数参数 + const traverTree = function(root) { + // 2. 确定递归终止条件 + if(root === null) { + return ; + } + traverTree(root.left); + // 3. 单层递归逻辑 + map.set(root.val,map.has(root.val)?map.get(root.val)+1:1); + traverTree(root.right); + } + traverTree(root); + //上面把数据都存储到map + //下面开始寻找map里面的 + // 定义一个最大出现次数的初始值为root.val的出现次数 + let maxCount = map.get(root.val); + // 定义一个存放结果的数组res + let res = []; + for(let [key,value] of map) { + // 如果当前值等于最大出现次数就直接在res增加该值 + if(value === maxCount) { + res.push(key); + } + // 如果value的值大于原本的maxCount就清空res的所有值,因为找到了更大的 + if(value>maxCount) { + res = []; + maxCount = value; + res.push(key); + } + } + return res; +}; +``` +不使用额外空间,利用二叉树性质,中序遍历(有序): +```javascript +var findMode = function(root) { + // 不使用额外空间,使用中序遍历,设置出现最大次数初始值为1 + let count = 0,maxCount = 1; + let pre = root,res = []; + // 1.确定递归函数及函数参数 + const travelTree = function(cur) { + // 2. 确定递归终止条件 + if(cur === null) { + return ; + } + travelTree(cur.left); + // 3. 单层递归逻辑 + if(pre.val === cur.val) { + count++; + }else { + count = 1; + } + pre = cur; + if(count === maxCount) { + res.push(cur.val); + } + if(count > maxCount) { + res = []; + maxCount = count; + res.push(cur.val); + } + travelTree(cur.right); + } + travelTree(root); + return res; +}; +``` ----------------------- From f3b21c7984039eaa37c8da08eac725e6c8d1e11b Mon Sep 17 00:00:00 2001 From: "xeniaxie(xl)" Date: Sat, 26 Jun 2021 21:12:39 +0800 Subject: [PATCH 05/27] =?UTF-8?q?0236=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88JavaScrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0236.二叉树的最近公共祖先.md | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 2f7aa6c3..7673a0ab 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -310,7 +310,31 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { return nil } ``` - +JavaScript版本: +```javascript +var lowestCommonAncestor = function(root, p, q) { + // 使用递归的方法 + // 需要从下到上,所以使用后序遍历 + // 1. 确定递归的函数 + const travelTree = function(root,p,q) { + // 2. 确定递归终止条件 + if(root === null || root === p||root === q) { + return root; + } + // 3. 确定递归单层逻辑 + let left = travelTree(root.left,p,q); + let right = travelTree(root.right,p,q); + if(left !== null&&right !== null) { + return root; + } + if(left ===null) { + return right; + } + return left; + } + return travelTree(root,p,q); +}; +``` ----------------------- From 1844077399766d28727f1b0d297855a473030339 Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Sat, 26 Jun 2021 22:53:33 +0800 Subject: [PATCH 06/27] =?UTF-8?q?0746.=E4=BD=BF=E7=94=A8=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF.md=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..2ca27187 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "pwa-chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file From 3dc4167c59a29b7654d0a03a76645bee1e6e57db Mon Sep 17 00:00:00 2001 From: Yan Wen Date: Sun, 27 Jun 2021 09:19:45 +0800 Subject: [PATCH 07/27] =?UTF-8?q?update=E5=B7=A6=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E6=B7=BB=E5=8A=A0python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../剑指Offer58-II.左旋转字符串.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 39c8382c..ca903900 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -119,6 +119,27 @@ class Solution { ``` Python: +```python +# 方法一:可以使用切片方法 +class Solution: + def reverseLeftWords(self, s: str, n: int) -> str: + return s[n:] + s[0:n] + +# 方法二:也可以使用上文描述的方法,有些面试中不允许使用切片,那就使用上文作者提到的方法 +# class Solution: +# def reverseLeftWords(self, s: str, n: int) -> str: +# s = list(s) +# s[0:n] = list(reversed(s[0:n])) +# s[n:] = list(reversed(s[n:])) +# s.reverse() + +# return "".join(s) + + +# 时间复杂度:O(n) +# 空间复杂度:O(n),python的string为不可变,需要开辟同样大小的list空间来修改 +``` + Go: ```go From c0dbcc8a73eec6517b8b26e376a8c7060b8b858b Mon Sep 17 00:00:00 2001 From: "xeniaxie(xl)" Date: Sun, 27 Jun 2021 10:28:58 +0800 Subject: [PATCH 08/27] =?UTF-8?q?0235=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.二叉搜索树的最近公共祖先.md | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index d78db42a..b8b23f09 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -312,7 +312,46 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { }else {return findLeft} } ``` - +JavaScript版本: +1. 使用递归的方法: +```javascript +var lowestCommonAncestor = function(root, p, q) { + // 使用递归的方法 + // 1. 使用给定的递归函数lowestCommonAncestor + // 2. 确定递归终止条件 + if(root === null) { + return root; + } + if(root.val>p.val&&root.val>q.val) { + // 向左子树查询 + let left = lowestCommonAncestor(root.left,p,q); + return left !== null&&left; + } + if(root.valp.val&&root.val>q.val) { + root = root.left; + }else if(root.val Date: Sun, 27 Jun 2021 10:35:03 +0800 Subject: [PATCH 09/27] =?UTF-8?q?Revert=20"0235=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit c0dbcc8a73eec6517b8b26e376a8c7060b8b858b. --- ...35.二叉搜索树的最近公共祖先.md | 41 +------------------ 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index b8b23f09..d78db42a 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -312,46 +312,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { }else {return findLeft} } ``` -JavaScript版本: -1. 使用递归的方法: -```javascript -var lowestCommonAncestor = function(root, p, q) { - // 使用递归的方法 - // 1. 使用给定的递归函数lowestCommonAncestor - // 2. 确定递归终止条件 - if(root === null) { - return root; - } - if(root.val>p.val&&root.val>q.val) { - // 向左子树查询 - let left = lowestCommonAncestor(root.left,p,q); - return left !== null&&left; - } - if(root.valp.val&&root.val>q.val) { - root = root.left; - }else if(root.val Date: Sun, 27 Jun 2021 10:45:48 +0800 Subject: [PATCH 10/27] =?UTF-8?q?0235=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96?= =?UTF-8?q?=E5=85=88Javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.二叉搜索树的最近公共祖先.md | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index d78db42a..8ea3cdc5 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -312,8 +312,46 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { }else {return findLeft} } ``` - - +JavaScript版本: +1. 使用递归的方法 +```javascript +var lowestCommonAncestor = function(root, p, q) { + // 使用递归的方法 + // 1. 使用给定的递归函数lowestCommonAncestor + // 2. 确定递归终止条件 + if(root === null) { + return root; + } + if(root.val>p.val&&root.val>q.val) { + // 向左子树查询 + let left = lowestCommonAncestor(root.left,p,q); + return left !== null&&left; + } + if(root.valp.val&&root.val>q.val) { + root = root.left; + }else if(root.val Date: Sun, 27 Jun 2021 13:11:31 +0800 Subject: [PATCH 11/27] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 1269d9c1..3cbfb347 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -82,7 +82,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 那么可以有两个方向推出来dp[i][j], -* 由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j] +* 由dp[i - 1][j]推出,即背包容量为j,里面不放物品i的最大价值,此时dp[i][j]就是dp[i - 1][j]。(其实就是当物品i的重量大于背包j的重量时,物品i无法放进背包中,所以被背包内的价值依然和前面相同。) * 由dp[i - 1][j - weight[i]]推出,dp[i - 1][j - weight[i]] 为背包容量为j - weight[i]的时候不放物品i的最大价值,那么dp[i - 1][j - weight[i]] + value[i] (物品i的价值),就是背包放物品i得到的最大价值 所以递归公式: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); From 1a16c119a39f1929af9294b653385defa7c904f3 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Sun, 27 Jun 2021 15:44:40 +0800 Subject: [PATCH 12/27] =?UTF-8?q?feat(=E4=BA=8C=E5=8F=89=E6=A0=91=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80.md):=20=E6=96=B0=E5=A2=9Ejava?= =?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/二叉树理论基础.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index 55383e91..d4dfe0c6 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -188,6 +188,21 @@ struct TreeNode { Java: +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + TreeNode() {} + TreeNode(int val) { this.val = val; } + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} +``` + Python: From 5e68f30728cbcdb670eda7f12d3c208796f5c731 Mon Sep 17 00:00:00 2001 From: Jijie LIU Date: Sun, 27 Jun 2021 17:40:11 +0200 Subject: [PATCH 13/27] =?UTF-8?q?update=20279.=E5=AE=8C=E5=85=A8=E5=B9=B3?= =?UTF-8?q?=E6=96=B9=E6=95=B0:=20=E6=8F=90=E4=BE=9B=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 60d6d165..d0922de1 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -214,8 +214,26 @@ class Solution: return dp[n] ``` +Python3: +```python +class Solution: + def numSquares(self, n: int) -> int: + # 初始化 + # 组成和的完全平方数的最多个数,就是只用1构成 + # 因此,dp[i] = i + dp = [i for i in range(n + 1)] + # dp[0] = 0 无意义,只是为了方便记录特殊情况: + # n本身就是完全平方数,dp[n] = min(dp[n], dp[n - n] + 1) = 1 + for i in range(1, n): # 遍历物品 + if i * i > n: + break + num = i * i + for j in range(num, n + 1): # 遍历背包 + dp[j] = min(dp[j], dp[j - num] + 1) + return dp[n] +``` Go: ```go From dc03975acdb47f6c5eaa0b2b3a47d119f8fe78f2 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 29 Jun 2021 00:20:13 +0800 Subject: [PATCH 14/27] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200098.=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20go?= =?UTF-8?q?=E7=89=88=20=EF=BC=88=E5=A2=9E=E5=8A=A0=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E8=A7=A3=E9=A2=98=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加中序遍历解题法 --- problems/0098.验证二叉搜索树.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index e38c5ade..b93d8cd5 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -376,6 +376,28 @@ func isBST(root *TreeNode, min, max int) bool { return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max) } ``` +```go +// 中序遍历解法 +func isValidBST(root *TreeNode) bool { + // 保存上一个指针 + var prev *TreeNode + var travel func(node *TreeNode) bool + travel = func(node *TreeNode) bool { + if node == nil { + return true + } + leftRes := travel(node.Left) + // 当前值小于等于前一个节点的值,返回false + if prev != nil && node.Val <= prev.Val { + return false + } + prev = node + rightRes := travel(node.Right) + return leftRes && rightRes + } + return travel(root) +} +``` JavaScript版本 From f75acf5b896d78b5b124b7c6b3713a75a4206acc Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Tue, 29 Jun 2021 14:22:56 +0800 Subject: [PATCH 15/27] =?UTF-8?q?0-1=E8=83=8C=E5=8C=85javascript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-1.md | 46 ++++++++++++++++++++++++ problems/背包理论基础01背包-2.md | 23 ++++++++++++ 2 files changed, 69 insertions(+) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 3cbfb347..85bc7e42 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -380,6 +380,52 @@ func main() { } ``` +javaScript: + +```js +function testWeightBagProblem (wight, value, size) { + const len = wight.length, + dp = Array.from({length: len + 1}).map( + () => Array(size + 1).fill(0) + ); + + for(let i = 1; i <= len; i++) { + for(let j = 0; j <= size; j++) { + if(wight[i - 1] <= j) { + dp[i][j] = Math.max( + dp[i - 1][j], + value[i - 1] + dp[i - 1][j - wight[i - 1]] + ) + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + +// console.table(dp); + + return dp[len][size]; +} + +function testWeightBagProblem2 (wight, value, size) { + const len = wight.length, + dp = Array(size + 1).fill(0); + for(let i = 1; i <= len; i++) { + for(let j = size; j >= wight[i - 1]; j--) { + dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]); + } + } + return dp[size]; +} + + +function test () { + console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); +} + +test(); +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index 48275908..36856cd6 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -294,6 +294,29 @@ func main() { } ``` +javaScript: + +```js + +function testWeightBagProblem(wight, value, size) { + const len = wight.length, + dp = Array(size + 1).fill(0); + for(let i = 1; i <= len; i++) { + for(let j = size; j >= wight[i - 1]; j--) { + dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]); + } + } + return dp[size]; +} + + +function test () { + console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); +} + +test(); +``` + ----------------------- From 204c0c5e959c76c035bf35c704c91f388e8d94aa Mon Sep 17 00:00:00 2001 From: "qingyi.liu" Date: Tue, 29 Jun 2021 14:27:49 +0800 Subject: [PATCH 16/27] =?UTF-8?q?416.=20=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86=20javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 0d306c74..75c665cd 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -240,6 +240,26 @@ class Solution: Go: +javaScript: + +```js +var canPartition = function(nums) { + const sum = (nums.reduce((p, v) => p + v)); + if (sum & 1) return false; + const dp = Array(sum / 2 + 1).fill(0); + for(let i = 0; i < nums.length; i++) { + for(let j = sum / 2; j >= nums[i]; j--) { + dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i]); + if (dp[j] === sum / 2) { + return true; + } + } + } + return dp[sum / 2] === sum / 2; +}; +``` + + ----------------------- From abc55ed100b031a00383038a3b7e2ee91c667b7a Mon Sep 17 00:00:00 2001 From: KailokFung Date: Tue, 29 Jun 2021 16:50:34 +0800 Subject: [PATCH 17/27] =?UTF-8?q?feat(0226):=20=E6=96=B0=E5=A2=9Ejava=20bf?= =?UTF-8?q?s=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 2b628ec4..bb0d6d34 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -205,6 +205,7 @@ public: Java: ```Java +//DFS递归 class Solution { /** * 前后序遍历都可以 @@ -226,6 +227,31 @@ class Solution { root.right = tmp; } } + +//BFS +class Solution { + public TreeNode invertTree(TreeNode root) { + if (root == null) {return null;} + ArrayDeque deque = new ArrayDeque<>(); + deque.offer(root); + while (!deque.isEmpty()) { + int size = deque.size(); + while (size-- > 0) { + TreeNode node = deque.poll(); + swap(node); + if (node.left != null) {deque.offer(node.left);} + if (node.right != null) {deque.offer(node.right);} + } + } + return root; + } + + public void swap(TreeNode root) { + TreeNode temp = root.left; + root.left = root.right; + root.right = temp; + } +} ``` Python: From 011b0cc6fdd8058b738984066f075b37eb8245fc Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 29 Jun 2021 21:55:02 +0800 Subject: [PATCH 18/27] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE=20go=E7=89=88=20=EF=BC=88=E4=B8=AD?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86=E7=9A=84=E5=90=8C=E6=97=B6=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=9C=80=E5=B0=8F=E5=80=BC=EF=BC=8C=E4=B8=8D=E7=94=A8?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E7=9A=84=E5=AD=98=E5=82=A8=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 中序遍历的同时计算最小值,不用额外的存储空间 --- .../0530.二叉搜索树的最小绝对差.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 8fa756bc..47b2b434 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -255,6 +255,29 @@ func findMIn(root *TreeNode,res *[]int){ findMIn(root.Right,res) } ``` +```go +// 中序遍历的同时计算最小值 +func getMinimumDifference(root *TreeNode) int { + // 保留前一个节点的指针 + var prev *TreeNode + // 定义一个比较大的值 + min := math.MaxInt64 + var travel func(node *TreeNode) + travel = func(node *TreeNode) { + if node == nil { + return + } + travel(node.Left) + if prev != nil && node.Val - prev.Val < min { + min = node.Val - prev.Val + } + prev = node + travel(node.Right) + } + travel(root) + return min +} +``` JavaScript版本 From f85d1c1a3792d848f42f7fda03fbab5fd712d29c Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Tue, 29 Jun 2021 22:41:40 +0800 Subject: [PATCH 19/27] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200501.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?= =?UTF-8?q?=E6=95=B0=20go=E7=89=88=EF=BC=88=E5=8E=9F=E5=85=88=E7=9A=84?= =?UTF-8?q?=E5=86=99=E6=B3=95=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=8C=E8=BF=87?= =?UTF-8?q?=E4=B8=8D=E4=BA=86leetcode=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原先的写法有问题,过不了leetcode,更新正确的解法 --- problems/0501.二叉搜索树中的众数.md | 64 +++++++++----------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index f4b239c3..d2326d67 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -474,7 +474,7 @@ func traversal(root *TreeNode,history map[int]int){ } ``` -计数法BSL(此代码在执行代码里能执行,但提交后报错,不知为何,思路是对的) +计数法,不使用额外空间,利用二叉树性质,中序遍历 ```go /** @@ -485,41 +485,35 @@ func traversal(root *TreeNode,history map[int]int){ * Right *TreeNode * } */ - var count,maxCount int //统计计数 -func findMode(root *TreeNode) []int { - var result []int - var pre *TreeNode //前指针 - if root.Left==nil&&root.Right==nil{ - result=append(result,root.Val) - return result + func findMode(root *TreeNode) []int { + res := make([]int, 0) + count := 1 + max := 1 + var prev *TreeNode + var travel func(node *TreeNode) + travel = func(node *TreeNode) { + if node == nil { + return + } + travel(node.Left) + if prev != nil && prev.Val == node.Val { + count++ + } else { + count = 1 + } + if count >= max { + if count > max && len(res) > 0 { + res = []int{node.Val} + } else { + res = append(res, node.Val) + } + max = count + } + prev = node + travel(node.Right) } - traversal(root,&result,pre) - return result -} -func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计 - //如果BSL中序遍历相邻的两个节点值相同,则统计频率;如果不相同,依据BSL中序遍历排好序的性质,重新计数 - if pre==nil{ - count=1 - }else if pre.Val==root.Val{ - count++ - }else { - count=1 - } - //如果统计的频率等于最大频率,则加入结果集;如果统计的频率大于最大频率,更新最大频率且重新将结果加入新的结果集中 - if count==maxCount{ - *result=append(*result,root.Val) - }else if count>maxCount{ - maxCount=count//重新赋值maxCount - *result=[]int{}//清空result中的内容 - *result=append(*result,root.Val) - } - pre=root//保存上一个的节点 - if root.Left!=nil{ - traversal(root.Left,result,pre) - } - if root.Right!=nil{ - traversal(root.Right,result,pre) - } + travel(root) + return res } ``` From 383c9cb1705b6a0760a5208ff879a1cf56c35eb8 Mon Sep 17 00:00:00 2001 From: daniel1n <54945782+daniel1n@users.noreply.github.com> Date: Thu, 1 Jul 2021 18:25:51 +0800 Subject: [PATCH 20/27] =?UTF-8?q?Update=200501.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit java的暴力解法(map记录频率) --- problems/0501.二叉搜索树中的众数.md | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index d2326d67..66be790f 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -345,6 +345,40 @@ public: Java: +暴力法 +```java +class Solution { + public int[] findMode(FindModeInBinarySearchTree.TreeNode root) { + Map map = new HashMap<>(); + List list = new ArrayList<>(); + if (root == null) return list.stream().mapToInt(Integer::intValue).toArray(); + // 获得频率 Map + searchBST(root, map); + List> mapList = map.entrySet().stream() + .sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue())) + .collect(Collectors.toList()); + list.add(mapList.get(0).getKey()); + // 把频率最高的加入 list + for (int i = 1; i < mapList.size(); i++) { + if (mapList.get(i).getValue() == mapList.get(i - 1).getValue()) { + list.add(mapList.get(i).getKey()); + } else { + break; + } + } + return list.stream().mapToInt(Integer::intValue).toArray(); + } + + void searchBST(FindModeInBinarySearchTree.TreeNode curr, Map map) { + if (curr == null) return; + map.put(curr.val, map.getOrDefault(curr.val, 0) + 1); + searchBST(curr.left, map); + searchBST(curr.right, map); + } + +} +``` + ```Java class Solution { ArrayList resList; From d5e128e20921517085aa199e3a0667e8c2c252de Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Thu, 1 Jul 2021 23:13:32 +0800 Subject: [PATCH 21/27] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=20go=E7=89=88=EF=BC=88=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0701.二叉搜索树中的插入操作.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 794e0ae2..61027453 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -271,6 +271,9 @@ class Solution: Go: + +递归法 + ```Go func insertIntoBST(root *TreeNode, val int) *TreeNode { if root == nil { @@ -285,6 +288,31 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { return root } ``` +迭代法 +```go +func insertIntoBST(root *TreeNode, val int) *TreeNode { + if root == nil { + return &TreeNode{Val:val} + } + node := root + var pnode *TreeNode + for node != nil { + if val > node.Val { + pnode = node + node = node.Right + } else { + pnode = node + node = node.Left + } + } + if val > pnode.Val { + pnode.Right = &TreeNode{Val: val} + } else { + pnode.Left = &TreeNode{Val: val} + } + return root +} +``` JavaScript版本 From c47bafa412965064846d82be391a0ba851ae4d20 Mon Sep 17 00:00:00 2001 From: amdrose <927410375@qq.com> Date: Fri, 2 Jul 2021 01:47:13 +0800 Subject: [PATCH 22/27] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20php=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 31a808b0..5be94996 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -187,7 +187,23 @@ var twoSum = function (nums, target) { }; ``` +php +```php +function twoSum(array $nums, int $target): array +{ + for ($i = 0; $i < count($nums);$i++) { + // 计算剩下的数 + $residue = $target - $nums[$i]; + // 匹配的index,有则返回index, 无则返回false + $match_index = array_search($residue, $nums); + if ($match_index !== false && $match_index != $i) { + return array($i, $match_index); + } + } + return []; +} +``` ----------------------- From 46a4b5161cd5a777d6eb81547f2b25a7279248a5 Mon Sep 17 00:00:00 2001 From: amdrose <927410375@qq.com> Date: Fri, 2 Jul 2021 01:54:55 +0800 Subject: [PATCH 23/27] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20php=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 5b77a170..36abb58c 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -354,6 +354,47 @@ def is_valid(strs) end ``` +php: + +```php +function threeSum(array $nums): array +{ + $result = []; + $length = count($nums); + if ($length < 3) { + return []; + } + sort($nums); + for ($i = 0; $i < $length; $i++) { + // 如果大于0结束 + if ($nums[$i] > 0) break; + // 去重 + if ($i > 0 && $nums[$i] == $nums[$i - 1]) continue; + $left = $i + 1; + $right = $length - 1; + // 比较 + while ($left < $right) { + $sum = $nums[$i] + $nums[$left] + $nums[$right]; + if ($sum < 0) { + $left++; + } elseif ($sum > 0) { + $right--; + } else { + array_push($result, [$nums[$i], $nums[$left], $nums[$right]]); + while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++; + while ($left < $right && $nums[$right - 1] == $nums[$right]) $right--; + $left++; + $right--; + } + } + } + + return $result; +} +``` + + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 759f05328dfabb652a034b6400d2a162edc91679 Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Fri, 2 Jul 2021 07:51:09 +0800 Subject: [PATCH 24/27] =?UTF-8?q?=E6=95=B4=E6=95=B0=E6=8B=86=E5=88=86.md?= =?UTF-8?q?=20Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 16 ---------------- problems/0343.整数拆分.md | 13 +++++++++++++ 2 files changed, 13 insertions(+), 16 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 2ca27187..00000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - - { - "type": "pwa-chrome", - "request": "launch", - "name": "Launch Chrome against localhost", - "url": "http://localhost:8080", - "webRoot": "${workspaceFolder}" - } - ] -} \ No newline at end of file diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index cf60575f..7b0dbd0f 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -225,7 +225,20 @@ class Solution: Go: +Javascript: +```Javascript +var integerBreak = function(n) { + let dp = new Array(n + 1).fill(0) + dp[2] = 1 + for(let i = 3; i <= n; i++) { + for(let j = 1; j < i; j++) { + dp[i] = Math.max(dp[i], dp[i - j] * j, (i - j) * j) + } + } + return dp[n] +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 6360e80430bec5f648bf242ef1a2871a938d7189 Mon Sep 17 00:00:00 2001 From: hengzzha <80679845+hengzzha@users.noreply.github.com> Date: Fri, 2 Jul 2021 15:20:36 +0800 Subject: [PATCH 25/27] =?UTF-8?q?Update=20=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92-=E8=82=A1=E7=A5=A8=E9=97=AE=E9=A2=98=E6=80=BB?= =?UTF-8?q?=E7=BB=93=E7=AF=87.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/动态规划-股票问题总结篇.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/动态规划-股票问题总结篇.md b/problems/动态规划-股票问题总结篇.md index cac77b65..d22132c0 100644 --- a/problems/动态规划-股票问题总结篇.md +++ b/problems/动态规划-股票问题总结篇.md @@ -381,6 +381,12 @@ dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); dp[i][2] = dp[i - 1][0] + prices[i]; dp[i][3] = dp[i - 1][2]; ``` +```C++ +dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3]- prices[i], dp[i - 1][1]) - prices[i]; +dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); +dp[i][2] = dp[i - 1][0] + prices[i]; +dp[i][3] = dp[i - 1][2]; +``` 整体代码如下: From 891bf6a7eb92cb54eec4fa1d4a7ee0fcd1448fdb Mon Sep 17 00:00:00 2001 From: "xeniaxie(xl)" Date: Sat, 3 Jul 2021 16:10:02 +0800 Subject: [PATCH 26/27] =?UTF-8?q?669=E4=BF=AE=E5=89=AA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0669.修剪二叉搜索树.md | 53 ++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 06d99b9d..e20c3cea 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -289,6 +289,59 @@ Go: +JavaScript版本: +迭代: +```js +var trimBST = function(root, low, high) { + if(root === null) { + return null; + } + while(root !==null &&(root.valhigh)) { + if(root.valhigh) { + cur.right = cur.right.left; + } + cur = cur.right; + } + return root; +}; +``` + +递归: +```js +var trimBST = function (root,low,high) { + if(root === null) { + return null; + } + if(root.valhigh) { + let left = trimBST(root.left,low,high); + return left; + } + root.left = trimBST(root.left,low,high); + root.right = trimBST(root.right,low,high); + return root; + } +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 59403e05819e659dd3a0566656774bc6dd48846e Mon Sep 17 00:00:00 2001 From: KailokFung Date: Sat, 3 Jul 2021 16:14:10 +0800 Subject: [PATCH 27/27] =?UTF-8?q?feat(0257):=20=E6=96=B0=E5=A2=9E=E4=B8=A4?= =?UTF-8?q?=E7=A7=8Djava=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 49 ++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index d5a0478e..ce596396 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -283,6 +283,7 @@ public: Java: ```Java +//解法一 class Solution { /** * 递归法 @@ -321,6 +322,52 @@ class Solution { } } +//解法二(常规前序遍历,不用回溯),更容易理解 +class Solution { + public List binaryTreePaths(TreeNode root) { + List res = new ArrayList<>(); + helper(root, new StringBuilder(), res); + return res; + } + + public void helper(TreeNode root, StringBuilder sb, List res) { + if (root == null) {return;} + // 遇到叶子结点就放入当前路径到res集合中 + if (root.left == null && root.right ==null) { + sb.append(root.val); + res.add(sb.toString()); + // 记得结束当前方法 + return; + } + helper(root.left,new StringBuilder(sb).append(root.val + "->"),res); + helper(root.right,new StringBuilder(sb).append(root.val + "->"),res); + } +} + +//针对解法二优化,思路本质是一样的 +class Solution { + public List binaryTreePaths(TreeNode root) { + List res = new ArrayList<>(); + helper(root, "", res); + return res; + } + + public void helper(TreeNode root, String path, List res) { + if (root == null) {return;} + // 由原始解法二可以知道,root的值肯定会下面某一个条件加入到path中,那么干脆直接在这一步加入即可 + StringBuilder sb = new StringBuilder(path); + sb.append(root.val); + if (root.left == null && root.right ==null) { + res.add(sb.toString()); + }else{ + // 如果是非叶子结点则还需要跟上一个 “->” + sb.append("->"); + helper(root.left,sb.toString(),res); + helper(root.right,sb.toString(),res); + } + } +} + ``` Python: @@ -350,7 +397,7 @@ class Solution: ``` Go: - + ```go func binaryTreePaths(root *TreeNode) []string { res := make([]string, 0)