From b2f6e0c272610e174e20e35a7cdac5195ab65ac8 Mon Sep 17 00:00:00 2001 From: Tickylime <35446132+Tickylime@users.noreply.github.com> Date: Sun, 19 Dec 2021 23:00:56 +0800 Subject: [PATCH 01/22] =?UTF-8?q?Update=200707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 145efa18..95e90ac6 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -706,6 +706,9 @@ func (this *MyLinkedList) AddAtIndex(index int, val int) { head = head.Next index-- } + if index > 0 { + return + } node := &Node{ Val: val, //node.Next = MyLinkedList[index] From 23725675a81de764c53dafd5f02c1e9bfdcb4dd2 Mon Sep 17 00:00:00 2001 From: db <39407623+IcePigZDB@users.noreply.github.com> Date: Mon, 20 Dec 2021 15:45:51 +0800 Subject: [PATCH 02/22] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新错别字 --- problems/0102.二叉树的层序遍历.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 04b980c6..270dce6f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -12,7 +12,7 @@ * 107.二叉树的层次遍历II * 199.二叉树的右视图 * 637.二叉树的层平均值 -* 429.N叉树的前序遍历 +* 429.N叉树的层序遍历 * 515.在每个树行中找最大值 * 116.填充每个节点的下一个右侧节点指针 * 117.填充每个节点的下一个右侧节点指针II From f6be40f9d78e1fd46a18d78c3d9ccb9edd61dbe1 Mon Sep 17 00:00:00 2001 From: mengyuan Date: Mon, 20 Dec 2021 19:44:54 +0800 Subject: [PATCH 03/22] =?UTF-8?q?update:=200376.=E6=91=86=E5=8A=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97=20js=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?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/0376.摆动序列.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 3432ca53..b7293635 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -261,6 +261,7 @@ func wiggleMaxLength(nums []int) int { ``` ### Javascript +**贪心** ```Javascript var wiggleMaxLength = function(nums) { if(nums.length <= 1) return nums.length @@ -277,6 +278,25 @@ var wiggleMaxLength = function(nums) { return result }; ``` +**动态规划** +```Javascript +var wiggleMaxLength = function(nums) { + if (nums.length === 1) return 1; + // 考虑前i个数,当第i个值作为峰谷时的情况(则第i-1是峰顶) + let down = 1; + // 考虑前i个数,当第i个值作为峰顶时的情况(则第i-1是峰谷) + let up = 1; + for (let i = 1; i < nums.length; i++) { + if (nums[i] < nums[i - 1]) { + down = Math.max(up + 1, down); + } + if (nums[i] > nums[i - 1]) { + up = Math.max(down + 1, up) + } + } + return Math.max(down, up); +}; +``` -----------------------
From 581dbd64f020e50c6f508131c588ab82f8abd9d1 Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Tue, 21 Dec 2021 09:28:32 +0800 Subject: [PATCH 04/22] =?UTF-8?q?=E8=83=8C=E5=8C=85=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C=E5=85=A8=E8=83=8C?= =?UTF-8?q?=E5=8C=85=EF=BC=9A=E4=BF=AE=E6=94=B9Java=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础完全背包.md | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 27d820bc..3cc8557c 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -177,41 +177,41 @@ int main() { Java: ```java - //先遍历物品,再遍历背包 - private static void testCompletePack(){ - int[] weight = {1, 3, 4}; - int[] value = {15, 20, 30}; - int bagWeight = 4; - int[] dp = new int[bagWeight + 1]; - for (int i = 0; i < weight.length; i++){ - for (int j = 1; j <= bagWeight; j++){ - if (j - weight[i] >= 0){ - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); - } +//先遍历物品,再遍历背包 +private static void testCompletePack(){ + int[] weight = {1, 3, 4}; + int[] value = {15, 20, 30}; + int bagWeight = 4; + int[] dp = new int[bagWeight + 1]; + for (int i = 0; i < weight.length; i++){ + for (int j = 1; j <= bagWeight; j++){ + if (j - weight[i] >= 0){ + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); } } - for (int maxValue : dp){ - System.out.println(maxValue + " "); - } } + for (int maxValue : dp){ + System.out.println(maxValue + " "); + } +} - //先遍历背包,再遍历物品 - private static void testCompletePackAnotherWay(){ - int[] weight = {1, 3, 4}; - int[] value = {15, 20, 30}; - int bagWeight = 4; - int[] dp = new int[bagWeight + 1]; - for (int i = 1; i <= bagWeight; i++){ - for (int j = 0; j < weight.length; j++){ - if (i - weight[j] >= 0){ - dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]); - } +//先遍历背包,再遍历物品 +private static void testCompletePackAnotherWay(){ + int[] weight = {1, 3, 4}; + int[] value = {15, 20, 30}; + int bagWeight = 4; + int[] dp = new int[bagWeight + 1]; + for (int i = 1; i <= bagWeight; i++){ + for (int j = 0; j < weight.length; j++){ + if (i - weight[j] >= 0){ + dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]); } } - for (int maxValue : dp){ - System.out.println(maxValue + " "); - } } + for (int maxValue : dp){ + System.out.println(maxValue + " "); + } +} ``` From 91db8652e7f38dc968291d639740bfc6f8d58164 Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Tue, 21 Dec 2021 10:55:00 +0800 Subject: [PATCH 05/22] =?UTF-8?q?0322.=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2?= =?UTF-8?q?=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0322.零钱兑换.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 637e68eb..8f3438af 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -81,7 +81,7 @@ dp[0] = 0; 4. 确定遍历顺序 -本题求钱币最小个数,**那么钱币有顺序和没有顺序都可以,都不影响钱币的最小个数。**。 +本题求钱币最小个数,**那么钱币有顺序和没有顺序都可以,都不影响钱币的最小个数**。 所以本题并不强调集合是组合还是排列。 @@ -170,7 +170,7 @@ public: 这也是我为什么要先讲518.零钱兑换II 然后再讲本题即:322.零钱兑换,这是Carl的良苦用心那。 -相信大家看完之后,对背包问题中的遍历顺序又了更深的理解了。 +相信大家看完之后,对背包问题中的遍历顺序有更深的理解了。 From 0b5e79d826b9eca2f4ef53e9ddd9dc540e288ca9 Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Tue, 21 Dec 2021 11:30:52 +0800 Subject: [PATCH 06/22] =?UTF-8?q?279.=E5=AE=8C=E5=85=A8=E5=B9=B3=E6=96=B9?= =?UTF-8?q?=E6=95=B0=EF=BC=9A=E4=BF=AE=E6=94=B9=E5=8A=A8=E8=A7=84=E4=BA=94?= =?UTF-8?q?=E9=83=A8=E6=9B=B2=E4=B8=AD=20i=E5=92=8Cj=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BD=BF=E7=94=A8=20=E5=92=8C=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=96=87=E5=AD=97=E8=A1=A8=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 86c3170e..7bc0c2f7 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -40,7 +40,7 @@ 1. 确定dp数组(dp table)以及下标的含义 -**dp[i]:和为i的完全平方数的最少数量为dp[i]** +**dp[j]:和为j的完全平方数的最少数量为dp[j]** 2. 确定递推公式 @@ -58,7 +58,7 @@ dp[0]表示 和为0的完全平方数的最小数量,那么dp[0]一定是0。 非0下标的dp[j]应该是多少呢? -从递归公式dp[j] = min(dp[j - i * i] + 1, dp[j]);中可以看出每次dp[j]都要选最小的,**所以非0下标的dp[i]一定要初始为最大值,这样dp[j]在递推的时候才不会被初始值覆盖**。 +从递归公式dp[j] = min(dp[j - i * i] + 1, dp[j]);中可以看出每次dp[j]都要选最小的,**所以非0下标的dp[j]一定要初始为最大值,这样dp[j]在递推的时候才不会被初始值覆盖**。 4. 确定遍历顺序 @@ -70,9 +70,9 @@ dp[0]表示 和为0的完全平方数的最小数量,那么dp[0]一定是0。 在[动态规划:322. 零钱兑换](https://programmercarl.com/0322.零钱兑换.html)中我们就深入探讨了这个问题,本题也是一样的,是求最小数! -**所以本题外层for遍历背包,里层for遍历物品,还是外层for遍历物品,内层for遍历背包,都是可以的!** +**所以本题外层for遍历背包,内层for遍历物品,还是外层for遍历物品,内层for遍历背包,都是可以的!** -我这里先给出外层遍历背包,里层遍历物品的代码: +我这里先给出外层遍历背包,内层遍历物品的代码: ```CPP vector dp(n + 1, INT_MAX); From fbddab5769b40824d9dca8c35c97f7f66707939d Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Tue, 21 Dec 2021 11:39:10 +0800 Subject: [PATCH 07/22] =?UTF-8?q?20210204=E5=8A=A8=E8=A7=84=E5=91=A8?= =?UTF-8?q?=E6=9C=AB=E6=80=BB=E7=BB=93=EF=BC=9A=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/周总结/20210204动规周末总结.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/周总结/20210204动规周末总结.md b/problems/周总结/20210204动规周末总结.md index 31009298..9a64c152 100644 --- a/problems/周总结/20210204动规周末总结.md +++ b/problems/周总结/20210204动规周末总结.md @@ -85,7 +85,7 @@ public: 关键看遍历顺序。 -本题求钱币最小个数,**那么钱币有顺序和没有顺序都可以,都不影响钱币的最小个数。**。 +本题求钱币最小个数,**那么钱币有顺序和没有顺序都可以,都不影响钱币的最小个数**。 所以本题并不强调集合是组合还是排列。 From f460b46a261fd5a75a8bad175d5f41d30b632e87 Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Wed, 22 Dec 2021 10:02:54 +0800 Subject: [PATCH 08/22] =?UTF-8?q?0139.=E5=8D=95=E8=AF=8D=E6=8B=86=E5=88=86?= =?UTF-8?q?=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0139.单词拆分.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index db80d4d4..e24ffc1a 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -220,7 +220,7 @@ public: 但因为分割子串的特殊性,遍历背包放在外循环,将遍历物品放在内循环更方便一些。 -本题其实递推公式都不是重点,遍历顺序才是重点,如果我直接把代码贴出来,估计同学们也会想两个for循环的顺序理所当然就是这样,甚至都不会想为什么遍历背包的for循环为什么在外层。 +本题其实递推公式都不是重点,遍历顺序才是重点,如果我直接把代码贴出来,估计同学们也会想两个for循环的顺序理所当然就是这样,甚至都不会想为什么遍历背包的for循环在外层。 不分析透彻不是Carl的风格啊,哈哈 From f4b5c9198d5fe5ab8e8d41c334a406ece7bca0df Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Wed, 22 Dec 2021 10:10:45 +0800 Subject: [PATCH 09/22] =?UTF-8?q?=E8=83=8C=E5=8C=85=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=A4=9A=E9=87=8D=E8=83=8C?= =?UTF-8?q?=E5=8C=85=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94=E8=AF=AF?= 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 55f7cf96..82418188 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -7,7 +7,7 @@ # 动态规划:关于多重背包,你该了解这些! -之前我们已经体统的讲解了01背包和完全背包,如果没有看过的录友,建议先把如下三篇文章仔细阅读一波。 +之前我们已经系统的讲解了01背包和完全背包,如果没有看过的录友,建议先把如下三篇文章仔细阅读一波。 * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) From 1a08a66586a5f3f5f2362e8b4e4e05fc1204140f Mon Sep 17 00:00:00 2001 From: qyg <1600314850@qq.com> Date: Thu, 23 Dec 2021 10:45:41 +0800 Subject: [PATCH 10/22] =?UTF-8?q?20210225=E5=8A=A8=E8=A7=84=E5=91=A8?= =?UTF-8?q?=E6=9C=AB=E6=80=BB=E7=BB=93=EF=BC=9A=E8=B0=83=E6=95=B4=E7=AC=94?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/周总结/20210225动规周末总结.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/周总结/20210225动规周末总结.md b/problems/周总结/20210225动规周末总结.md index 4ea92266..0bf9dbdb 100644 --- a/problems/周总结/20210225动规周末总结.md +++ b/problems/周总结/20210225动规周末总结.md @@ -3,7 +3,7 @@ ## 周一 -[动态规划:开始打家劫舍!](https://programmercarl.com/0198.打家劫舍.html)中就是给一个数组相邻之间不能连着偷,如果偷才能得到最大金钱。 +[动态规划:开始打家劫舍!](https://programmercarl.com/0198.打家劫舍.html)中就是给一个数组相邻之间不能连着偷,如何偷才能得到最大金钱。 1. 确定dp数组含义 @@ -65,7 +65,7 @@ dp[1] = max(nums[0], nums[1]); ## 周三 -[动态规划:还要打家劫舍!](https://programmercarl.com/0337.打家劫舍III.html)这次是在一颗二叉树上打家劫舍了,条件还是一样的,相临的不能偷。 +[动态规划:还要打家劫舍!](https://programmercarl.com/0337.打家劫舍III.html)这次是在一棵二叉树上打家劫舍了,条件还是一样的,相临的不能偷。 这道题目是树形DP的入门题目,其实树形DP其实就是在树上进行递推公式的推导,没有什么神秘的。 @@ -191,12 +191,12 @@ return {val2, val1}; ## 周四 -[动态规划:买卖股票的最佳时机](https://programmercarl.com/0121.买卖股票的最佳时机.html) 一段时间,只能买买一次,问最大收益。 +[动态规划:买卖股票的最佳时机](https://programmercarl.com/0121.买卖股票的最佳时机.html) 一段时间,只能买卖一次,问最大收益。 -这里我给出了三中解法: +这里我给出了三种解法: 暴力解法代码: -``` +```CPP class Solution { public: int maxProfit(vector& prices) { From 7b7251f1e4bb8125c7f88030a2c93db0c0ceace3 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 13:56:43 +0800 Subject: [PATCH 11/22] =?UTF-8?q?0017.=E7=94=B5=E8=AF=9D=E5=8F=B7=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88=EF=BC=9A=E2=80=9C?= =?UTF-8?q?=E4=BA=8C=E4=BD=8D=E6=95=B0=E7=BB=84=E2=80=9D=20->=20=E2=80=9C?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E6=95=B0=E7=BB=84=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 146493e9..7040182f 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -37,7 +37,7 @@ ## 数字和字母如何映射 -可以使用map或者定义一个二位数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下: +可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下: ```cpp const string letterMap[10] = { From de0465b773693fa6c19a4da73a008cfa287ce812 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 14:02:02 +0800 Subject: [PATCH 12/22] =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E6=80=BB=E7=BB=93?= =?UTF-8?q?=EF=BC=9A=E2=80=9C=E6=A3=8B=E7=89=8C=E2=80=9D=20->=20=E2=80=9C?= =?UTF-8?q?=E6=A3=8B=E7=9B=98=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/回溯总结.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/problems/回溯总结.md b/problems/回溯总结.md index 2fefeb04..cff41f24 100644 --- a/problems/回溯总结.md +++ b/problems/回溯总结.md @@ -331,7 +331,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。 -下面我用一个3 * 3 的棋牌,将搜索过程抽象为一颗树,如图: +下面我用一个3 * 3 的棋盘,将搜索过程抽象为一颗树,如图: ![51.N皇后](https://img-blog.csdnimg.cn/20201118225433127.png) @@ -437,20 +437,5 @@ N皇后问题分析: **回溯算法系列正式结束,新的系列终将开始,录友们准备开启新的征程!** - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - - -----------------------
From 90aa75c57300eaa7eea9a87d268cb9b0ad8a8553 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 18:45:54 +0800 Subject: [PATCH 13/22] =?UTF-8?q?0077.=E7=BB=84=E5=90=88=E3=80=810077.?= =?UTF-8?q?=E7=BB=84=E5=90=88=E4=BC=98=E5=8C=96=EF=BC=9ASwift=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=87=8F=E5=B0=91=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 10 +++++----- problems/0077.组合优化.md | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 2bf856df..4ec154e1 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -627,7 +627,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ func combine(_ n: Int, _ k: Int) -> [[Int]] { var path = [Int]() var result = [[Int]]() - func backtracking(_ n: Int, _ k: Int, _ startIndex: Int) { + func backtracking(start: Int) { // 结束条件,并收集结果 if path.count == k { result.append(path) @@ -638,15 +638,15 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { // let end = n // 剪枝优化 let end = n - (k - path.count) + 1 - guard startIndex <= end else { return } - for i in startIndex ... end { + guard start <= end else { return } + for i in start ... end { path.append(i) // 处理结点 - backtracking(n, k, i + 1) // 递归 + backtracking(start: i + 1) // 递归 path.removeLast() // 回溯 } } - backtracking(n, k, 1) + backtracking(start: 1) return result } ``` diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index b713bdcd..5fe56e82 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -300,7 +300,7 @@ Swift: func combine(_ n: Int, _ k: Int) -> [[Int]] { var path = [Int]() var result = [[Int]]() - func backtracking(_ n: Int, _ k: Int, _ startIndex: Int) { + func backtracking(start: Int) { // 结束条件,并收集结果 if path.count == k { result.append(path) @@ -311,15 +311,15 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { // let end = n // 剪枝优化 let end = n - (k - path.count) + 1 - guard startIndex <= end else { return } - for i in startIndex ... end { + guard start <= end else { return } + for i in start ... end { path.append(i) // 处理结点 - backtracking(n, k, i + 1) // 递归 + backtracking(start: i + 1) // 递归 path.removeLast() // 回溯 } } - backtracking(n, k, 1) + backtracking(start: 1) return result } ``` From 7c4bafe9b1d4c83a36520e3cc9c37d9c103a3651 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 20:06:04 +0800 Subject: [PATCH 14/22] =?UTF-8?q?0216.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C?= =?UTF-8?q?III=EF=BC=9ASwift=E5=AE=9E=E7=8E=B0=E5=87=8F=E5=B0=91=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0216.组合总和III.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index a411117c..26c630b9 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -462,7 +462,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { var result = [[Int]]() var path = [Int]() - func backtracking(sum: Int, startIndex: Int) { + func backtracking(sum: Int, start: Int) { // 剪枝 if sum > targetSum { return } // 终止条件 @@ -474,16 +474,16 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { } // 单层逻辑 - let endIndex = 9 - guard startIndex <= endIndex else { return } - for i in startIndex ... endIndex { + let end = 9 + guard start <= end else { return } + for i in start ... end { path.append(i) // 处理 - backtracking(sum: sum + i, startIndex: i + 1) + backtracking(sum: sum + i, start: i + 1) path.removeLast() // 回溯 } } - backtracking(sum: 0, startIndex: 1) + backtracking(sum: 0, start: 1) return result } ``` From 622b443bbe309d91c14693e6b2dff96fd209bbb4 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 20:12:46 +0800 Subject: [PATCH 15/22] =?UTF-8?q?0039.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C?= =?UTF-8?q?=EF=BC=9ASwift=E5=AE=9E=E7=8E=B0=E4=BC=98=E5=8C=96=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=91=BD=E5=90=8D=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=89=AA?= =?UTF-8?q?=E6=9E=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index e78f2e2d..0f8fe4f6 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -455,7 +455,6 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { var path = [Int]() func backtracking(sum: Int, startIndex: Int) { // 终止条件 - if sum > target { return } if sum == target { result.append(path) return @@ -464,8 +463,11 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { let end = candidates.count guard startIndex < end else { return } for i in startIndex ..< end { + let sum = sum + candidates[i] // 使用局部变量隐藏回溯 + if sum > target { continue } // 剪枝 + path.append(candidates[i]) // 处理 - backtracking(sum: sum + candidates[i], startIndex: i) // sum这里用新变量完成回溯,i不用+1以重复访问 + backtracking(sum: sum, startIndex: i) // i不用+1以重复访问 path.removeLast() // 回溯 } } From 690897f33f513887c07809bd2fc158004094769c Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 20:23:12 +0800 Subject: [PATCH 16/22] =?UTF-8?q?0131.=E5=88=86=E5=89=B2=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E4=B8=B2=EF=BC=9ASwift=E5=AE=9E=E7=8E=B0=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E4=B8=AD=E8=B7=B3=E8=BF=87=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index f4d72eea..84a5bd02 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -575,12 +575,9 @@ func partition(_ s: String) -> [[String]] { for i in startIndex ..< s.count { // 回文则收集,否则跳过 - if isPalindrome(start: startIndex, end: i) { - let substring = String(s[startIndex ... i]) - path.append(substring) - } else { - continue - } + guard isPalindrome(start: startIndex, end: i) else { continue } + let substring = String(s[startIndex ... i]) + path.append(substring) // 处理 backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串 if !path.isEmpty { path.removeLast() } // 回溯 } From 2a59d4fff670f89f012fdc280d662be38773b186 Mon Sep 17 00:00:00 2001 From: Martin Hsu <31008681+Martin-Hsu@users.noreply.github.com> Date: Fri, 24 Dec 2021 16:48:52 +0800 Subject: [PATCH 17/22] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 簡化版可以直接i = 0開始 --- problems/0739.每日温度.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md index d16416d9..d025e73c 100644 --- a/problems/0739.每日温度.md +++ b/problems/0739.每日温度.md @@ -152,8 +152,7 @@ public: vector dailyTemperatures(vector& T) { stack st; // 递减栈 vector result(T.size(), 0); - st.push(0); - for (int i = 1; i < T.size(); i++) { + for (int i = 0; i < T.size(); i++) { while (!st.empty() && T[i] > T[st.top()]) { // 注意栈不能为空 result[st.top()] = i - st.top(); st.pop(); From 854a8c6f90bd343fc8aab3751f01bb56516fd1ff Mon Sep 17 00:00:00 2001 From: zhuxianzhong <22525482@qq.com> Date: Sun, 26 Dec 2021 10:01:41 +0800 Subject: [PATCH 18/22] =?UTF-8?q?=E6=9B=B4=E6=96=B00034.=E5=9C=A8=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md=20=E7=96=91?= =?UTF-8?q?=E4=BC=BC=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序数组中查找元素的第一个和最后一个位置.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 618fa395..72012747 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -50,7 +50,7 @@ 采用二分法来去寻找左右边界,为了让代码清晰,我分别写两个二分来寻找左边界和右边界。 -**刚刚接触二分搜索的同学不建议上来就像如果用一个二分来查找左右边界,很容易把自己绕进去,建议扎扎实实的写两个二分分别找左边界和右边界** +**刚刚接触二分搜索的同学不建议上来就想用一个二分来查找左右边界,很容易把自己绕进去,建议扎扎实实的写两个二分分别找左边界和右边界** ### 寻找右边界 From 2282bae4ccd82ca5242b3ea7a072b534f7d06881 Mon Sep 17 00:00:00 2001 From: zhuxianzhong <22525482@qq.com> Date: Sun, 26 Dec 2021 10:02:24 +0800 Subject: [PATCH 19/22] =?UTF-8?q?=E6=9B=B4=E6=96=B00034.=E5=9C=A8=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE.md=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...数组中查找元素的第一个和最后一个位置.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 72012747..2b96e41b 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -56,9 +56,9 @@ 先来寻找右边界,至于二分查找,如果看过[704.二分查找](https://programmercarl.com/0704.二分查找.html)就会知道,二分查找中什么时候用while (left <= right),有什么时候用while (left < right),其实只要清楚**循环不变量**,很容易区分两种写法。 -那么这里我采用while (left <= right)的写法,区间定义为[left, right],即左闭又闭的区间(如果这里有点看不懂了,强烈建议把[704.二分查找](https://programmercarl.com/0704.二分查找.html)这篇文章先看了,704题目做了之后再做这道题目就好很多了) +那么这里我采用while (left <= right)的写法,区间定义为[left, right],即左闭右闭的区间(如果这里有点看不懂了,强烈建议把[704.二分查找](https://programmercarl.com/0704.二分查找.html)这篇文章先看了,704题目做了之后再做这道题目就好很多了) -确定好:计算出来的右边界是不包好target的右边界,左边界同理。 +确定好:计算出来的右边界是不包含target的右边界,左边界同理。 可以写出如下代码 From 8c16a79ea406385fd1520d14016a3373e145c473 Mon Sep 17 00:00:00 2001 From: db <39407623+IcePigZDB@users.noreply.github.com> Date: Sun, 26 Dec 2021 22:00:18 +0800 Subject: [PATCH 20/22] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E6=B7=B1=E5=BA=A6=20null->NULL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 82a82fd5..e0cf2b63 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -48,7 +48,7 @@ int getdepth(treenode* node) 代码如下: ```CPP -if (node == null) return 0; +if (node == NULL) return 0; ``` 3. 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。 @@ -68,7 +68,7 @@ return depth; class solution { public: int getdepth(treenode* node) { - if (node == null) return 0; + if (node == NULL) return 0; int leftdepth = getdepth(node->left); // 左 int rightdepth = getdepth(node->right); // 右 int depth = 1 + max(leftdepth, rightdepth); // 中 @@ -104,7 +104,7 @@ public: void getdepth(treenode* node, int depth) { result = depth > result ? depth : result; // 中 - if (node->left == null && node->right == null) return ; + if (node->left == NULL && node->right == NULL) return ; if (node->left) { // 左 depth++; // 深度+1 @@ -137,7 +137,7 @@ public: int result; void getdepth(treenode* node, int depth) { result = depth > result ? depth : result; // 中 - if (node->left == null && node->right == null) return ; + if (node->left == NULL && node->right == NULL) return ; if (node->left) { // 左 getdepth(node->left, depth + 1); } @@ -173,7 +173,7 @@ c++代码如下: class solution { public: int maxdepth(treenode* root) { - if (root == null) return 0; + if (root == NULL) return 0; int depth = 0; queue que; que.push(root); @@ -238,7 +238,7 @@ class solution { public: int maxdepth(node* root) { queue que; - if (root != null) que.push(root); + if (root != NULL) que.push(root); int depth = 0; while (!que.empty()) { int size = que.size(); From a00146c55944c218c28dc1ddde1ca70899202513 Mon Sep 17 00:00:00 2001 From: db <39407623+IcePigZDB@users.noreply.github.com> Date: Mon, 27 Dec 2021 09:27:01 +0800 Subject: [PATCH 21/22] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index b21c7cfe..372a4660 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -90,6 +90,7 @@ public: 如下为精简之后的递归代码:(257. 二叉树的所有路径) +CPP ``` class Solution { private: From 4545e48a90836b7d3a94d910d9d72e58dcd1c2d9 Mon Sep 17 00:00:00 2001 From: db <39407623+IcePigZDB@users.noreply.github.com> Date: Mon, 27 Dec 2021 09:27:45 +0800 Subject: [PATCH 22/22] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 372a4660..27ed7a6f 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -90,8 +90,7 @@ public: 如下为精简之后的递归代码:(257. 二叉树的所有路径) -CPP -``` +```CPP class Solution { private: void traversal(TreeNode* cur, string path, vector& result) { @@ -132,8 +131,7 @@ traversal(cur->left, path, result); // 左 即: -``` - +``` CPP if (cur->left) { path += "->"; traversal(cur->left, path, result); // 左