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 01/15] =?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 02/15] =?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 03/15] =?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 7b785ab004eaa2635980a96ee543cbe65f4852de Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 13 Jan 2022 22:14:12 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 650f2a99..31e19b10 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -264,6 +264,27 @@ var canConstruct = function(ransomNote, magazine) { }; ``` +TypeScript: + +```typescript +function canConstruct(ransomNote: string, magazine: string): boolean { + let helperArr: number[] = new Array(26).fill(0); + let base: number = 'a'.charCodeAt(0); + let index: number; + for (let i = 0, length = magazine.length; i < length; i++) { + helperArr[magazine[i].charCodeAt(0) - base]++; + } + for (let i = 0, length = ransomNote.length; i < length; i++) { + index = ransomNote[i].charCodeAt(0) - base; + helperArr[index]--; + if (helperArr[index] < 0) { + return false; + } + } + return true; +}; +``` + PHP: ```php From 9d59aab89e5a61f752e67f3c969260a98a95fded Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 18:26:34 +0800 Subject: [PATCH 05/15] =?UTF-8?q?0028.=E5=AE=9E=E7=8E=B0strStr=EF=BC=9A?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0028.实现strStr.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index f0b56719..c23f5558 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -259,7 +259,7 @@ void getNext(int* next, const string& s) 然后还要对next数组进行初始化赋值,如下: -``` +```cpp int j = -1; next[0] = j; ``` @@ -278,8 +278,8 @@ next[i] 表示 i(包括i)之前最长相等的前后缀长度(其实就是 所以遍历模式串s的循环下标i 要从 1开始,代码如下: -``` -for(int i = 1; i < s.size(); i++) { +```cpp +for (int i = 1; i < s.size(); i++) { ``` 如果 s[i] 与 s[j+1]不相同,也就是遇到 前后缀末尾不相同的情况,就要向前回退。 @@ -292,7 +292,7 @@ next[j]就是记录着j(包括j)之前的子串的相同前后缀的长度 所以,处理前后缀不相同的情况代码如下: -``` +```cpp while (j >= 0 && s[i] != s[j + 1]) { // 前后缀不相同了     j = next[j]; // 向前回退 } @@ -300,7 +300,7 @@ while (j >= 0 && s[i] != s[j + 1]) { // 前后缀不相同了 3. 处理前后缀相同的情况 -如果s[i] 与 s[j + 1] 相同,那么就同时向后移动i 和j 说明找到了相同的前后缀,同时还要将j(前缀的长度)赋给next[i], 因为next[i]要记录相同前后缀的长度。 +如果 s[i] 与 s[j + 1] 相同,那么就同时向后移动i 和j 说明找到了相同的前后缀,同时还要将j(前缀的长度)赋给next[i], 因为next[i]要记录相同前后缀的长度。 代码如下: @@ -346,7 +346,7 @@ void getNext(int* next, const string& s){ i就从0开始,遍历文本串,代码如下: -``` +```cpp for (int i = 0; i < s.size(); i++)  ``` @@ -356,7 +356,7 @@ for (int i = 0; i < s.size(); i++)  代码如下: -``` +```cpp while(j >= 0 && s[i] != t[j + 1]) {     j = next[j]; } @@ -364,7 +364,7 @@ while(j >= 0 && s[i] != t[j + 1]) { 如果 s[i] 与 t[j + 1] 相同,那么i 和 j 同时向后移动, 代码如下: -``` +```cpp if (s[i] == t[j + 1]) {     j++; // i的增加在for循环里 } @@ -376,7 +376,7 @@ if (s[i] == t[j + 1]) { 代码如下: -``` +```cpp if (j == (t.size() - 1) ) {     return (i - t.size() + 1); } From 57af6f6a4b8941e5422c8387ab8591501c5f28f4 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 19:53:51 +0800 Subject: [PATCH 06/15] =?UTF-8?q?0027.=E7=A7=BB=E9=99=A4=E5=85=83=E7=B4=A0?= =?UTF-8?q?=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 99990302..d69f2bcf 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -42,7 +42,7 @@ ![27.移除元素-暴力解法](https://tva1.sinaimg.cn/large/008eGmZEly1gntrc7x9tjg30du09m1ky.gif) -很明显暴力解法的时间复杂度是O(n^2),这道题目暴力解法在leetcode上是可以过的。 +很明显暴力解法的时间复杂度是$O(n^2)$,这道题目暴力解法在leetcode上是可以过的。 代码如下: From 71ada4737a7cc00de5403c43f73a7d6c824eb7b2 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 19:54:34 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=E6=A0=88=E4=B8=8E=E9=98=9F=E5=88=97?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=EF=BC=9A=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/栈与队列理论基础.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/栈与队列理论基础.md b/problems/栈与队列理论基础.md index b9811b29..44fcbdd5 100644 --- a/problems/栈与队列理论基础.md +++ b/problems/栈与队列理论基础.md @@ -67,7 +67,7 @@ deque是一个双向队列,只要封住一段,只开通另一端就可以实 我们也可以指定vector为栈的底层实现,初始化语句如下: -``` +```cpp std::stack > third; // 使用vector为底层容器的栈 ``` @@ -77,7 +77,7 @@ std::stack > third; // 使用vector为底层容器的栈 也可以指定list 为起底层实现,初始化queue的语句如下: -``` +```cpp std::queue> third; // 定义以list为底层容器的队列 ``` From 13601e89abdff1a8b196645f647db551d8f4154e Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 20:13:03 +0800 Subject: [PATCH 08/15] =?UTF-8?q?0225.=E7=94=A8=E9=98=9F=E5=88=97=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=A0=88=E3=80=810232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 2 +- problems/0232.用栈实现队列.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index fdb544a6..524ca329 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -46,7 +46,7 @@ 模拟的队列执行语句如下: -``` +```cpp queue.push(1); queue.push(2); queue.pop(); // 注意弹出的操作 diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 4edba2f2..0e4fce28 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -21,7 +21,7 @@ empty() -- 返回队列是否为空。 示例: -``` +```cpp MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); From cd4529c8ff7c4570a4541988c1cf3a76e3586150 Mon Sep 17 00:00:00 2001 From: bin3826246 <59920177+bin3826246@users.noreply.github.com> Date: Fri, 14 Jan 2022 19:15:00 +0800 Subject: [PATCH 09/15] =?UTF-8?q?Create=20=E9=9D=A2=E8=AF=95=E9=A2=98=2002?= =?UTF-8?q?.07.=20=E8=A7=A3=E6=B3=95=E6=9B=B4=E6=96=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题 02.07. 解法更新.md | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 problems/面试题 02.07. 解法更新.md diff --git a/problems/面试题 02.07. 解法更新.md b/problems/面试题 02.07. 解法更新.md new file mode 100644 index 00000000..119f875c --- /dev/null +++ b/problems/面试题 02.07. 解法更新.md @@ -0,0 +1,41 @@ +# 双指针,不计算链表长度 +设置指向headA和headB的指针pa、pb,分别遍历两个链表,每次循环同时更新pa和pb。 +* 当链表A遍历完之后,即pa为空时,将pa指向headB; +* 当链表B遍历完之后,即pa为空时,将pb指向headA; +* 当pa与pb相等时,即指向同一个节点,该节点即为相交起始节点。 +* 若链表不相交,则pa、pb同时为空时退出循环,即如果链表不相交,pa与pb在遍历过全部节点后同时指向结尾空节点,此时退出循环,返回空。 +# 证明思路 +设链表A不相交部分长度为a,链表B不相交部分长度为b,两个链表相交部分长度为c。
+在pa指向链表A时,即pa为空之前,pa经过链表A不相交部分和相交部分,走过的长度为a+c;
+pa指向链表B后,在移动相交节点之前经过链表B不相交部分,走过的长度为b,总合为a+c+b。
+同理,pb走过长度的总合为b+c+a。二者相等,即pa与pb可同时到达相交起始节点。
+该方法可避免计算具体链表长度。 +```cpp +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + //链表为空时,返回空指针 + if(headA == nullptr || headB == nullptr) return nullptr; + ListNode* pa = headA; + ListNode* pb = headB; + //pa与pb在遍历过全部节点后,同时指向结尾空节点时退出循环 + while(pa != nullptr || pb != nullptr){ + //pa为空时,将pa指向headB + if(pa == nullptr){ + pa = headB; + } + //pa为空时,将pb指向headA + if(pb == nullptr){ + pb = headA; + } + //pa == pb时,返回相交起始节点 + if(pa == pb){ + return pa; + } + pa = pa->next; + pb = pb->next; + } + return nullptr; + } +}; +``` From 570022f6666b633891bb9e590488529d5238d254 Mon Sep 17 00:00:00 2001 From: bin3826246 <59920177+bin3826246@users.noreply.github.com> Date: Fri, 14 Jan 2022 19:20:36 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=9802.07=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0cpp=E5=8F=8C=E6=8C=87=E9=92=88=E6=96=B0?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 双指针新解法,不需计算链表长度 --- problems/面试题 02.07. 解法更新.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/面试题 02.07. 解法更新.md b/problems/面试题 02.07. 解法更新.md index 119f875c..6115d02e 100644 --- a/problems/面试题 02.07. 解法更新.md +++ b/problems/面试题 02.07. 解法更新.md @@ -28,7 +28,7 @@ public: if(pb == nullptr){ pb = headA; } - //pa == pb时,返回相交起始节点 + //pa与pb相等时,返回相交起始节点 if(pa == pb){ return pa; } From 2ea72e3e4345e0de2feed93b4a74dd9bbbc25b68 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 14 Jan 2022 21:59:34 +0800 Subject: [PATCH 11/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880015.=E4=B8=89?= =?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/0015.三数之和.md | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index c78ab06d..8992c5f4 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -332,7 +332,43 @@ var threeSum = function(nums) { return res; }; ``` +TypeScript: +```typescript +function threeSum(nums: number[]): number[][] { + nums.sort((a, b) => a - b); + let length = nums.length; + let left: number = 0, + right: number = length - 1; + let resArr: number[][] = []; + for (let i = 0; i < length; i++) { + if (i > 0 && nums[i] === nums[i - 1]) { + continue; + } + left = i + 1; + right = length - 1; + while (left < right) { + let total: number = nums[i] + nums[left] + nums[right]; + if (total === 0) { + resArr.push([nums[i], nums[left], nums[right]]); + left++; + right--; + while (nums[right] === nums[right + 1]) { + right--; + } + while (nums[left] === nums[left - 1]) { + left++; + } + } else if (total < 0) { + left++; + } else { + right--; + } + } + } + return resArr; +}; +``` ruby: ```ruby From 12bf8ae05b176816a21cfd741b2274bfada14aae Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 14 Jan 2022 22:36:55 +0800 Subject: [PATCH 12/15] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880018.=E5=9B=9B?= =?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/0018.四数之和.md | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index b94ebeef..dae8636b 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -311,7 +311,49 @@ var fourSum = function(nums, target) { }; ``` +TypeScript: + +```typescript +function fourSum(nums: number[], target: number): number[][] { + nums.sort((a, b) => a - b); + let first: number = 0, + second: number, + third: number, + fourth: number; + let length: number = nums.length; + let resArr: number[][] = []; + for (; first < length; first++) { + if (first > 0 && nums[first] === nums[first - 1]) { + continue; + } + for (second = first + 1; second < length; second++) { + if ((second - first) > 1 && nums[second] === nums[second - 1]) { + continue; + } + third = second + 1; + fourth = length - 1; + while (third < fourth) { + let total: number = nums[first] + nums[second] + nums[third] + nums[fourth]; + if (total === target) { + resArr.push([nums[first], nums[second], nums[third], nums[fourth]]); + third++; + fourth--; + while (nums[third] === nums[third - 1]) third++; + while (nums[fourth] === nums[fourth + 1]) fourth--; + } else if (total < target) { + third++; + } else { + fourth--; + } + } + } + } + return resArr; +}; +``` + 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 13/15] =?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 14/15] =?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 15/15] =?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_二叉树的中序遍历