From b37bf8d2e25edd67e96a0f797ce03fc80cb6e133 Mon Sep 17 00:00:00 2001 From: hhj233 Date: Mon, 13 Sep 2021 10:44:59 +0800 Subject: [PATCH 1/6] feat: distance_dp-java --- ...编辑距离,卡尔做了三步铺垫.md | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md index 31a5e448..2eb253ba 100644 --- a/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md +++ b/problems/为了绝杀编辑距离,卡尔做了三步铺垫.md @@ -167,7 +167,33 @@ else { Java: - +```java +class Solution { + public int minDistance(String word1, String word2) { + int m = word1.length(); + int n = word2.length(); + int[][] dp = new int[m+1][n+1]; + for(int i = 1; i <= m; i++){ + dp[i][0] = i; + } + for(int i = 1; i <= n; i++){ + dp[0][i] = i; + } + for(int i = 1; i <= m; i++){ + for(int j = 1; j <= n; j++){ + int left = dp[i][j-1]+1; + int mid = dp[i-1][j-1]; + int right = dp[i-1][j]+1; + if(word1.charAt(i-1) != word2.charAt(j-1)){ + mid ++; + } + dp[i][j] = Math.min(left,Math.min(mid,right)); + } + } + return dp[m][n]; + } +} +``` Python: From 00b7b3687f28f3e3b542375e11bdaa99803c75f0 Mon Sep 17 00:00:00 2001 From: Jerry-306 <82520819+Jerry-306@users.noreply.github.com> Date: Mon, 13 Sep 2021 10:58:20 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=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/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 2bcded70..7d04b514 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -34,7 +34,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, ## 01 背包 -有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 +有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 ![动态规划-背包问题](https://img-blog.csdnimg.cn/20210117175428387.jpg) From 74cfeea92040bf6c49279882e298049c02edf43a Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 13 Sep 2021 07:49:13 +0100 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII.md=20C=E8=AF=AD=E8=A8=80=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/0040.组合总和II.md | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 4aab1782..8925ef81 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -392,7 +392,66 @@ var combinationSum2 = function(candidates, target) { } }; ``` +C: +```c +int* path; +int pathTop; +int** ans; +int ansTop; +//记录ans中每一个一维数组的大小 +int* length; +int cmp(const void* a1, const void* a2) { + return *((int*)a1) - *((int*)a2); +} +void backTracking(int* candidates, int candidatesSize, int target, int sum, int startIndex) { + if(sum >= target) { + //若sum等于target,复制当前path进入 + if(sum == target) { + int* tempPath = (int*)malloc(sizeof(int) * pathTop); + int j; + for(j = 0; j < pathTop; j++) { + tempPath[j] = path[j]; + } + length[ansTop] = pathTop; + ans[ansTop++] = tempPath; + } + return ; + } + + int i; + for(i = startIndex; i < candidatesSize; i++) { + //对同一层树中使用过的元素跳过 + if(i > startIndex && candidates[i] == candidates[i-1]) + continue; + path[pathTop++] = candidates[i]; + sum += candidates[i]; + backTracking(candidates, candidatesSize, target, sum, i + 1); + //回溯 + sum -= candidates[i]; + pathTop--; + } +} + +int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes){ + path = (int*)malloc(sizeof(int) * 50); + ans = (int**)malloc(sizeof(int*) * 100); + length = (int*)malloc(sizeof(int) * 100); + pathTop = ansTop = 0; + //快速排序candidates,让相同元素挨到一起 + qsort(candidates, candidatesSize, sizeof(int), cmp); + + backTracking(candidates, candidatesSize, target, 0, 0); + + *returnSize = ansTop; + *returnColumnSizes = (int*)malloc(sizeof(int) * ansTop); + int i; + for(i = 0; i < ansTop; i++) { + (*returnColumnSizes)[i] = length[i]; + } + return ans; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 09293fe395442e5b46fdd24dbf7e58679dbbad4b Mon Sep 17 00:00:00 2001 From: Wen Date: Mon, 13 Sep 2021 19:57:54 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=200257.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?= =?UTF-8?q?.md=20Python3=E8=A7=A3=E6=B3=95=201.=20=E4=BC=98=E5=8C=96Python?= =?UTF-8?q?3=E9=80=92=E5=BD=92=E8=A7=A3=E6=B3=95=202.=20=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?Python3=E8=BF=AD=E4=BB=A3=E8=A7=A3=E6=B3=95=203.=20=E5=B0=BD?= =?UTF-8?q?=E9=87=8F=E9=81=B5=E5=AE=88PEP8=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 62 +++++++++++++++++------ 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 2984427f..bfcbe4b5 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -371,25 +371,57 @@ class Solution { Python: ```Python class Solution: + """二叉树的所有路径 递归法""" + def binaryTreePaths(self, root: TreeNode) -> List[str]: - path=[] - res=[] - def backtrace(root, path): - if not root:return - path.append(root.val) - if (not root.left)and (not root.right): - res.append(path[:]) - ways=[] - if root.left:ways.append(root.left) - if root.right:ways.append(root.right) - for way in ways: - backtrace(way,path) - path.pop() - backtrace(root,path) - return ["->".join(list(map(str,i))) for i in res] + path, result = '', [] + self.traversal(root, path, result) + return result + + def traversal(self, cur: TreeNode, path: List, result: List): + path += str(cur.val) + # 如果当前节点为叶子节点,添加路径到结果中 + if not (cur.left or cur.right): + result.append(path) + return + + if cur.left: + self.traversal(cur.left, path + '->', result) + + if cur.right: + self.traversal(cur.right, path + '->', result) ``` +```python +from collections import deque + + +class Solution: + """二叉树的所有路径 迭代法""" + + def binaryTreePaths(self, root: TreeNode) -> List[str]: + # 题目中节点数至少为1 + stack, path_st, result = deque([root]), deque(), [] + path_st.append(str(root.val)) + + while stack: + cur = stack.pop() + path = path_st.pop() + # 如果当前节点为叶子节点,添加路径到结果中 + if not (cur.left or cur.right): + result.append(path) + if cur.right: + stack.append(cur.right) + path_st.append(path + '->' + str(cur.right.val)) + if cur.left: + stack.append(cur.left) + path_st.append(path + '->' + str(cur.left.val)) + + return result +``` + + Go: ```go func binaryTreePaths(root *TreeNode) []string { From f46c111d9e0f966a618436e0d054d21951b22cb9 Mon Sep 17 00:00:00 2001 From: Wen Date: Tue, 14 Sep 2021 19:39:46 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Python3=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=201.=20=E4=BF=AE=E5=A4=8D=E8=AF=AD=E6=B3=95=E9=94=99?= =?UTF-8?q?=E8=AF=AF=202.=20=E4=BC=98=E5=8C=96=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0654.最大二叉树.md | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index 1a6d39af..b644a0a3 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -258,16 +258,30 @@ class Solution { ## Python ```python -//递归法 class Solution: + """最大二叉树 递归法""" + def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: - if not nums: return None //终止条件 - root = TreeNode(max(nums)) //新建节点 - p = nums.index(root.val) //找到最大值位置 - if p > 0: //保证有左子树 - root.left = self.constructMaximumBinaryTree(nums[:p]) //递归 - if p < len(nums): //保证有右子树 - root.right = self.constructMaximumBinaryTree(nums[p+1:]) //递归 + return self.traversal(nums, 0, len(nums)) + + def traversal(self, nums: List[int], begin: int, end: int) -> TreeNode: + # 列表长度为0时返回空节点 + if begin == end: + return None + + # 找到最大的值和其对应的下标 + max_index = begin + for i in range(begin, end): + if nums[i] > nums[max_index]: + max_index = i + + # 构建当前节点 + root = TreeNode(nums[max_index]) + + # 递归构建左右子树 + root.left = self.traversal(nums, begin, max_index) + root.right = self.traversal(nums, max_index + 1, end) + return root ``` From 35023c59947f11ed789c7ea32037dd793240f7a8 Mon Sep 17 00:00:00 2001 From: Jerry-306 <82520819+Jerry-306@users.noreply.github.com> Date: Tue, 14 Sep 2021 20:09:16 +0800 Subject: [PATCH 6/6] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 4993bede..07cf0433 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -321,7 +321,7 @@ const findTargetSumWays = (nums, target) => { const sum = nums.reduce((a, b) => a+b); - if(target > sum) { + if(Math.abs(target) > sum) { return 0; }