From 7b6223f7d911cf4be160b60e5405efad8e07028c Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Fri, 11 Jun 2021 00:33:03 +0800 Subject: [PATCH 1/8] =?UTF-8?q?Update=200070.=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added python version code --- problems/0070.爬楼梯完全背包版本.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 9fc33bb8..90c9a8c0 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -146,7 +146,18 @@ class Solution { ``` Python: - +```python +class Solution: + def climbStairs(self, n: int) -> int: + m = 2 + dp = [0] * (n + 1) + dp[0] = 1 + for i in range(n + 1): + for j in range(1, m + 1): + if i >= j: + dp[i] += dp[i - j] + return dp[-1] +``` Go: ```go From 5b268b787f0f07e926013c7246469244b95d0169 Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Fri, 11 Jun 2021 20:59:12 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=89=91=E6=8C=87Offe?= =?UTF-8?q?r05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20go=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer05.替换空格.md | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 794f9ac5..a4c0149f 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -154,7 +154,54 @@ Python: Go: +```go +// 遍历添加 +func replaceSpace(s string) string { + b := []byte(s) + result := make([]byte, 0) + for i := 0; i < len(b); i++ { + if b[i] == ' ' { + result = append(result, []byte("%20")...) + } else { + result = append(result, b[i]) + } + } + return string(result) +} +// 原地修改 +func replaceSpace(s string) string { + b := []byte(s) + length := len(b) + spaceCount := 0 + // 计算空格数量 + for _, v := range b { + if v == ' ' { + spaceCount++ + } + } + // 扩展原有切片 + resizeCount := spaceCount * 2 + tmp := make([]byte, resizeCount) + b = append(b, tmp...) + i := length - 1 + j := len(b) - 1 + for i >= 0 { + if b[i] != ' ' { + b[j] = b[i] + i-- + j-- + } else { + b[j] = '0' + b[j-1] = '2' + b[j-2] = '%' + i-- + j = j - 3 + } + } + return string(b) +} +``` From fde1d0230871a5b16c65123d14581d941d8ba83e Mon Sep 17 00:00:00 2001 From: Yang Date: Fri, 11 Jun 2021 16:16:29 -0400 Subject: [PATCH 3/8] =?UTF-8?q?Update=200718.=E6=9C=80=E9=95=BF=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add java solution --- problems/0718.最长重复子数组.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 204a513b..bef616d3 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -154,7 +154,25 @@ public: Java: - +```java +class Solution { + public int findLength(int[] nums1, int[] nums2) { + int result = 0; + int[][] dp = new int[nums1.length + 1][nums2.length + 1]; + + for (int i = 1; i < nums1.length + 1; i++) { + for (int j = 1; j < nums2.length + 1; j++) { + if (nums1[i - 1] == nums2[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + max = Math.max(max, dp[i][j]); + } + } + } + + return result; + } +} +``` Python: From fd2dfc4c0139d3296470271c5b1e0bd9af1303ba Mon Sep 17 00:00:00 2001 From: haofeng <852172305@qq.com> Date: Sat, 12 Jun 2021 10:43:17 +0800 Subject: [PATCH 4/8] =?UTF-8?q?Update=200279.=E5=AE=8C=E5=85=A8=E5=B9=B3?= =?UTF-8?q?=E6=96=B9=E6=95=B0.md=20=E6=B7=BB=E5=8A=A0=20python3=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 8d8b161c..60d6d165 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -26,7 +26,7 @@ 输入:n = 13 输出:2 解释:13 = 4 + 9 -  + 提示: * 1 <= n <= 10^4 @@ -184,6 +184,38 @@ class Solution { Python: +```python3 +class Solution: + def numSquares(self, n: int) -> int: + '''版本一''' + # 初始化 + nums = [i**2 for i in range(1, n + 1) if i**2 <= n] + dp = [10**4]*(n + 1) + dp[0] = 0 + # 遍历背包 + for j in range(1, n + 1): + # 遍历物品 + for num in nums: + if j >= num: + dp[j] = min(dp[j], dp[j - num] + 1) + return dp[n] + + def numSquares1(self, n: int) -> int: + '''版本二''' + # 初始化 + nums = [i**2 for i in range(1, n + 1) if i**2 <= n] + dp = [10**4]*(n + 1) + dp[0] = 0 + # 遍历物品 + for num in nums: + # 遍历背包 + for j in range(num, n + 1) + dp[j] = min(dp[j], dp[j - num] + 1) + return dp[n] +``` + + + Go: ```go From 362a8947f04cbca1ceed2ad2dac392dd95ca81fb Mon Sep 17 00:00:00 2001 From: haofeng <852172305@qq.com> Date: Sat, 12 Jun 2021 11:08:35 +0800 Subject: [PATCH 5/8] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2.md=20=E6=B7=BB=E5=8A=A0=20python3=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0322.零钱兑换.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index cc288593..cf537088 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -35,7 +35,7 @@ 示例 5: 输入:coins = [1], amount = 2 输出:2 -  + 提示: * 1 <= coins.length <= 12 @@ -209,6 +209,36 @@ class Solution { Python: +```python3 +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + '''版本一''' + # 初始化 + dp = [amount + 1]*(amount + 1) + dp[0] = 0 + # 遍历物品 + for coin in coins: + # 遍历背包 + for j in range(coin, amount + 1): + dp[j] = min(dp[j], dp[j - coin] + 1) + return dp[amount] if dp[amount] < amount + 1 else -1 + + def coinChange1(self, coins: List[int], amount: int) -> int: + '''版本二''' + # 初始化 + dp = [amount + 1]*(amount + 1) + dp[0] = 0 + # 遍历物品 + for j in range(1, amount + 1): + # 遍历背包 + for coin in coins: + if j >= coin: + dp[j] = min(dp[j], dp[j - coin] + 1) + return dp[amount] if dp[amount] < amount + 1 else -1 +``` + + + Go: ```go From 1c427ed82a095fb7e632c7a3b89ac2e1ba1255f6 Mon Sep 17 00:00:00 2001 From: haofeng <852172305@qq.com> Date: Sat, 12 Jun 2021 12:06:27 +0800 Subject: [PATCH 6/8] =?UTF-8?q?Update=200139.=E5=8D=95=E8=AF=8D=E6=8B=86?= =?UTF-8?q?=E5=88=86.md=20=E6=B7=BB=E5=8A=A0=20python3=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0139.单词拆分.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index aa729e02..b6a6242e 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -252,6 +252,23 @@ class Solution { Python: +```python3 +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + '''排列''' + dp = [False]*(len(s) + 1) + dp[0] = True + # 遍历背包 + for j in range(1, len(s) + 1): + # 遍历单词 + for word in wordDict: + if j >= len(word): + dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j]) + return dp[len(s)] +``` + + + Go: ```Go From ecf5ff1dc832b028e377d4afed3bfbf5f6317573 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 12 Jun 2021 14:24:04 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=EF=BC=9A=E4=BB=A5=E4=B8=BA=E4=BD=BF=E7=94=A8=E4=BA=86?= =?UTF-8?q?=E9=80=92=E5=BD=92=EF=BC=8C=E5=85=B6=E5=AE=9E=E8=BF=98=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E7=9D=80=E5=9B=9E=E6=BA=AF=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 二叉树:以为使用了递归,其实还隐藏着回溯 go版本 --- problems/二叉树中递归带着回溯.md | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index aede1831..8014d6e1 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -336,6 +336,110 @@ class Solution: ``` Go: +100.相同的树 +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func isSameTree(p *TreeNode, q *TreeNode) bool { + switch { + case p == nil && q == nil: + return true + case p == nil || q == nil: + fmt.Println(p,q) + fallthrough + case p.Val != q.Val: + return false + } + return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right) +} +``` + +257.二叉的所有路径 +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func binaryTreePaths(root *TreeNode) []string { + var result []string + traversal(root,&result,"") + return result +} +func traversal(root *TreeNode,result *[]string,pathStr string){ + //判断是否为第一个元素 + if len(pathStr)!=0{ + pathStr=pathStr+"->"+strconv.Itoa(root.Val) + }else{ + pathStr=strconv.Itoa(root.Val) + } + //判断是否为叶子节点 + if root.Left==nil&&root.Right==nil{ + *result=append(*result,pathStr) + return + } + //左右 + if root.Left!=nil{ + traversal(root.Left,result,pathStr) + } + if root.Right!=nil{ + traversal(root.Right,result,pathStr) + } +} +``` + +> 回溯法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func binaryTreePaths(root *TreeNode) []string { + var result []string + var path []int + traversal(root,&result,&path) + return result +} +func traversal(root *TreeNode,result *[]string,path *[]int){ + *path=append(*path,root.Val) + //判断是否为叶子节点 + if root.Left==nil&&root.Right==nil{ + pathStr:=strconv.Itoa((*path)[0]) + for i:=1;i"+strconv.Itoa((*path)[i]) + } + *result=append(*result,pathStr) + return + } + //左右 + if root.Left!=nil{ + traversal(root.Left,result,path) + *path=(*path)[:len(*path)-1]//回溯到上一个节点(因为traversal会加下一个节点值到path中) + } + if root.Right!=nil{ + traversal(root.Right,result,path) + *path=(*path)[:len(*path)-1]//回溯 + } +} +``` + + From 14542ac3404b82ad492ea50cde7f56a6051185dd Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 12 Jun 2021 14:25:27 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=EF=BC=9A=E4=BB=A5=E4=B8=BA=E4=BD=BF=E7=94=A8=E4=BA=86?= =?UTF-8?q?=E9=80=92=E5=BD=92=EF=BC=8C=E5=85=B6=E5=AE=9E=E8=BF=98=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E7=9D=80=E5=9B=9E=E6=BA=AF=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 二叉树:以为使用了递归,其实还隐藏着回溯 go版本 --- problems/二叉树中递归带着回溯.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 8014d6e1..372dc40c 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -336,6 +336,7 @@ class Solution: ``` Go: + 100.相同的树 ```go /** @@ -351,7 +352,6 @@ func isSameTree(p *TreeNode, q *TreeNode) bool { case p == nil && q == nil: return true case p == nil || q == nil: - fmt.Println(p,q) fallthrough case p.Val != q.Val: return false