From 39f852e656a10cc5558148fb31c4f93f4c455d6f Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 6 Jun 2021 22:13:15 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0222.完全二叉树的节点个数 go版本 --- .../0222.完全二叉树的节点个数.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 91e24247..998e22f3 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -308,6 +308,35 @@ class Solution: Go: +递归版本 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +//本题直接就是求有多少个节点,无脑存进数组算长度就行了。 +func countNodes(root *TreeNode) int { + if root == nil { + return 0 + } + res := 1 + if root.Right != nil { + res += countNodes(root.Right) + } + if root.Left != nil { + res += countNodes(root.Left) + } + return res +} +``` + + + JavaScript: 递归版本 From 05f0c7ae93345d65986edc6e62ea8afb7bfd47c1 Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Sun, 6 Jun 2021 18:32:53 -0700 Subject: [PATCH 2/8] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 2adb826c..aede1831 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -257,6 +257,83 @@ Java: Python: +100.相同的树 +> 递归法 +```python +class Solution: + def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: + return self.compare(p, q) + + def compare(self, tree1, tree2): + if not tree1 and tree2: + return False + elif tree1 and not tree2: + return False + elif not tree1 and not tree2: + return True + elif tree1.val != tree2.val: #注意这里我没有使用else + return False + + #此时就是:左右节点都不为空,且数值相同的情况 + #此时才做递归,做下一层的判断 + compareLeft = self.compare(tree1.left, tree2.left) #左子树:左、 右子树:左 + compareRight = self.compare(tree1.right, tree2.right) #左子树:右、 右子树:右 + isSame = compareLeft and compareRight #左子树:中、 右子树:中(逻辑处理) + return isSame +``` + +257.二叉的所有路径 +> 递归中隐藏着回溯 +```python +class Solution: + def binaryTreePaths(self, root: TreeNode) -> List[str]: + result = [] + path = [] + if not root: + return result + self.traversal(root, path, result) + return result + + def traversal(self, cur, path, result): + path.append(cur.val) + #这才到了叶子节点 + if not cur.left and not cur.right: + sPath = "" + for i in range(len(path)-1): + sPath += str(path[i]) + sPath += "->" + sPath += str(path[len(path)-1]) + result.append(sPath) + return + if cur.left: + self.traversal(cur.left, path, result) + path.pop() #回溯 + if cur.right: + self.traversal(cur.right, path, result) + path.pop() #回溯 +``` + +> 精简版 +```python +class Solution: + def binaryTreePaths(self, root: TreeNode) -> List[str]: + result = [] + path = "" + if not root: + return result + self.traversal(root, path, result) + return result + + def traversal(self, cur, path, result): + path += str(cur.val) #中 + if not cur.left and not cur.right: + result.append(path) + return + if cur.left: + self.traversal(cur.left, path+"->", result) #左 回溯就隐藏在这里 + if cur.right: + self.traversal(cur.right, path+"->", result) #右 回溯就隐藏在这里 +``` Go: From d268469c225421e6424954ba5c7dad0cc357332f Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Sun, 6 Jun 2021 20:23:45 -0700 Subject: [PATCH 3/8] =?UTF-8?q?Update=200198.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0198.打家劫舍.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index 8b46a784..f49857bb 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -131,7 +131,20 @@ class Solution { ``` Python: - +```python +class Solution: + def rob(self, nums: List[int]) -> int: + if len(nums) == 0: + return 0 + if len(nums) == 1: + return nums[0] + dp = [0] * len(nums) + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + for i in range(2, len(nums)): + dp[i] = max(dp[i-2]+nums[i], dp[i-1]) + return dp[-1] +``` Go: ```Go From 74d061366602ec1f10b42ce7e9bc0e0406bb4d5f Mon Sep 17 00:00:00 2001 From: Baturu <45113401+z80160280@users.noreply.github.com> Date: Sun, 6 Jun 2021 21:02:20 -0700 Subject: [PATCH 4/8] =?UTF-8?q?Update=200337.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DIII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0337.打家劫舍III.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 58d0e640..0a4b752b 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -287,6 +287,25 @@ class Solution { Python: +> 动态规划 +```python +class Solution: + def rob(self, root: TreeNode) -> int: + result = self.robTree(root) + return max(result[0], result[1]) + + #长度为2的数组,0:不偷,1:偷 + def robTree(self, cur): + if not cur: + return (0, 0) #这里返回tuple, 也可以返回list + left = self.robTree(cur.left) + right = self.robTree(cur.right) + #偷cur + val1 = cur.val + left[0] + right[0] + #不偷cur + val2 = max(left[0], left[1]) + max(right[0], right[1]) + return (val2, val1) +``` Go: From 0913f488a401d5102a347852e1eecd3b30836f1f Mon Sep 17 00:00:00 2001 From: zhangzw Date: Mon, 7 Jun 2021 12:03:16 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1=20Go?= =?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/背包理论基础01背包-1.md | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index a78d9232..de4508ad 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -273,7 +273,40 @@ Python: Go: +```go +func test_2_wei_bag_problem1(weight, value []int, bagWeight int) int { + // 定义dp数组 + dp := make([][]int, len(weight)) + for i, _ := range dp { + dp[i] = make([]int, bagWeight+1) + } + // 初始化 + for j := bagWeight; j >= weight[0]; j-- { + dp[0][j] = dp[0][j-weight[0]] + value[0] + } + // 递推公式 + for i := 1; i < len(weight); i++ { + //正序,也可以倒序 + for j := weight[i];j<= bagWeight ; j++ { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) + } + } + return dp[len(weight)-1][bagWeight] +} +func max(a,b int) int { + if a > b { + return a + } + return b +} + +func main() { + weight := []int{1,3,4} + value := []int{15,20,30} + test_2_wei_bag_problem1(weight,value,4) +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 7fe87c16d93eb2b2fdaeaf0cc53a1499135d0e3a Mon Sep 17 00:00:00 2001 From: zhangzw Date: Mon, 7 Jun 2021 12:03:53 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2=20Go?= =?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/背包理论基础01背包-2.md | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index e831d88f..1d3f8b03 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -219,7 +219,36 @@ Python: Go: +```go +func test_1_wei_bag_problem(weight, value []int, bagWeight int) int { + // 定义 and 初始化 + dp := make([]int,bagWeight+1) + // 递推顺序 + for i := 0 ;i < len(weight) ; i++ { + // 这里必须倒序,区别二维,因为二维dp保存了i的状态 + for j:= bagWeight; j >= weight[i] ; j-- { + // 递推公式 + dp[j] = max(dp[j], dp[j-weight[i]]+value[i]) + } + } + //fmt.Println(dp) + return dp[bagWeight] +} +func max(a,b int) int { + if a > b { + return a + } + return b +} + + +func main() { + weight := []int{1,3,4} + value := []int{15,20,30} + test_1_wei_bag_problem(weight,value,4) +} +``` From 2499b611a5129717fa99e10a89daa170e7084f99 Mon Sep 17 00:00:00 2001 From: nmydt <62681228+nmydt@users.noreply.github.com> Date: Mon, 7 Jun 2021 16:44:12 +0800 Subject: [PATCH 7/8] =?UTF-8?q?Update=200530.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E5=B7=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 143bd053..03c48d9c 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -151,7 +151,30 @@ public: Java: - +递归 +```java +class Solution { + TreeNode pre;// 记录上一个遍历的结点 + int result = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { + if(root==null)return 0; + traversal(root); + return result; + } + public void traversal(TreeNode root){ + if(root==null)return; + //左 + traversal(root.left); + //中 + if(pre!=null){ + result = Math.min(result,root.val-pre.val); + } + pre = root; + //右 + traversal(root.right); + } +} +``` ```Java class Solution { TreeNode pre;// 记录上一个遍历的结点 From 13537833ada6dedea5f7bb91766e6ecd1b9b172a Mon Sep 17 00:00:00 2001 From: zhangzw Date: Mon, 7 Jun 2021 16:46:29 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A00474.=E4=B8=80=E5=92=8C?= =?UTF-8?q?=E9=9B=B6=20Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index d60faa98..fd925f76 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -206,7 +206,43 @@ class Solution: ``` Go: +```go +func findMaxForm(strs []string, m int, n int) int { + // 定义数组 + dp := make([][]int, m+1) + for i,_ := range dp { + dp[i] = make([]int, n+1 ) + } + // 遍历 + for i:=0;i= zeroNum;j-- { + for k:=n ; k >= oneNum;k-- { + // 推导公式 + dp[j][k] = max(dp[j][k],dp[j-zeroNum][k-oneNum]+1) + } + } + //fmt.Println(dp) + } + return dp[m][n] +} +func max(a,b int) int { + if a > b { + return a + } + return b +} +```