diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index fa8ad193..5c8270b6 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -147,6 +147,7 @@ class Solution { Python: + ```python3 class Solution: def climbStairs(self, n: int) -> int: @@ -163,8 +164,6 @@ class Solution: ``` - - Go: ```go func climbStairs(n int) int { 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 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 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 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: diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index aede1831..372dc40c 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -337,6 +337,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: + 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]//回溯 + } +} +``` + + + 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) +} +```