From 6586f9a8299138d0e7fcb6135f68c75d67a36bdc Mon Sep 17 00:00:00 2001 From: weiting-cn <2254912@qq.com> Date: Thu, 13 Jan 2022 17:06:28 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E9=81=8D=E5=8E=86=E8=83=8C=E5=8C=85=E5=AE=B9?= =?UTF-8?q?=E9=87=8F=E6=97=B6=E7=9A=84=E8=BE=B9=E7=95=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包问题理论基础完全背包.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 3cc8557c..f79310b8 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -52,7 +52,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 ```CPP // 先遍历物品,再遍历背包 for(int i = 0; i < weight.size(); i++) { // 遍历物品 - for(int j = weight[i]; j < bagWeight ; j++) { // 遍历背包容量 + for(int j = weight[i]; j <= bagWeight ; j++) { // 遍历背包容量 dp[j] = max(dp[j], dp[j - weight[i]] + value[i]); } From 97a0b8d46b9ffaf1174cde3ea01d2d2afda6a9fc Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 13 Jan 2022 19:18:32 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 37c95736..b337d1e2 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -186,6 +186,24 @@ var twoSum = function (nums, target) { }; ``` +TypeScript: + +```typescript +function twoSum(nums: number[], target: number): number[] { + let helperMap: Map = new Map(); + let index: number | undefined; + let resArr: number[] = []; + for (let i = 0, length = nums.length; i < length; i++) { + index = helperMap.get(target - nums[i]); + if (index !== undefined) { + resArr = [i, index]; + } + helperMap.set(nums[i], i); + } + return resArr; +}; +``` + php ```php From ed2f56fa28817edbfccd3d3bc38f07eac71a5f2b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 13 Jan 2022 20:32:18 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880454.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II.md=EF=BC=89:=E5=A2=9E=E5=8A=A0typ?= =?UTF-8?q?escript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 6853354c..352f693b 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -139,7 +139,7 @@ class Solution(object): return count -``` +``` Go: ```go @@ -192,8 +192,33 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) { }; ``` +TypeScript: + +```typescript +function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { + let helperMap: Map = new Map(); + let resNum: number = 0; + let tempVal: number | undefined; + for (let i of nums1) { + for (let j of nums2) { + tempVal = helperMap.get(i + j); + helperMap.set(i + j, tempVal ? tempVal + 1 : 1); + } + } + for (let k of nums3) { + for (let l of nums4) { + tempVal = helperMap.get(0 - (k + l)); + if (tempVal) { + resNum += tempVal; + } + } + } + return resNum; +}; +``` PHP: + ```php class Solution { /** From b124befd1263689d1078a3417cd10cbf5ce2aebe Mon Sep 17 00:00:00 2001 From: chenhaoran14 <2718827494@qq.com> Date: Thu, 20 Jan 2022 01:15:23 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=AF=B9Java=E7=9A=84=E5=A4=A7=E9=A1=B6?= =?UTF-8?q?=E5=A0=86=E5=92=8C=E5=B0=8F=E9=A1=B6=E5=A0=86=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BA=86=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 0b8fd2d7..8bd774e9 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -142,7 +142,7 @@ class Solution { Set> entries = map.entrySet(); // 根据map的value值正序排,相当于一个小顶堆 - PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue()); + PriorityQueue> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue()); for (Map.Entry entry : entries) { queue.offer(entry); if (queue.size() > k) { From baff8206d5a6bbb0a66b0db938fca36139bd0e10 Mon Sep 17 00:00:00 2001 From: weiting-cn <2254912@qq.com> Date: Thu, 20 Jan 2022 11:36:17 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20(0583.=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C.md)=20:=20=E5=A2=9E=E5=8A=A0=E8=A7=A3?= =?UTF-8?q?=E9=A2=98=E6=80=9D=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0583.两个字符串的删除操作.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index d2f7d84b..53c1a125 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -18,6 +18,8 @@ ## 思路 +### 动态规划一 + 本题和[动态规划:115.不同的子序列](https://programmercarl.com/0115.不同的子序列.html)相比,其实就是两个字符串都可以删除了,情况虽说复杂一些,但整体思路是不变的。 这次是两个字符串可以相互删了,这种题目也知道用动态规划的思路来解,动规五部曲,分析如下: @@ -98,6 +100,29 @@ public: ``` +### 动态规划二 + +本题和[动态规划:1143.最长公共子序列](https://programmercarl.com/1143.最长公共子序列.html)基本相同,只要求出两个字符串的最长公共子序列长度即可,那么除了最长公共子序列之外的字符都是必须删除的,最后用两个字符串的总长度减去两个最长公共子序列的长度就是删除的最少步数。 + +代码如下: + +```CPP +class Solution { +public: + int minDistance(string word1, string word2) { + vector> dp(word1.size()+1, vector(word2.size()+1, 0)); + for (int i=1; i<=word1.size(); i++){ + for (int j=1; j<=word2.size(); j++){ + if (word1[i-1] == word2[j-1]) dp[i][j] = dp[i-1][j-1] + 1; + else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); + } + } + return word1.size()+word2.size()-dp[word1.size()][word2.size()]*2; + } +}; + +``` + ## 其他语言版本 From 43eb0269d3dbbf47794eeee8690bcebae3d523db Mon Sep 17 00:00:00 2001 From: chenhaoran14 <2718827494@qq.com> Date: Thu, 20 Jan 2022 12:04:49 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=AF=B9Java=E7=89=88=E6=9C=AC=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E6=8E=92=E5=88=97=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的递归遍历.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 45b576e7..4beed650 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -116,19 +116,19 @@ Java: ```Java // 前序遍历·递归·LC144_二叉树的前序遍历 class Solution { - ArrayList preOrderReverse(TreeNode root) { - ArrayList result = new ArrayList(); - preOrder(root, result); + public List preorderTraversal(TreeNode root) { + List result = new ArrayList(); + preorder(root, result); return result; } - void preOrder(TreeNode root, ArrayList result) { + public void preorder(TreeNode root, List result) { if (root == null) { return; } - result.add(root.val); // 注意这一句 - preOrder(root.left, result); - preOrder(root.right, result); + result.add(root.val); + preorder(root.left, result); + preorder(root.right, result); } } // 中序遍历·递归·LC94_二叉树的中序遍历