From deaf08a9ad225a113fd7c0efd40f161d93ca460c Mon Sep 17 00:00:00 2001 From: zj <294839611@qq.com> Date: Tue, 21 Mar 2023 16:15:58 +0800 Subject: [PATCH 001/161] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= =?UTF-8?q?=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index ff0b6aba28..1b04b057f4 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -338,6 +338,64 @@ public class BagProblem { ``` +```java +import java.util.Arrays; + +public class BagProblem { + public static void main(String[] args) { + int[] weight = {1,3,4}; + int[] value = {15,20,30}; + int bagSize = 4; + testWeightBagProblem(weight,value,bagSize); + } + + /** + * 初始化 dp 数组做了简化(给物品增加冗余维)。这样初始化dp数组,默认全为0即可。 + * dp[i][j] 表示从下标为[0 - i-1]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 + * 其实是模仿背包重量从 0 开始,背包容量 j 为 0 的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为 0。 + * 可选物品也可以从无开始,也就是没有物品可选,即dp[0][j],这样无论背包容量为多少,背包价值总和一定为 0。 + * @param weight 物品的重量 + * @param value 物品的价值 + * @param bagSize 背包的容量 + */ + public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ + + // 创建dp数组 + int goods = weight.length; // 获取物品的数量 + int[][] dp = new int[goods + 1][bagSize + 1]; // 给物品增加冗余维,i = 0 表示没有物品可选 + + // 初始化dp数组,默认全为0即可 + // 填充dp数组 + for (int i = 1; i <= goods; i++) { + for (int j = 1; j <= bagSize; j++) { + if (j < weight[i - 1]) { // i - 1 对应物品 i + /** + * 当前背包的容量都没有当前物品i大的时候,是不放物品i的 + * 那么前i-1个物品能放下的最大价值就是当前情况的最大价值 + */ + dp[i][j] = dp[i - 1][j]; + } else { + /** + * 当前背包的容量可以放下物品i + * 那么此时分两种情况: + * 1、不放物品i + * 2、放物品i + * 比较这两种情况下,哪种背包中物品的最大价值最大 + */ + dp[i][j] = Math.max(dp[i - 1][j] , dp[i - 1][j - weight[i - 1]] + value[i - 1]); // i - 1 对应物品 i + } + } + } + + // 打印dp数组 + for(int[] arr : dp){ + System.out.println(Arrays.toString(arr)); + } + } +} + +``` + ### python ```python From 7d92df37eb7aca18042512789a286ec4eec3123a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 12:36:36 +0800 Subject: [PATCH 002/161] =?UTF-8?q?Update=200309.=E6=9C=80=E4=BD=B3?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA=E5=90=AB?= =?UTF-8?q?=E5=86=B7=E5=86=BB=E6=9C=9F.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index d10e61b77a..cd71136ba6 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -455,7 +455,29 @@ function maxProfit(prices: number[]): number { }; ``` - +Rust: + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + /* + * dp[i][0]: 持股状态; + * dp[i][1]: 无股状态,当天为非冷冻期; + * dp[i][2]: 无股状态,当天卖出; + * dp[i][3]: 无股状态,当天为冷冻期; + */ + let mut dp = vec![vec![0; 4]; prices.len()]; + dp[0][0] = -prices[0]; + for (i, &p) in prices.iter().enumerate().skip(1) { + dp[i][0] = dp[i - 1][0].max((dp[i - 1][3] - p).max(dp[i - 1][1] - p)); + dp[i][1] = dp[i - 1][1].max(dp[i - 1][3]); + dp[i][2] = dp[i - 1][0] + p; + dp[i][3] = dp[i - 1][2]; + } + *dp[prices.len() - 1].iter().skip(1).max().unwrap() + } +} +```

From e563783a71fe3de6564c7c01e8ffbfc961f3efee Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 12:52:41 +0800 Subject: [PATCH 003/161] =?UTF-8?q?Update=200714.=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=88=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=EF=BC=89.md=20=E4=BC=98=E5=8C=96=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 1443f14710..9aa82d4ab2 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -270,18 +270,29 @@ impl Solution { **动态规划** ```Rust impl Solution { - fn max(a: i32, b: i32) -> i32 { - if a > b { a } else { b } + pub fn max_profit(prices: Vec, fee: i32) -> i32 { + let mut dp = vec![vec![0; 2]; prices.len()]; + dp[0][0] = -prices[0]; + for (i, &p) in prices.iter().enumerate().skip(1) { + dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - p); + dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + p - fee); + } + dp[prices.len() - 1][1] } +} +``` + +**动态规划优化** + +```rust +impl Solution { pub fn max_profit(prices: Vec, fee: i32) -> i32 { - let n = prices.len(); - let mut dp = vec![vec![0; 2]; n]; - dp[0][0] -= prices[0]; - for i in 1..n { - dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]); - dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee); + let (mut low, mut res) = (-prices[0], 0); + for p in prices { + low = low.max(res - p); + res = res.max(p + low - fee); } - Self::max(dp[n - 1][0], dp[n - 1][1]) + res } } ``` From 53f077dc13f8a0076c94bd7db7bbeccd4815a9ff Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Fri, 30 Jun 2023 13:15:33 +0800 Subject: [PATCH 004/161] Apply suggestions from code review --- ...\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 9aa82d4ab2..042de94718 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -282,7 +282,7 @@ impl Solution { } ``` -**动态规划优化** +**动态规划空间优化** ```rust impl Solution { From d945a304c49f4f41de91b2794b35de0c5f79110b Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 3 Jul 2023 19:29:44 +0800 Subject: [PATCH 005/161] =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9Arecode=20->=20record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0383.\350\265\216\351\207\221\344\277\241.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index e74cdf71fd..70501469d8 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -88,7 +88,7 @@ public: return false; } for (int i = 0; i < magazine.length(); i++) { - // 通过recode数据记录 magazine里各个字符出现次数 + // 通过record数据记录 magazine里各个字符出现次数 record[magazine[i]-'a'] ++; } for (int j = 0; j < ransomNote.length(); j++) { @@ -218,7 +218,7 @@ Go: ```go func canConstruct(ransomNote string, magazine string) bool { record := make([]int, 26) - for _, v := range magazine { // 通过recode数据记录 magazine里各个字符出现次数 + for _, v := range magazine { // 通过record数据记录 magazine里各个字符出现次数 record[v-'a']++ } for _, v := range ransomNote { // 遍历ransomNote,在record里对应的字符个数做--操作 From dacd1a91d4c4cfeeeebd77b3840118f1ba61e02d Mon Sep 17 00:00:00 2001 From: ZKkkk Date: Tue, 4 Jul 2023 19:46:00 +0800 Subject: [PATCH 006/161] =?UTF-8?q?Update=201221.=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=B9=B3=E8=A1=A1=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20about=20Type?= =?UTF-8?q?Script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\241\345\255\227\347\254\246\344\270\262.md" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" index b587514a41..2a7b092290 100644 --- "a/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/1221.\345\210\206\345\211\262\345\271\263\350\241\241\345\255\227\347\254\246\344\270\262.md" @@ -156,6 +156,21 @@ var balancedStringSplit = function(s) { }; ``` +### TypeScript + +```ts +function balancedStringSplit(s: string): number { + let count: number = 0 + let res: number = 0 + for(let i of s){ + if(i === 'R') count++ + else count-- + if(count === 0) res++ + } + + return res +}; +```

From 4fe73f30dc793ace07e15b23ff0956eb221c5856 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 5 Jul 2023 14:18:55 +0800 Subject: [PATCH 007/161] =?UTF-8?q?=E4=B9=A6=E5=86=99=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8Cnull=E5=86=99=E6=88=90=E4=BA=86nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 3d73598da9..28c5a4c037 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == nullptr) return root; +if (root == null) return root; ``` * 确定单层递归的逻辑 From 751ba68faa3d16b45fce13e0ada8aac58d24e719 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Wed, 5 Jul 2023 14:35:56 +0800 Subject: [PATCH 008/161] =?UTF-8?q?null=E5=86=99=E6=88=90=E4=BA=86nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 28c5a4c037..48737486d0 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == nullptr) return root->right; + if (root->left == null) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == nullptr) return root->left; + else if (root->right == null) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != nullptr) { + while(cur->left != null) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -118,23 +118,23 @@ return root; class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == nullptr && root->right == nullptr) { + if (root->left == null && root->right == null) { ///! 内存释放 delete root; - return nullptr; + return null; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == nullptr) { + else if (root->left == null) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == nullptr) { + else if (root->right == null) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != nullptr) { + while(cur->left != null) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -178,9 +178,9 @@ public: class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; + if (root == null) return root; if (root->val == key) { - if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == nullptr) return target; - if (target->right == nullptr) return target->left; + if (target == null) return target; + if (target->right == null) return target->left; TreeNode* cur = target->right; while (cur->left) { cur = cur->left; @@ -222,16 +222,16 @@ private: } public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == nullptr) return root; + if (root == null) return root; TreeNode* cur = root; - TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur + TreeNode* pre = null; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == nullptr) { // 如果搜索树只有头结点 + if (pre == null) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子 From d7753b31b6f66def80719e82cb7d8ddef3056a7e Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Thu, 6 Jul 2023 23:14:49 +0800 Subject: [PATCH 009/161] =?UTF-8?q?Update=201002.=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=B8=B8=E7=94=A8=E5=AD=97=E7=AC=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix formatting --- ...6\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index 9ec3c6c4e8..a53148b313 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -16,6 +16,7 @@ 输入:words = ["bella","label","roller"] 输出:["e","l","l"] + 示例 2: 输入:words = ["cool","lock","cook"] From 873257dcd812b1d190f9decbf0fed64761d23933 Mon Sep 17 00:00:00 2001 From: Liu Yongliang <41845017+tlylt@users.noreply.github.com> Date: Sat, 8 Jul 2023 07:25:59 +0800 Subject: [PATCH 010/161] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix Python solution comment --- .../0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index ca62e3edc2..438fa352f8 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -160,7 +160,7 @@ class Solution: for index, value in enumerate(nums): if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key return [records[target- value], index] - records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key + records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中 return [] ``` (版本二)使用集合 From dba8820427c64b66add3b7ad053226e7d9943e53 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 10:43:24 +0800 Subject: [PATCH 011/161] =?UTF-8?q?Update=200739.=E6=AF=8F=E6=97=A5?= =?UTF-8?q?=E6=B8=A9=E5=BA=A6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\346\227\245\346\270\251\345\272\246.md" | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index 749dc97235..d2da37371a 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -455,7 +455,27 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` - +Rust: + +```rust +impl Solution { + /// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标 + pub fn daily_temperatures(temperatures: Vec) -> Vec { + let mut res = vec![0; temperatures.len()]; + let mut stack = vec![]; + for (idx, &value) in temperatures.iter().enumerate() { + while !stack.is_empty() && temperatures[*stack.last().unwrap()] < value { + // 弹出,并计算res中对应位置的值 + let i = stack.pop().unwrap(); + res[i] = (idx - i) as i32; + } + // 入栈 + stack.push(idx) + } + res + } +} +```

From 965afc9af3dc5c0fb8932937770f8395c98b7b86 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:20:21 +0800 Subject: [PATCH 012/161] =?UTF-8?q?=E4=BF=AE=E6=94=B9java=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=A7=A3=E6=B3=95=E7=9A=84=E4=BD=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E9=80=82=E5=90=88=E5=9B=BE=E7=9A=84=E8=A7=A3=E6=B3=95=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=E7=AC=AC=E4=B8=80=E4=B8=AA=EF=BC=8C=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E4=B8=AA=E8=A7=A3=E6=B3=95=EF=BC=88=E9=9A=BE=E7=90=86=E8=A7=A3?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=89=E6=94=BE=E5=9C=A8=E5=85=B6=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9Ejava=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 48737486d0..d13c48e529 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -268,6 +268,34 @@ public: ## Java +```java +// 解法1(最好理解的版本) +class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) return root; + if (root.val == key) { + if (root.left == null) { + return root.right; + } else if (root.right == null) { + return root.left; + } else { + TreeNode cur = root.right; + while (cur.left != null) { + cur = cur.left; + } + cur.left = root.left; + root = root.right; + return root; + } + } + if (root.val > key) root.left = deleteNode(root.left, key); + if (root.val < key) root.right = deleteNode(root.right, key); + return root; + } +} +``` + + ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -296,33 +324,57 @@ class Solution { } } ``` +递归法 ```java -// 解法2 class Solution { public TreeNode deleteNode(TreeNode root, int key) { - if (root == null) return root; - if (root.val == key) { - if (root.left == null) { - return root.right; - } else if (root.right == null) { - return root.left; - } else { - TreeNode cur = root.right; - while (cur.left != null) { - cur = cur.left; - } - cur.left = root.left; - root = root.right; - return root; + if (root == null){ + return null; + } + //寻找对应的对应的前面的节点,以及他的前一个节点 + TreeNode cur = root; + TreeNode pre = null; + while (cur != null){ + if (cur.val < key){ + pre = cur; + cur = cur.right; + } else if (cur.val > key) { + pre = cur; + cur = cur.left; + }else { + break; } } - if (root.val > key) root.left = deleteNode(root.left, key); - if (root.val < key) root.right = deleteNode(root.right, key); + if (pre == null){ + return deleteOneNode(cur); + } + if (pre.left !=null && pre.left.val == key){ + pre.left = deleteOneNode(cur); + } + if (pre.right !=null && pre.right.val == key){ + pre.right = deleteOneNode(cur); + } return root; } + + public TreeNode deleteOneNode(TreeNode node){ + if (node == null){ + return null; + } + if (node.right == null){ + return node.left; + } + TreeNode cur = node.right; + while (cur.left !=null){ + cur = cur.left; + } + cur.left = node.left; + return node.right; + } } ``` + ## Python 递归法(版本一) ```python From 0fd7b04d5c0fac9a760af96d2d5de7f5beaeb896 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Sat, 8 Jul 2023 17:42:19 +0800 Subject: [PATCH 013/161] =?UTF-8?q?=E6=92=A4=E5=9B=9E=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84nullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index d13c48e529..e617cc69b6 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -51,7 +51,7 @@ TreeNode* deleteNode(TreeNode* root, int key) 遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了 ``` -if (root == null) return root; +if (root == nullptr) return root; ``` * 确定单层递归的逻辑 @@ -85,14 +85,14 @@ if (root == null) return root; if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - if (root->left == null) return root->right; + if (root->left == nullptr) return root->right; // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) return root->left; + else if (root->right == nullptr) return root->left; // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置 // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -118,23 +118,23 @@ return root; class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 + if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了 if (root->val == key) { // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点 - if (root->left == null && root->right == null) { + if (root->left == nullptr && root->right == nullptr) { ///! 内存释放 delete root; - return null; + return nullptr; } // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点 - else if (root->left == null) { + else if (root->left == nullptr) { auto retNode = root->right; ///! 内存释放 delete root; return retNode; } // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点 - else if (root->right == null) { + else if (root->right == nullptr) { auto retNode = root->left; ///! 内存释放 delete root; @@ -144,7 +144,7 @@ public: // 并返回删除节点右孩子为新的根节点。 else { TreeNode* cur = root->right; // 找右子树最左面的节点 - while(cur->left != null) { + while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置 @@ -178,9 +178,9 @@ public: class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; + if (root == nullptr) return root; if (root->val == key) { - if (root->right == null) { // 这里第二次操作目标值:最终删除的作用 + if (root->right == nullptr) { // 这里第二次操作目标值:最终删除的作用 return root->left; } TreeNode *cur = root->right; @@ -211,8 +211,8 @@ private: // 并返回目标节点右孩子为新的根节点 // 是动画里模拟的过程 TreeNode* deleteOneNode(TreeNode* target) { - if (target == null) return target; - if (target->right == null) return target->left; + if (target == nullptr) return target; + if (target->right == nullptr) return target->left; TreeNode* cur = target->right; while (cur->left) { cur = cur->left; @@ -222,16 +222,16 @@ private: } public: TreeNode* deleteNode(TreeNode* root, int key) { - if (root == null) return root; + if (root == nullptr) return root; TreeNode* cur = root; - TreeNode* pre = null; // 记录cur的父节点,用来删除cur + TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur while (cur) { if (cur->val == key) break; pre = cur; if (cur->val > key) cur = cur->left; else cur = cur->right; } - if (pre == null) { // 如果搜索树只有头结点 + if (pre == nullptr) { // 如果搜索树只有头结点 return deleteOneNode(cur); } // pre 要知道是删左孩子还是右孩子 From 1c7b15bbbc224e37f9341161f31a94cebe73dfb0 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 18:26:09 +0800 Subject: [PATCH 014/161] =?UTF-8?q?Update=200496.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0I.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240I.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 31c3ce4387..411a47df9b 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -387,6 +387,32 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` +Rust + +```rust +impl Solution { + pub fn next_greater_element(nums1: Vec, nums2: Vec) -> Vec { + let mut ans = vec![-1; nums1.len()]; + use std::collections::HashMap; + let mut map = HashMap::new(); + for (idx, &i) in nums1.iter().enumerate() { + map.insert(i, idx); + } + let mut stack = vec![]; + for (idx, &i) in nums2.iter().enumerate() { + while !stack.is_empty() && nums2[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + if let Some(&jdx) = map.get(&nums2[pos]) { + ans[jdx] = i; + } + } + stack.push(idx); + } + ans + } +} +``` +

From 85e2b18a1d9f5eba4704a3b585524ea54bc73c7d Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 20:38:47 +0800 Subject: [PATCH 015/161] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240II.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 3fd4b3b6db..a090f32c3e 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -266,6 +266,24 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` +Rust +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let mut ans = vec![-1; nums.len() * 2]; + let mut stack = vec![]; + let double = nums.repeat(2); + for (idx, &i) in double.iter().enumerate() { + while !stack.is_empty() && double[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + ans[pos] = i; + } + stack.push(idx); + } + ans.into_iter().take(nums.len()).collect() + } +} +```

From 6e7e5e0fae032918f5288e86c177c3e40916c6e0 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 22:13:12 +0800 Subject: [PATCH 016/161] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8?= =?UTF-8?q?=E6=B0=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index db66095da2..833a7613e2 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -926,6 +926,56 @@ int trap(int* height, int heightSize) { * 空间复杂度 O(1) +Rust + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h > 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top > height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +``` +

From cef827812ab2084e6d6c0437bff6fd4d2f0b2720 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sun, 9 Jul 2023 14:30:01 +0800 Subject: [PATCH 017/161] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index f9a8350818..bc82a860cc 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -670,6 +670,61 @@ function largestRectangleArea(heights: number[]): number { ``` +Rust + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid >= 0 && v[mid as usize] >= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid] >= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()] > e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +``` +

From a31b5865df04e5004724995e2b36610ec45d9489 Mon Sep 17 00:00:00 2001 From: life <13122192336@163.com> Date: Mon, 10 Jul 2023 16:00:53 +0800 Subject: [PATCH 018/161] =?UTF-8?q?=E7=BB=84=E5=90=88=E6=96=B0=E5=A2=9Ejav?= =?UTF-8?q?a=E7=89=88=E6=9C=AC=E6=9C=AA=E5=89=AA=E6=9E=9D=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 444e15ce74..8ade6e114f 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -351,7 +351,30 @@ public: ### Java: +未剪枝优化 +```java +class Solution { + List> result= new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> combine(int n, int k) { + backtracking(n,k,1); + return result; + } + public void backtracking(int n,int k,int startIndex){ + if (path.size() == k){ + result.add(new ArrayList<>(path)); + return; + } + for (int i =startIndex;i<=n;i++){ + path.add(i); + backtracking(n,k,i+1); + path.removeLast(); + } + } +} +``` +剪枝优化: ```java class Solution { List> result = new ArrayList<>(); From 3ba32880fa55769ef99686ee42bd5b8da9bc077b Mon Sep 17 00:00:00 2001 From: "jingsong.zeng" Date: Tue, 11 Jul 2023 14:17:53 +0800 Subject: [PATCH 019/161] Add the dart implementation of 0704. --- ...14\345\210\206\346\237\245\346\211\276.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 75135749bd..ba35867129 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -755,8 +755,56 @@ object Solution { } } ``` +**Dart:** + +```dart +(版本一)左闭右闭区间 +class Solution { + int search(List nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left <= right) { + int middle = ((left + right)/2).truncate(); + switch (nums[middle].compareTo(target)) { + case 1: + right = middle - 1; + continue; + case -1: + left = middle + 1; + continue; + default: + return middle; + } + } + return -1; + } +} + +(版本二)左闭右开区间 +class Solution { + int search(List nums, int target) { + int left = 0; + int right = nums.length; + while (left < right) { + int middle = left + ((right - left) >> 1); + switch (nums[middle].compareTo(target)) { + case 1: + right = middle; + continue; + case -1: + left = middle + 1; + continue; + default: + return middle; + } + } + return -1; + } +} +``` +

From 0129bde1d87599790af4ad5dc32cc2cd05320e16 Mon Sep 17 00:00:00 2001 From: Nihilism <114405451+Nihilism0@users.noreply.github.com> Date: Fri, 14 Jul 2023 13:56:34 +0800 Subject: [PATCH 020/161] =?UTF-8?q?=E4=BC=98=E5=8C=96=200151.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8DGo=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...214\347\232\204\345\215\225\350\257\215.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 6dd3cd4975..ee944089d3 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -546,26 +546,28 @@ func reverseWords(s string) string { b = b[:slowIndex] } //2.反转整个字符串 - reverse(&b, 0, len(b)-1) + reverse(b) //3.反转单个单词 i单词开始位置,j单词结束位置 i := 0 for i < len(b) { j := i for ; j < len(b) && b[j] != ' '; j++ { } - reverse(&b, i, j-1) + reverse(b[i:j]) i = j i++ } return string(b) } -func reverse(b *[]byte, left, right int) { - for left < right { - (*b)[left], (*b)[right] = (*b)[right], (*b)[left] - left++ - right-- - } +func reverse(b []byte) { + left := 0 + right := len(b) - 1 + for left < right { + b[left], b[right] = b[right], b[left] + left++ + right-- + } } ``` From 8887adbb109acd58ac65bea198d1dc287d99db42 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: Fri, 14 Jul 2023 21:52:24 +0800 Subject: [PATCH 021/161] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E5=B9=BF=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\345\271\277\346\220\234\347\211\210.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index 39af9f50a3..4a990a445b 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -196,6 +196,41 @@ class Solution { } } ``` + +Python: + +```python +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + ans = 0 + + def bfs(x, y): + q = deque() + q.append([x, y]) + visited[x][y] = True + while q: + curx, cury = q.popleft() + for d in dirs: + nextx = curx + d[0] + nexty = cury + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + if visited[nextx][nexty] == False and grid[nextx][nexty] == "1": + q.append([nextx, nexty]) + visited[nextx][nexty] = True # 只要加入队列立刻标记 + + for i in range(m): + for j in range(n): + if grid[i][j] == "1" and not visited[i][j]: + ans += 1 # 遇到没访问过的陆地,+1 + bfs(i, j) # 将与其链接的陆地都标记上 true + + return ans +``` +

From 9b5c5ebeea85b133514ab42c0218b79e13599b45 Mon Sep 17 00:00:00 2001 From: limuxuale0927 Date: Fri, 14 Jul 2023 22:07:13 +0800 Subject: [PATCH 022/161] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200200.=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E6=95=B0=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88.md=20Py?= =?UTF-8?q?thon3=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index c30ace1999..f610e32330 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -218,6 +218,67 @@ class Solution { } } ``` + +Python: + +```python +# 版本一 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + if not visited[nextx][nexty] and grid[nextx][nexty] == '1': # 没有访问过的同时是陆地的 + visited[nextx][nexty] = True + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + visited[i][j] = True + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` + +```python +# 版本二 +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + visited = [[False] * n for _ in range(m)] + dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向 + result = 0 + + def dfs(x, y): + if visited[x][y] or grid[x][y] == '0': + return # 终止条件:访问过的节点 或者 遇到海水 + visited[x][y] = True + for d in dirs: + nextx = x + d[0] + nexty = y + d[1] + if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过 + continue + dfs(nextx, nexty) + + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == '1': + result += 1 # 遇到没访问过的陆地,+1 + dfs(i, j) # 将与其链接的陆地都标记上 true + + return result +``` +

From 5794b17ab4ccb5da0411896eda509c503014d7d6 Mon Sep 17 00:00:00 2001 From: TonySu Date: Sun, 16 Jul 2023 22:00:05 +0800 Subject: [PATCH 023/161] =?UTF-8?q?Update=200039.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E5=92=8C.md,=20=E4=BF=AE=E5=A4=8DPython=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E4=B8=80=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 版本的回溯剪枝版本一当中, break 会终止整个for 循环,从而提前终止搜索,应该改成continue --- .../0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 4d9466c3c0..e09a44e496 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -311,7 +311,7 @@ class Solution: for i in range(startIndex, len(candidates)): if total + candidates[i] > target: - break + continue total += candidates[i] path.append(candidates[i]) self.backtracking(candidates, target, total, i, path, result) From b51a1d89bf6b340f3a96ba7f109c1d481a887693 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:21:20 +0800 Subject: [PATCH 024/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index a09974df7c..da5a1b0244 100644 --- "a/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\351\223\276\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -16,15 +16,15 @@ 如图所示: ![链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194529815.png) -# 链表的类型 +## 链表的类型 接下来说一下链表的几种类型: -## 单链表 +### 单链表 刚刚说的就是单链表。 -## 双链表 +### 双链表 单链表中的指针域只能指向节点的下一个节点。 @@ -35,7 +35,7 @@ 如图所示: ![链表2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194559317.png) -## 循环链表 +### 循环链表 循环链表,顾名思义,就是链表首尾相连。 @@ -44,7 +44,7 @@ ![链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/20200806194629603.png) -# 链表的存储方式 +## 链表的存储方式 了解完链表的类型,再来说一说链表在内存中的存储方式。 @@ -60,7 +60,7 @@ 这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。 -# 链表的定义 +## 链表的定义 接下来说一说链表的定义。 @@ -100,9 +100,9 @@ head->val = 5; 所以如果不定义构造函数使用默认构造函数的话,在初始化的时候就不能直接给变量赋值! -# 链表的操作 +## 链表的操作 -## 删除节点 +### 删除节点 删除D节点,如图所示: @@ -116,7 +116,7 @@ head->val = 5; 其他语言例如Java、Python,就有自己的内存回收机制,就不用自己手动释放了。 -## 添加节点 +### 添加节点 如图所示: @@ -126,7 +126,7 @@ head->val = 5; 但是要注意,要是删除第五个节点,需要从头节点查找到第四个节点通过next指针进行删除操作,查找的时间复杂度是O(n)。 -# 性能分析 +## 性能分析 再把链表的特性和数组的特性进行一个对比,如图所示: @@ -143,8 +143,7 @@ head->val = 5; ## 其他语言版本 - -Java: +### Java: ```java public class ListNode { @@ -171,7 +170,7 @@ public class ListNode { } ``` -JavaScript: +### JavaScript: ```javascript class ListNode { @@ -184,7 +183,7 @@ class ListNode { } ``` -TypeScript: +### TypeScript: ```typescript class ListNode { @@ -197,7 +196,7 @@ class ListNode { } ``` -Python: +### Python: ```python class ListNode: @@ -206,7 +205,7 @@ class ListNode: self.next = next ``` -Go: +### Go: ```go type ListNode struct { @@ -215,7 +214,7 @@ type ListNode struct { } ``` -Scala: +### Scala: ```scala class ListNode(_x: Int = 0, _next: ListNode = null) { @@ -224,7 +223,7 @@ class ListNode(_x: Int = 0, _next: ListNode = null) { } ``` -Rust: +### Rust: ```rust #[derive(PartialEq, Eq, Clone, Debug)] @@ -246,3 +245,4 @@ impl ListNode { + From a78116889f705d9e7f72f927decb0a9e844bd807 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:28:12 +0800 Subject: [PATCH 025/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index 300f98e911..f09c572b95 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -27,14 +27,12 @@ 输入:head = [7,7,7,7], val = 7 输出:[] -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 - -为了方便大家理解,我特意录制了视频:[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),结合视频在看本题解,事半功倍。 +## 思路 这里以链表 1 4 2 4 来举例,移除元素4。 @@ -91,7 +89,7 @@ 最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点 -# C++代码 +### C++代码 **直接使用原来的链表来进行移除节点操作:** @@ -159,7 +157,7 @@ public: ## 其他语言版本 -C: +### C: 用原来的链表操作: ```c @@ -227,7 +225,7 @@ struct ListNode* removeElements(struct ListNode* head, int val){ } ``` -Java: +### Java: ```java /** @@ -308,7 +306,7 @@ public ListNode removeElements(ListNode head, int val) { } ``` -Python: +### Python: ```python (版本一)虚拟头节点法 @@ -334,7 +332,7 @@ class Solution: ``` -Go: +### Go: ```go /** @@ -359,7 +357,7 @@ func removeElements(head *ListNode, val int) *ListNode { } ``` -javaScript: +### JavaScript: ```js /** @@ -381,7 +379,7 @@ var removeElements = function(head, val) { }; ``` -TypeScript: +### TypeScript: 版本一(在原链表上直接删除): @@ -437,7 +435,7 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { }; ``` -Swift: +### Swift: ```swift /** @@ -465,7 +463,7 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? { } ``` -PHP: +### PHP: ```php /** @@ -493,7 +491,7 @@ func removeElements(head *ListNode, val int) *ListNode { } ``` -RUST: +### Rust: ```rust // Definition for singly-linked list. @@ -531,7 +529,7 @@ impl Solution { } ``` -Scala: +### Scala: ```scala /** @@ -564,7 +562,7 @@ object Solution { } ``` -Kotlin: +### Kotlin: ```kotlin /** @@ -600,7 +598,8 @@ class Solution { } ``` -C# +### C# + ```CSharp /** * Definition for singly-linked list. From 61366ff59037d5f22f256e5bc410efd8a36a35d3 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:41:02 +0800 Subject: [PATCH 026/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200707.=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\350\241\250\345\205\203\347\264\240.md" | 4 +-- ...76\350\256\241\351\223\276\350\241\250.md" | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" index f09c572b95..c8f802a183 100644 --- "a/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" +++ "b/problems/0203.\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240.md" @@ -88,9 +88,6 @@ 最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点 - -### C++代码 - **直接使用原来的链表来进行移除节点操作:** ```CPP @@ -638,3 +635,4 @@ public class Solution + diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 3f096a2246..c27f0107fb 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -25,12 +25,12 @@ ![707示例](https://code-thinking-1253855093.file.myqcloud.com/pics/20200814200558953.png) -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表操作学个通透!LeetCode:707.设计链表](https://www.bilibili.com/video/BV1FU4y1X7WD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 如果对链表的基础知识还不太懂,可以看这篇文章:[关于链表,你该了解这些!](https://programmercarl.com/链表理论基础.html) @@ -58,8 +58,6 @@ 下面采用的设置一个虚拟头结点(这样更方便一些,大家看代码就会感受出来)。 - -## 代码 ```CPP class MyLinkedList { public: @@ -167,7 +165,8 @@ private: ## 其他语言版本 -C: +### C: + ```C typedef struct MyLinkedList { int val; @@ -291,7 +290,8 @@ void myLinkedListFree(MyLinkedList* obj) { */ ``` -Java: +### Java: + ```Java //单链表 class ListNode { @@ -487,7 +487,8 @@ class MyLinkedList { */ ``` -Python: +### Python: + ```python (版本一)单链表法 class ListNode: @@ -661,7 +662,7 @@ class MyLinkedList: # obj.deleteAtIndex(index) ``` -Go: +### Go: ```go //单链表实现 @@ -915,7 +916,7 @@ func (this *MyLinkedList) DeleteAtIndex(index int) { } ``` -javaScript: +### JavaScript: ```js @@ -1055,7 +1056,8 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { */ ``` -TypeScript: +### TypeScript: + ```TypeScript class ListNode { public val: number; @@ -1173,7 +1175,8 @@ class MyLinkedList { } ``` -Kotlin: +### Kotlin: + ```kotlin class MyLinkedList { @@ -1241,8 +1244,7 @@ class MyLinkedList { } ``` - -Swift: +### Swift: ```swift class MyLinkedList { @@ -1323,7 +1325,8 @@ class MyLinkedList { } ``` -Scala: +### Scala: + ```scala class ListNode(_x: Int = 0, _next: ListNode = null) { var next: ListNode = _next @@ -1393,7 +1396,7 @@ class MyLinkedList() { } ``` -Rust: +### Rust: ```rust #[derive(Debug)] @@ -1486,4 +1489,3 @@ impl MyLinkedList { - From 32bf073bc8c2fe08aabc9aaf0ef61184febfb0a8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 14:50:44 +0800 Subject: [PATCH 027/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200206.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\350\275\254\351\223\276\350\241\250.md" | 745 ++++++++++++++++++ ...73\350\275\254\351\223\276\350\241\250.md" | 55 +- 2 files changed, 772 insertions(+), 28 deletions(-) create mode 100644 "problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" diff --git "a/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" new file mode 100644 index 0000000000..5a57939af2 --- /dev/null +++ "b/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" @@ -0,0 +1,745 @@ +

+ + + +

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ + +> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 + +# 206.反转链表 + +[力扣题目链接](https://leetcode.cn/problems/reverse-linked-list/) + +题意:反转一个单链表。 + +示例: +输入: 1->2->3->4->5->NULL +输出: 5->4->3->2->1->NULL + +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 + +如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。 + +其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: + + +![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) + +之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 + +那么接下来看一看是如何反转的呢? + +我们拿有示例中的链表来举例,如动画所示:(纠正:动画应该是先移动pre,在移动cur) + +![](https://code-thinking.cdn.bcebos.com/gifs/206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.gif) + +首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。 + +然后就要开始反转了,首先要把 cur->next 节点用tmp指针保存一下,也就是保存一下这个节点。 + +为什么要保存一下这个节点呢,因为接下来要改变 cur->next 的指向了,将cur->next 指向pre ,此时已经反转了第一个节点了。 + +接下来,就是循环走如下代码逻辑了,继续移动pre和cur指针。 + +最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。 + +### 双指针法 +```CPP +class Solution { +public: + ListNode* reverseList(ListNode* head) { + ListNode* temp; // 保存cur的下一个节点 + ListNode* cur = head; + ListNode* pre = NULL; + while(cur) { + temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next + cur->next = pre; // 翻转操作 + // 更新pre 和 cur指针 + pre = cur; + cur = temp; + } + return pre; + } +}; +``` + +* 时间复杂度: O(n) +* 空间复杂度: O(1) + +### 递归法 + +递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 + +关键是初始化的地方,可能有的同学会不理解, 可以看到双指针法中初始化 cur = head,pre = NULL,在递归法中可以从如下代码看出初始化的逻辑也是一样的,只不过写法变了。 + +具体可以看代码(已经详细注释),**双指针法写出来之后,理解如下递归写法就不难了,代码逻辑都是一样的。** +```CPP +class Solution { +public: + ListNode* reverse(ListNode* pre,ListNode* cur){ + if(cur == NULL) return pre; + ListNode* temp = cur->next; + cur->next = pre; + // 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步 + // pre = cur; + // cur = temp; + return reverse(cur,temp); + } + ListNode* reverseList(ListNode* head) { + // 和双指针法初始化是一样的逻辑 + // ListNode* cur = head; + // ListNode* pre = NULL; + return reverse(NULL, head); + } + +}; +``` + +* 时间复杂度: O(n), 要递归处理链表的每个节点 +* 空间复杂度: O(n), 递归调用了 n 层栈空间 + +我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。 + +具体代码如下(带详细注释): + +```CPP +class Solution { +public: + ListNode* reverseList(ListNode* head) { + // 边缘条件判断 + if(head == NULL) return NULL; + if (head->next == NULL) return head; + + // 递归调用,翻转第二个节点开始往后的链表 + ListNode *last = reverseList(head->next); + // 翻转头节点与第二个节点的指向 + head->next->next = head; + // 此时的 head 节点为尾节点,next 需要指向 NULL + head->next = NULL; + return last; + } +}; +``` + +* 时间复杂度: O(n) +* 空间复杂度: O(n) + + +## 其他语言版本 + +### Java: + +```java +// 双指针 +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prev = null; + ListNode cur = head; + ListNode temp = null; + while (cur != null) { + temp = cur.next;// 保存下一个节点 + cur.next = prev; + prev = cur; + cur = temp; + } + return prev; + } +} +``` + +```java +// 递归 +class Solution { + public ListNode reverseList(ListNode head) { + return reverse(null, head); + } + + private ListNode reverse(ListNode prev, ListNode cur) { + if (cur == null) { + return prev; + } + ListNode temp = null; + temp = cur.next;// 先保存下一个节点 + cur.next = prev;// 反转 + // 更新prev、cur位置 + // prev = cur; + // cur = temp; + return reverse(cur, temp); + } +} +``` + +```java +// 从后向前递归 +class Solution { + ListNode reverseList(ListNode head) { + // 边缘条件判断 + if(head == null) return null; + if (head.next == null) return head; + + // 递归调用,翻转第二个节点开始往后的链表 + ListNode last = reverseList(head.next); + // 翻转头节点与第二个节点的指向 + head.next.next = head; + // 此时的 head 节点为尾节点,next 需要指向 NULL + head.next = null; + return last; + } +} +``` + +### Python: + +```python +(版本一)双指针法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: ListNode) -> ListNode: + cur = head + pre = None + while cur: + temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next + cur.next = pre #反转 + #更新pre、cur指针 + pre = cur + cur = temp + return pre +``` + +```python +(版本二)递归法 +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reverseList(self, head: ListNode) -> ListNode: + return self.reverse(head, None) + def reverse(self, cur: ListNode, pre: ListNode) -> ListNode: + if cur == None: + return pre + temp = cur.next + cur.next = pre + return self.reverse(temp, cur) + +``` + + + +### Go: + +```go +//双指针 +func reverseList(head *ListNode) *ListNode { + var pre *ListNode + cur := head + for cur != nil { + next := cur.Next + cur.Next = pre + pre = cur + cur = next + } + return pre +} + +//递归 +func reverseList(head *ListNode) *ListNode { + return help(nil, head) +} + +func help(pre, head *ListNode)*ListNode{ + if head == nil { + return pre + } + next := head.Next + head.Next = pre + return help(head, next) +} + +``` +### JavaScript: + +```js +/** + * @param {ListNode} head + * @return {ListNode} + */ + +// 双指针: +var reverseList = function(head) { + if(!head || !head.next) return head; + let temp = null, pre = null, cur = head; + while(cur) { + temp = cur.next; + cur.next = pre; + pre = cur; + cur = temp; + } + // temp = cur = null; + return pre; +}; + +// 递归: +var reverse = function(pre, head) { + if(!head) return pre; + const temp = head.next; + head.next = pre; + pre = head + return reverse(pre, temp); +} + +var reverseList = function(head) { + return reverse(null, head); +}; + +// 递归2 +var reverse = function(head) { + if(!head || !head.next) return head; + // 从后往前翻 + const pre = reverse(head.next); + head.next = pre.next; + pre.next = head; + return head; +} + +var reverseList = function(head) { + let cur = head; + while(cur && cur.next) { + cur = cur.next; + } + reverse(head); + return cur; +}; +``` + +### TypeScript: + +```typescript +// 双指针法 +function reverseList(head: ListNode | null): ListNode | null { + let preNode: ListNode | null = null, + curNode: ListNode | null = head, + tempNode: ListNode | null; + while (curNode) { + tempNode = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +}; + +// 递归(从前往后翻转) +function reverseList(head: ListNode | null): ListNode | null { + function recur(preNode: ListNode | null, curNode: ListNode | null): ListNode | null { + if (curNode === null) return preNode; + let tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + return recur(preNode, curNode); + } + return recur(null, head); +}; + +// 递归(从后往前翻转) +function reverseList(head: ListNode | null): ListNode | null { + if (head === null) return null; + let newHead: ListNode | null; + function recur(node: ListNode | null, preNode: ListNode | null): void { + if (node.next === null) { + newHead = node; + newHead.next = preNode; + } else { + recur(node.next, node); + node.next = preNode; + } + } + recur(head, null); + return newHead; +}; +``` + +### Ruby: + +```ruby +# 双指针 +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +def reverse_list(head) + # return nil if head.nil? # 循环判断条件亦能起到相同作用因此不必单独判断 + cur, per = head, nil + until cur.nil? + tem = cur.next + cur.next = per + per = cur + cur = tem + end + per +end + +# 递归 +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end +def reverse_list(head) + reverse(nil, head) +end + +def reverse(pre, cur) + return pre if cur.nil? + tem = cur.next + cur.next = pre + reverse(cur, tem) # 通过递归实现双指针法中的更新操作 +end +``` + +### Kotlin: + +```Kotlin +fun reverseList(head: ListNode?): ListNode? { + var pre: ListNode? = null + var cur = head + while (cur != null) { + val temp = cur.next + cur.next = pre + pre = cur + cur = temp + } + return pre +} +``` +```kotlin +/** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ +class Solution { + fun reverseList(head: ListNode?): ListNode? { + // temp用来存储临时的节点 + var temp: ListNode? + // cur用来遍历链表 + var cur: ListNode? = head + // pre用来作为链表反转的工具 + // pre是比pre前一位的节点 + var pre: ListNode? = null + while (cur != null) { + // 临时存储原本cur的下一个节点 + temp = cur.next + // 使cur下一节点地址为它之前的 + cur.next = pre + // 之后随着cur的遍历移动pre + pre = cur; + // 移动cur遍历链表各个节点 + cur = temp; + } + // 由于开头使用pre为null,所以cur等于链表本身长度+1, + // 此时pre在cur前一位,所以此时pre为头节点 + return pre; + } +} +``` + +### Swift: + +```swift +/// 双指针法 (迭代) +/// - Parameter head: 头结点 +/// - Returns: 翻转后的链表头结点 +func reverseList(_ head: ListNode?) -> ListNode? { + if head == nil || head?.next == nil { + return head + } + var pre: ListNode? = nil + var cur: ListNode? = head + var temp: ListNode? = nil + while cur != nil { + temp = cur?.next + cur?.next = pre + pre = cur + cur = temp + } + return pre +} + +/// 递归 +/// - Parameter head: 头结点 +/// - Returns: 翻转后的链表头结点 +func reverseList2(_ head: ListNode?) -> ListNode? { + return reverse(pre: nil, cur: head) +} +func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { + if cur == nil { + return pre + } + let temp: ListNode? = cur?.next + cur?.next = pre + return reverse(pre: cur, cur: temp) +} +``` + +### C: +双指针法: + +```c +struct ListNode* reverseList(struct ListNode* head){ + //保存cur的下一个结点 + struct ListNode* temp; + //pre指针指向前一个当前结点的前一个结点 + struct ListNode* pre = NULL; + //用head代替cur,也可以再定义一个cur结点指向head。 + while(head) { + //保存下一个结点的位置 + temp = head->next; + //翻转操作 + head->next = pre; + //更新结点 + pre = head; + head = temp; + } + return pre; +} +``` + +递归法: +```c +struct ListNode* reverse(struct ListNode* pre, struct ListNode* cur) { + if(!cur) + return pre; + struct ListNode* temp = cur->next; + cur->next = pre; + //将cur作为pre传入下一层 + //将temp作为cur传入下一层,改变其指针指向当前cur + return reverse(cur, temp); +} + +struct ListNode* reverseList(struct ListNode* head){ + return reverse(NULL, head); +} +``` + + + +### PHP: + +```php +// 双指针法: +function reverseList($head) { + $cur = $head; + $pre = NULL; + while($cur){ + $temp = $cur->next; + $cur->next = $pre; + $pre = $cur; + $cur = $temp; + } + return $pre; + } +``` + +### Scala: +双指针法: + +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + var pre: ListNode = null + var cur = head + while (cur != null) { + var tmp = cur.next + cur.next = pre + pre = cur + cur = tmp + } + pre + } +} +``` +递归法: +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + reverse(null, head) + } + + def reverse(pre: ListNode, cur: ListNode): ListNode = { + if (cur == null) { + return pre // 如果当前cur为空,则返回pre + } + val tmp: ListNode = cur.next + cur.next = pre + reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 + } + +} +``` + +### Rust: +双指针法: + +```rust +impl Solution { + pub fn reverse_list(head: Option>) -> Option> { + let mut cur = head; + let mut pre = None; + while let Some(mut node) = cur.take() { + cur = node.next; + node.next = pre; + pre = Some(node); + } + pre + } +} +``` + +递归法: + +```rust +impl Solution { + pub fn reverse_list(head: Option>) -> Option> { + fn rev( + mut head: Option>, + mut pre: Option>, + ) -> Option> { + if let Some(mut node) = head.take() { + let cur = node.next; + node.next = pre; + pre = Some(node); + return rev(cur, pre); + } + pre + } + rev(head, None) + } +} +``` +### C#: +三指针法, 感觉会更直观: + +```cs +public LinkNumbers Reverse() +{ + ///用三指针,写的过程中能够弥补二指针在翻转过程中的想象 + LinkNumbers pre = null; + var move = root; + var next = root; + + while (next != null) + { + next = next.linknext; + move.linknext = pre; + pre = move; + move = next; + } + root = pre; + return root; +} + +///LinkNumbers的定义 +public class LinkNumbers +{ + /// + /// 链表值 + /// + public int value { get; set; } + + /// + /// 链表指针 + /// + public LinkNumbers linknext { get; set; } +} +``` + +## 其他解法 + +### 使用虚拟头结点解决链表反转 + +> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈) + +```java +// 迭代方法:增加虚头结点,使用头插法实现链表翻转 +public static ListNode reverseList1(ListNode head) { + // 创建虚头结点 + ListNode dumpyHead = new ListNode(-1); + dumpyHead.next = null; + // 遍历所有节点 + ListNode cur = head; + while(cur != null){ + ListNode temp = cur.next; + // 头插法 + cur.next = dumpyHead.next; + dumpyHead.next = cur; + cur = temp; + } + return dumpyHead.next; +} +``` + + + +### 使用栈解决反转链表的问题 + +* 首先将所有的结点入栈 +* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 + +```java +public ListNode reverseList(ListNode head) { + // 如果链表为空,则返回空 + if (head == null) return null; + // 如果链表中只有只有一个元素,则直接返回 + if (head.next == null) return head; + // 创建栈 每一个结点都入栈 + Stack stack = new Stack<>(); + ListNode cur = head; + while (cur != null) { + stack.push(cur); + cur = cur.next; + } + // 创建一个虚拟头结点 + ListNode pHead = new ListNode(0); + cur = pHead; + while (!stack.isEmpty()) { + ListNode node = stack.pop(); + cur.next = node; + cur = cur.next; + } + // 最后一个元素的next要赋值为空 + cur.next = null; + return pHead.next; +} +``` + +> 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` +![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) + +

+ + + diff --git "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" index c63c998dc7..5a57939af2 100644 --- "a/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" +++ "b/problems/0206.\347\277\273\350\275\254\351\223\276\350\241\250.md" @@ -17,14 +17,12 @@ 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 - -本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。 @@ -51,9 +49,7 @@ 最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。 -# C++代码 - -## 双指针法 +### 双指针法 ```CPP class Solution { public: @@ -76,7 +72,7 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) -## 递归法 +### 递归法 递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 @@ -137,8 +133,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // 双指针 class Solution { @@ -198,7 +194,8 @@ class Solution { } ``` -Python +### Python: + ```python (版本一)双指针法 # Definition for singly-linked list. @@ -219,8 +216,6 @@ class Solution: return pre ``` -Python递归法: - ```python (版本二)递归法 # Definition for singly-linked list. @@ -242,7 +237,7 @@ class Solution: -Go: +### Go: ```go //双指针 @@ -273,7 +268,7 @@ func help(pre, head *ListNode)*ListNode{ } ``` -javaScript: +### JavaScript: ```js /** @@ -328,7 +323,7 @@ var reverseList = function(head) { }; ``` -TypeScript: +### TypeScript: ```typescript // 双指针法 @@ -376,7 +371,7 @@ function reverseList(head: ListNode | null): ListNode | null { }; ``` -Ruby: +### Ruby: ```ruby # 双指针 @@ -421,7 +416,8 @@ def reverse(pre, cur) end ``` -Kotlin: +### Kotlin: + ```Kotlin fun reverseList(head: ListNode?): ListNode? { var pre: ListNode? = null @@ -471,7 +467,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift /// 双指针法 (迭代) /// - Parameter head: 头结点 @@ -508,8 +505,9 @@ func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { } ``` -C: +### C: 双指针法: + ```c struct ListNode* reverseList(struct ListNode* head){ //保存cur的下一个结点 @@ -549,7 +547,8 @@ struct ListNode* reverseList(struct ListNode* head){ -PHP: +### PHP: + ```php // 双指针法: function reverseList($head) { @@ -565,8 +564,9 @@ function reverseList($head) { } ``` -Scala: +### Scala: 双指针法: + ```scala object Solution { def reverseList(head: ListNode): ListNode = { @@ -601,7 +601,7 @@ object Solution { } ``` -Rust: +### Rust: 双指针法: ```rust @@ -640,7 +640,7 @@ impl Solution { } } ``` -C#: +### C#: 三指针法, 感觉会更直观: ```cs @@ -677,11 +677,11 @@ public class LinkNumbers } ``` +## 其他解法 +### 使用虚拟头结点解决链表反转 -## 使用虚拟头结点解决链表翻转 - -> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈) +> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈) ```java // 迭代方法:增加虚头结点,使用头插法实现链表翻转 @@ -704,7 +704,7 @@ public static ListNode reverseList1(ListNode head) { -## 使用栈解决反转链表的问题 +### 使用栈解决反转链表的问题 * 首先将所有的结点入栈 * 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 @@ -743,4 +743,3 @@ public ListNode reverseList(ListNode head) { - From 2acceb7474b60ad4aa16eef4b9bf94657ac85aeb Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:00:29 +0800 Subject: [PATCH 028/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20024.=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 37 +- ...15\350\275\254\351\223\276\350\241\250.md" | 745 ------------------ 2 files changed, 23 insertions(+), 759 deletions(-) delete mode 100644 "problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" diff --git "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" index d7c03b645b..c612c4b3b9 100644 --- "a/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0024.\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 24. 两两交换链表中的节点 +# 24. 两两交换链表中的节点 [力扣题目链接](https://leetcode.cn/problems/swap-nodes-in-pairs/) @@ -16,9 +16,11 @@ 24.两两交换链表中的节点-题意 -## 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -《代码随想录》算法公开课:[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 这道题目正常模拟就可以了。 @@ -88,7 +90,8 @@ public: ## 其他语言版本 -C: +### C: + ```c /** * Definition for singly-linked list. @@ -132,7 +135,7 @@ struct ListNode* swapPairs(struct ListNode* head){ } ``` -Java: +### Java: ```Java // 递归版本 @@ -176,7 +179,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 递归版本 # Definition for singly-linked list. @@ -226,7 +230,8 @@ class Solution: ``` -Go: +### Go: + ```go func swapPairs(head *ListNode) *ListNode { dummy := &ListNode{ @@ -262,7 +267,8 @@ func swapPairs(head *ListNode) *ListNode { } ``` -Javascript: +### Javascript: + ```javascript var swapPairs = function (head) { let ret = new ListNode(0, head), temp = ret; @@ -277,7 +283,7 @@ var swapPairs = function (head) { }; ``` -TypeScript: +### TypeScript: ```typescript function swapPairs(head: ListNode | null): ListNode | null { @@ -296,7 +302,7 @@ function swapPairs(head: ListNode | null): ListNode | null { }; ``` -Kotlin: +### Kotlin: ```kotlin fun swapPairs(head: ListNode?): ListNode? { @@ -316,7 +322,8 @@ fun swapPairs(head: ListNode?): ListNode? { } ``` -Swift: +### Swift: + ```swift func swapPairs(_ head: ListNode?) -> ListNode? { if head == nil || head?.next == nil { @@ -337,7 +344,8 @@ func swapPairs(_ head: ListNode?) -> ListNode? { return dummyHead.next } ``` -Scala: +### Scala: + ```scala // 虚拟头节点 object Solution { @@ -361,7 +369,8 @@ object Solution { } ``` -PHP: +### PHP: + ```php //虚拟头结点 function swapPairs($head) { @@ -404,7 +413,7 @@ function swapPairs($head) } ``` -Rust: +### Rust: ```rust // 虚拟头节点 diff --git "a/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" "b/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" deleted file mode 100644 index 5a57939af2..0000000000 --- "a/problems/0206.\345\217\215\350\275\254\351\223\276\350\241\250.md" +++ /dev/null @@ -1,745 +0,0 @@ -

- - - -

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - -> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。 - -# 206.反转链表 - -[力扣题目链接](https://leetcode.cn/problems/reverse-linked-list/) - -题意:反转一个单链表。 - -示例: -输入: 1->2->3->4->5->NULL -输出: 5->4->3->2->1->NULL - -## 算法公开课 - -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - -## 思路 - -如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。 - -其实只需要改变链表的next指针的指向,直接将链表反转 ,而不用重新定义一个新的链表,如图所示: - - -![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png) - -之前链表的头节点是元素1, 反转之后头结点就是元素5 ,这里并没有添加或者删除节点,仅仅是改变next指针的方向。 - -那么接下来看一看是如何反转的呢? - -我们拿有示例中的链表来举例,如动画所示:(纠正:动画应该是先移动pre,在移动cur) - -![](https://code-thinking.cdn.bcebos.com/gifs/206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.gif) - -首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。 - -然后就要开始反转了,首先要把 cur->next 节点用tmp指针保存一下,也就是保存一下这个节点。 - -为什么要保存一下这个节点呢,因为接下来要改变 cur->next 的指向了,将cur->next 指向pre ,此时已经反转了第一个节点了。 - -接下来,就是循环走如下代码逻辑了,继续移动pre和cur指针。 - -最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。 - -### 双指针法 -```CPP -class Solution { -public: - ListNode* reverseList(ListNode* head) { - ListNode* temp; // 保存cur的下一个节点 - ListNode* cur = head; - ListNode* pre = NULL; - while(cur) { - temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next - cur->next = pre; // 翻转操作 - // 更新pre 和 cur指针 - pre = cur; - cur = temp; - } - return pre; - } -}; -``` - -* 时间复杂度: O(n) -* 空间复杂度: O(1) - -### 递归法 - -递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。 - -关键是初始化的地方,可能有的同学会不理解, 可以看到双指针法中初始化 cur = head,pre = NULL,在递归法中可以从如下代码看出初始化的逻辑也是一样的,只不过写法变了。 - -具体可以看代码(已经详细注释),**双指针法写出来之后,理解如下递归写法就不难了,代码逻辑都是一样的。** -```CPP -class Solution { -public: - ListNode* reverse(ListNode* pre,ListNode* cur){ - if(cur == NULL) return pre; - ListNode* temp = cur->next; - cur->next = pre; - // 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步 - // pre = cur; - // cur = temp; - return reverse(cur,temp); - } - ListNode* reverseList(ListNode* head) { - // 和双指针法初始化是一样的逻辑 - // ListNode* cur = head; - // ListNode* pre = NULL; - return reverse(NULL, head); - } - -}; -``` - -* 时间复杂度: O(n), 要递归处理链表的每个节点 -* 空间复杂度: O(n), 递归调用了 n 层栈空间 - -我们可以发现,上面的递归写法和双指针法实质上都是从前往后翻转指针指向,其实还有另外一种与双指针法不同思路的递归写法:从后往前翻转指针指向。 - -具体代码如下(带详细注释): - -```CPP -class Solution { -public: - ListNode* reverseList(ListNode* head) { - // 边缘条件判断 - if(head == NULL) return NULL; - if (head->next == NULL) return head; - - // 递归调用,翻转第二个节点开始往后的链表 - ListNode *last = reverseList(head->next); - // 翻转头节点与第二个节点的指向 - head->next->next = head; - // 此时的 head 节点为尾节点,next 需要指向 NULL - head->next = NULL; - return last; - } -}; -``` - -* 时间复杂度: O(n) -* 空间复杂度: O(n) - - -## 其他语言版本 - -### Java: - -```java -// 双指针 -class Solution { - public ListNode reverseList(ListNode head) { - ListNode prev = null; - ListNode cur = head; - ListNode temp = null; - while (cur != null) { - temp = cur.next;// 保存下一个节点 - cur.next = prev; - prev = cur; - cur = temp; - } - return prev; - } -} -``` - -```java -// 递归 -class Solution { - public ListNode reverseList(ListNode head) { - return reverse(null, head); - } - - private ListNode reverse(ListNode prev, ListNode cur) { - if (cur == null) { - return prev; - } - ListNode temp = null; - temp = cur.next;// 先保存下一个节点 - cur.next = prev;// 反转 - // 更新prev、cur位置 - // prev = cur; - // cur = temp; - return reverse(cur, temp); - } -} -``` - -```java -// 从后向前递归 -class Solution { - ListNode reverseList(ListNode head) { - // 边缘条件判断 - if(head == null) return null; - if (head.next == null) return head; - - // 递归调用,翻转第二个节点开始往后的链表 - ListNode last = reverseList(head.next); - // 翻转头节点与第二个节点的指向 - head.next.next = head; - // 此时的 head 节点为尾节点,next 需要指向 NULL - head.next = null; - return last; - } -} -``` - -### Python: - -```python -(版本一)双指针法 -# Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def reverseList(self, head: ListNode) -> ListNode: - cur = head - pre = None - while cur: - temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next - cur.next = pre #反转 - #更新pre、cur指针 - pre = cur - cur = temp - return pre -``` - -```python -(版本二)递归法 -# Definition for singly-linked list. -# class ListNode: -# def __init__(self, val=0, next=None): -# self.val = val -# self.next = next -class Solution: - def reverseList(self, head: ListNode) -> ListNode: - return self.reverse(head, None) - def reverse(self, cur: ListNode, pre: ListNode) -> ListNode: - if cur == None: - return pre - temp = cur.next - cur.next = pre - return self.reverse(temp, cur) - -``` - - - -### Go: - -```go -//双指针 -func reverseList(head *ListNode) *ListNode { - var pre *ListNode - cur := head - for cur != nil { - next := cur.Next - cur.Next = pre - pre = cur - cur = next - } - return pre -} - -//递归 -func reverseList(head *ListNode) *ListNode { - return help(nil, head) -} - -func help(pre, head *ListNode)*ListNode{ - if head == nil { - return pre - } - next := head.Next - head.Next = pre - return help(head, next) -} - -``` -### JavaScript: - -```js -/** - * @param {ListNode} head - * @return {ListNode} - */ - -// 双指针: -var reverseList = function(head) { - if(!head || !head.next) return head; - let temp = null, pre = null, cur = head; - while(cur) { - temp = cur.next; - cur.next = pre; - pre = cur; - cur = temp; - } - // temp = cur = null; - return pre; -}; - -// 递归: -var reverse = function(pre, head) { - if(!head) return pre; - const temp = head.next; - head.next = pre; - pre = head - return reverse(pre, temp); -} - -var reverseList = function(head) { - return reverse(null, head); -}; - -// 递归2 -var reverse = function(head) { - if(!head || !head.next) return head; - // 从后往前翻 - const pre = reverse(head.next); - head.next = pre.next; - pre.next = head; - return head; -} - -var reverseList = function(head) { - let cur = head; - while(cur && cur.next) { - cur = cur.next; - } - reverse(head); - return cur; -}; -``` - -### TypeScript: - -```typescript -// 双指针法 -function reverseList(head: ListNode | null): ListNode | null { - let preNode: ListNode | null = null, - curNode: ListNode | null = head, - tempNode: ListNode | null; - while (curNode) { - tempNode = curNode.next; - curNode.next = preNode; - preNode = curNode; - curNode = tempNode; - } - return preNode; -}; - -// 递归(从前往后翻转) -function reverseList(head: ListNode | null): ListNode | null { - function recur(preNode: ListNode | null, curNode: ListNode | null): ListNode | null { - if (curNode === null) return preNode; - let tempNode: ListNode | null = curNode.next; - curNode.next = preNode; - preNode = curNode; - curNode = tempNode; - return recur(preNode, curNode); - } - return recur(null, head); -}; - -// 递归(从后往前翻转) -function reverseList(head: ListNode | null): ListNode | null { - if (head === null) return null; - let newHead: ListNode | null; - function recur(node: ListNode | null, preNode: ListNode | null): void { - if (node.next === null) { - newHead = node; - newHead.next = preNode; - } else { - recur(node.next, node); - node.next = preNode; - } - } - recur(head, null); - return newHead; -}; -``` - -### Ruby: - -```ruby -# 双指针 -# Definition for singly-linked list. -# class ListNode -# attr_accessor :val, :next -# def initialize(val = 0, _next = nil) -# @val = val -# @next = _next -# end -# end -def reverse_list(head) - # return nil if head.nil? # 循环判断条件亦能起到相同作用因此不必单独判断 - cur, per = head, nil - until cur.nil? - tem = cur.next - cur.next = per - per = cur - cur = tem - end - per -end - -# 递归 -# Definition for singly-linked list. -# class ListNode -# attr_accessor :val, :next -# def initialize(val = 0, _next = nil) -# @val = val -# @next = _next -# end -# end -def reverse_list(head) - reverse(nil, head) -end - -def reverse(pre, cur) - return pre if cur.nil? - tem = cur.next - cur.next = pre - reverse(cur, tem) # 通过递归实现双指针法中的更新操作 -end -``` - -### Kotlin: - -```Kotlin -fun reverseList(head: ListNode?): ListNode? { - var pre: ListNode? = null - var cur = head - while (cur != null) { - val temp = cur.next - cur.next = pre - pre = cur - cur = temp - } - return pre -} -``` -```kotlin -/** - * Example: - * var li = ListNode(5) - * var v = li.`val` - * Definition for singly-linked list. - * class ListNode(var `val`: Int) { - * var next: ListNode? = null - * } - */ -class Solution { - fun reverseList(head: ListNode?): ListNode? { - // temp用来存储临时的节点 - var temp: ListNode? - // cur用来遍历链表 - var cur: ListNode? = head - // pre用来作为链表反转的工具 - // pre是比pre前一位的节点 - var pre: ListNode? = null - while (cur != null) { - // 临时存储原本cur的下一个节点 - temp = cur.next - // 使cur下一节点地址为它之前的 - cur.next = pre - // 之后随着cur的遍历移动pre - pre = cur; - // 移动cur遍历链表各个节点 - cur = temp; - } - // 由于开头使用pre为null,所以cur等于链表本身长度+1, - // 此时pre在cur前一位,所以此时pre为头节点 - return pre; - } -} -``` - -### Swift: - -```swift -/// 双指针法 (迭代) -/// - Parameter head: 头结点 -/// - Returns: 翻转后的链表头结点 -func reverseList(_ head: ListNode?) -> ListNode? { - if head == nil || head?.next == nil { - return head - } - var pre: ListNode? = nil - var cur: ListNode? = head - var temp: ListNode? = nil - while cur != nil { - temp = cur?.next - cur?.next = pre - pre = cur - cur = temp - } - return pre -} - -/// 递归 -/// - Parameter head: 头结点 -/// - Returns: 翻转后的链表头结点 -func reverseList2(_ head: ListNode?) -> ListNode? { - return reverse(pre: nil, cur: head) -} -func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { - if cur == nil { - return pre - } - let temp: ListNode? = cur?.next - cur?.next = pre - return reverse(pre: cur, cur: temp) -} -``` - -### C: -双指针法: - -```c -struct ListNode* reverseList(struct ListNode* head){ - //保存cur的下一个结点 - struct ListNode* temp; - //pre指针指向前一个当前结点的前一个结点 - struct ListNode* pre = NULL; - //用head代替cur,也可以再定义一个cur结点指向head。 - while(head) { - //保存下一个结点的位置 - temp = head->next; - //翻转操作 - head->next = pre; - //更新结点 - pre = head; - head = temp; - } - return pre; -} -``` - -递归法: -```c -struct ListNode* reverse(struct ListNode* pre, struct ListNode* cur) { - if(!cur) - return pre; - struct ListNode* temp = cur->next; - cur->next = pre; - //将cur作为pre传入下一层 - //将temp作为cur传入下一层,改变其指针指向当前cur - return reverse(cur, temp); -} - -struct ListNode* reverseList(struct ListNode* head){ - return reverse(NULL, head); -} -``` - - - -### PHP: - -```php -// 双指针法: -function reverseList($head) { - $cur = $head; - $pre = NULL; - while($cur){ - $temp = $cur->next; - $cur->next = $pre; - $pre = $cur; - $cur = $temp; - } - return $pre; - } -``` - -### Scala: -双指针法: - -```scala -object Solution { - def reverseList(head: ListNode): ListNode = { - var pre: ListNode = null - var cur = head - while (cur != null) { - var tmp = cur.next - cur.next = pre - pre = cur - cur = tmp - } - pre - } -} -``` -递归法: -```scala -object Solution { - def reverseList(head: ListNode): ListNode = { - reverse(null, head) - } - - def reverse(pre: ListNode, cur: ListNode): ListNode = { - if (cur == null) { - return pre // 如果当前cur为空,则返回pre - } - val tmp: ListNode = cur.next - cur.next = pre - reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 - } - -} -``` - -### Rust: -双指针法: - -```rust -impl Solution { - pub fn reverse_list(head: Option>) -> Option> { - let mut cur = head; - let mut pre = None; - while let Some(mut node) = cur.take() { - cur = node.next; - node.next = pre; - pre = Some(node); - } - pre - } -} -``` - -递归法: - -```rust -impl Solution { - pub fn reverse_list(head: Option>) -> Option> { - fn rev( - mut head: Option>, - mut pre: Option>, - ) -> Option> { - if let Some(mut node) = head.take() { - let cur = node.next; - node.next = pre; - pre = Some(node); - return rev(cur, pre); - } - pre - } - rev(head, None) - } -} -``` -### C#: -三指针法, 感觉会更直观: - -```cs -public LinkNumbers Reverse() -{ - ///用三指针,写的过程中能够弥补二指针在翻转过程中的想象 - LinkNumbers pre = null; - var move = root; - var next = root; - - while (next != null) - { - next = next.linknext; - move.linknext = pre; - pre = move; - move = next; - } - root = pre; - return root; -} - -///LinkNumbers的定义 -public class LinkNumbers -{ - /// - /// 链表值 - /// - public int value { get; set; } - - /// - /// 链表指针 - /// - public LinkNumbers linknext { get; set; } -} -``` - -## 其他解法 - -### 使用虚拟头结点解决链表反转 - -> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈) - -```java -// 迭代方法:增加虚头结点,使用头插法实现链表翻转 -public static ListNode reverseList1(ListNode head) { - // 创建虚头结点 - ListNode dumpyHead = new ListNode(-1); - dumpyHead.next = null; - // 遍历所有节点 - ListNode cur = head; - while(cur != null){ - ListNode temp = cur.next; - // 头插法 - cur.next = dumpyHead.next; - dumpyHead.next = cur; - cur = temp; - } - return dumpyHead.next; -} -``` - - - -### 使用栈解决反转链表的问题 - -* 首先将所有的结点入栈 -* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 - -```java -public ListNode reverseList(ListNode head) { - // 如果链表为空,则返回空 - if (head == null) return null; - // 如果链表中只有只有一个元素,则直接返回 - if (head.next == null) return head; - // 创建栈 每一个结点都入栈 - Stack stack = new Stack<>(); - ListNode cur = head; - while (cur != null) { - stack.push(cur); - cur = cur.next; - } - // 创建一个虚拟头结点 - ListNode pHead = new ListNode(0); - cur = pHead; - while (!stack.isEmpty()) { - ListNode node = stack.pop(); - cur.next = node; - cur = cur.next; - } - // 最后一个元素的next要赋值为空 - cur.next = null; - return pHead.next; -} -``` - -> 采用这种方法需要注意一点。就是当整个出栈循环结束以后,cur正好指向原来链表的第一个结点,而此时结点1中的next指向的是结点2,因此最后还需要`cur.next = null` -![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png) - -

- - - From a64e11b09ad66f4a1ea22cdc3603e5cde85d88ef Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:13:42 +0800 Subject: [PATCH 029/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20019.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?n=E4=B8=AA=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4N\344\270\252\350\212\202\347\202\271.md" | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" index 84eac96b84..1c95ad5b2f 100644 --- "a/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" +++ "b/problems/0019.\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271.md" @@ -7,7 +7,7 @@ -## 19.删除链表的倒数第N个节点 +# 19.删除链表的倒数第N个节点 [力扣题目链接](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/) @@ -31,10 +31,12 @@ 输入:head = [1,2], n = 1 输出:[1] +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频再看本篇题解,更有助于大家对链表的理解。** -## 思路 -《代码随想录》算法公开课:[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。 @@ -93,7 +95,7 @@ public: ## 其他语言版本 -java: +### Java: ```java public ListNode removeNthFromEnd(ListNode head, int n){ @@ -120,7 +122,8 @@ public ListNode removeNthFromEnd(ListNode head, int n){ } ``` -Python: +### Python: + ```python # Definition for singly-linked list. # class ListNode: @@ -151,7 +154,8 @@ class Solution: return dummy_head.next ``` -Go: +### Go: + ```Go /** * Definition for singly-linked list. @@ -178,7 +182,7 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode { } ``` -JavaScript: +### JavaScript: ```js /** @@ -198,7 +202,7 @@ var removeNthFromEnd = function(head, n) { return ret.next; }; ``` -TypeScript: +### TypeScript: 版本一(快慢指针法): @@ -263,7 +267,7 @@ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { }; ``` -Kotlin: +### Kotlin: ```Kotlin fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { @@ -284,7 +288,8 @@ fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { } ``` -Swift: +### Swift: + ```swift func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { if head == nil { @@ -309,8 +314,8 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { } ``` +### PHP: -PHP: ```php function removeNthFromEnd($head, $n) { // 设置虚拟头节点 @@ -332,7 +337,8 @@ function removeNthFromEnd($head, $n) { } ``` -Scala: +### Scala: + ```scala object Solution { def removeNthFromEnd(head: ListNode, n: Int): ListNode = { @@ -356,7 +362,8 @@ object Solution { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn remove_nth_from_end(head: Option>, mut n: i32) -> Option> { @@ -377,7 +384,8 @@ impl Solution { } } ``` -C语言 +### C: + ```c /**c语言单链表的定义 * Definition for singly-linked list. @@ -412,7 +420,8 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { ``` -C#: +### C#: + ```csharp public class Solution { public ListNode RemoveNthFromEnd(ListNode head, int n) { From 8eb214c06025e359d9c4a40d87d4e3075f5d8bdf Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:23:24 +0800 Subject: [PATCH 030/161] =?UTF-8?q?=E9=9D=A2=E8=AF=95=E9=A2=98=2002.07.?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\276\350\241\250\347\233\270\344\272\244.md" | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" index 75e0311630..833eadca6a 100644 --- "a/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" +++ "b/problems/\351\235\242\350\257\225\351\242\23002.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" @@ -34,6 +34,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) + ## 思路 @@ -101,7 +102,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java public class Solution { @@ -150,8 +151,7 @@ public class Solution { } ``` - -Python: +### Python: ```python @@ -280,7 +280,7 @@ class Solution: return pointerA ``` -Go: +### Go: ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { @@ -341,7 +341,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -JavaScript: +### JavaScript: ```js var getListLen = function(head) { @@ -376,7 +376,7 @@ var getIntersectionNode = function(headA, headB) { }; ``` -TypeScript: +### TypeScript: ```typescript function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { @@ -413,7 +413,7 @@ function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): Li }; ``` -C: +### C: ```c ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { @@ -452,7 +452,7 @@ ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -508,3 +508,4 @@ object Solution { + From 17d56ed722d41a8d463de2de71c72345a88a2df5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:30:49 +0800 Subject: [PATCH 031/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200142.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\275\242\351\223\276\350\241\250.md" | 11 +++---- ...\345\275\242\351\223\276\350\241\250II.md" | 30 +++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" index 7d7121a0ba..b1f42ba979 100644 --- "a/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" +++ "b/problems/0141.\347\216\257\345\275\242\351\223\276\350\241\250.md" @@ -70,7 +70,7 @@ public: ## 其他语言版本 -### Java +### Java: ```java public class Solution { @@ -90,7 +90,7 @@ public class Solution { } ``` -### Python +### Python: ```python class Solution: @@ -105,7 +105,7 @@ class Solution: return False ``` -### Go +### Go: ```go func hasCycle(head *ListNode) bool { @@ -125,7 +125,7 @@ func hasCycle(head *ListNode) bool { } ``` -### JavaScript +### JavaScript: ```js var hasCycle = function(head) { @@ -141,7 +141,7 @@ var hasCycle = function(head) { }; ``` -### TypeScript +### TypeScript: ```typescript function hasCycle(head: ListNode | null): boolean { @@ -163,3 +163,4 @@ function hasCycle(head: ListNode | null): boolean { + diff --git "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" index f87d2cd914..d20101a706 100644 --- "a/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" +++ "b/problems/0142.\347\216\257\345\275\242\351\223\276\350\241\250II.md" @@ -11,7 +11,7 @@ > 找到有没有环已经很不容易了,还要让我找到环的入口? -## 142.环形链表II +# 142.环形链表II [力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/) @@ -24,9 +24,11 @@ ![循环链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20200816110112704.png) -## 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。** -《代码随想录》算法公开课:[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +## 思路 这道题目,不仅考察对链表的操作,而且还需要一些数学运算。 @@ -148,7 +150,7 @@ public: * 时间复杂度: O(n),快慢指针相遇前,指针走的次数小于链表长度,快慢指针相遇后,两个index指针走的次数也小于链表长度,总体为走的次数小于 2n * 空间复杂度: O(1) -## 补充 +### 补充 在推理过程中,大家可能有一个疑问就是:**为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y 呢?** @@ -190,8 +192,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java public class Solution { @@ -217,8 +218,7 @@ public class Solution { } ``` - -Python: +### Python: ```python (版本一)快慢指针法 @@ -270,7 +270,7 @@ class Solution: return None ``` -Go: +### Go: ```go func detectCycle(head *ListNode) *ListNode { @@ -290,7 +290,7 @@ func detectCycle(head *ListNode) *ListNode { } ``` -javaScript +### JavaScript ```js // 两种循环实现方式 @@ -334,7 +334,7 @@ var detectCycle = function(head) { }; ``` -TypeScript: +### TypeScript: ```typescript function detectCycle(head: ListNode | null): ListNode | null { @@ -356,7 +356,7 @@ function detectCycle(head: ListNode | null): ListNode | null { }; ``` -Swift: +### Swift: ```swift class Solution { @@ -391,7 +391,7 @@ extension ListNode: Equatable { } ``` -C: +### C: ```c ListNode *detectCycle(ListNode *head) { @@ -410,7 +410,7 @@ ListNode *detectCycle(ListNode *head) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -437,7 +437,7 @@ object Solution { } ``` -C#: +### C#: ```CSharp public class Solution { From 28b58782375755eda70381aaab630e52b50992c8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 18 Jul 2023 15:33:00 +0800 Subject: [PATCH 032/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\276\350\241\250\346\200\273\347\273\223\347\257\207.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index cfbafc45b3..dacd4dee5d 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -3,7 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- +# 链表总结篇 ## 链表的理论基础 @@ -68,7 +68,7 @@ [链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html)使用双指针来找到两个链表的交点(引用完全相同,即:内存地址完全相同的交点) -## 环形链表 +### 环形链表 在[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中,讲解了在链表如何找环,以及如何找环的入口位置。 @@ -100,3 +100,4 @@ + From 826554682f465da29414c4ad6aa9ba391a54e05a Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 18 Jul 2023 16:51:54 +0800 Subject: [PATCH 033/161] =?UTF-8?q?Update=200674.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97.md=20abo?= =?UTF-8?q?ut=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\242\236\345\272\217\345\210\227.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 8cc270ec64..58365df3a7 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -303,6 +303,9 @@ func findLengthOfLCIS(nums []int) int { ``` Rust: + +>动态规划 + ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,6 +323,25 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` +> 贪心 + +```rust +impl Solution { + pub fn find_length_of_lcis(nums: Vec) -> i32 { + let (mut res, mut count) = (1, 1); + for i in 1..nums.len() { + if nums[i] > nums[i - 1] { + count += 1; + res = res.max(count); + continue; + } + count = 1; + } + res + } +} +``` + Javascript: > 动态规划: From e5d694da4d071b1286eb22db2477f55bb91b0cf6 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Wed, 19 Jul 2023 10:33:50 +0800 Subject: [PATCH 034/161] Update --- ...\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" "b/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index f8c07016a4..52a8448b51 100644 --- "a/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" +++ "b/problems/\345\211\215\345\272\217/\346\267\261\345\234\263\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -36,7 +36,7 @@ * 微众银行(总部深圳) * 招银科技(总部深圳) * 平安系列(平安科技、平安寿险、平安产险、平安金融、平安好医生等) -* Shopee(东南亚最大的电商平台,最近发展势头非常强劲) +* Shopee(21年有裁员风波) * 有赞(深圳) * 迅雷(总部深圳) * 金蝶(总部深圳) From 607e4ebd49c67b6ddd4fcdd91b248e74ed0ed75e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:18:19 +0800 Subject: [PATCH 035/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" index 0ab17ec02b..3055875a11 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -8,6 +8,8 @@ +# 哈希表理论基础 + ## 哈希表 首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。 From c628aa65d438aaf4933f2b029ae71c93e3cfce63 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:25:34 +0800 Subject: [PATCH 036/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\345\274\202\344\275\215\350\257\215.md" | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" index 4ea43947e8..f47d8b05b6 100644 --- "a/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" +++ "b/problems/0242.\346\234\211\346\225\210\347\232\204\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.md" @@ -7,7 +7,7 @@ > 数组就是简单的哈希表,但是数组的大小可不是无限开辟的 -## 242.有效的字母异位词 +# 242.有效的字母异位词 [力扣题目链接](https://leetcode.cn/problems/valid-anagram/) @@ -21,13 +21,14 @@ 输入: s = "rat", t = "car" 输出: false - **说明:** 你可以假设字符串只包含小写字母。 -## 思路 +## 算法公开课 -本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA) +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。 @@ -88,12 +89,10 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1) - - ## 其他语言版本 +### Java: -Java: ```java /** * 242. 有效的字母异位词 字典解法 @@ -121,7 +120,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -165,7 +164,7 @@ class Solution(object): return a_count == b_count ``` -Go: +### Go: ```go func isAnagram(s string, t string) bool { @@ -182,7 +181,7 @@ func isAnagram(s string, t string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -218,7 +217,7 @@ var isAnagram = function(s, t) { }; ``` -TypeScript: +### TypeScript: ```typescript function isAnagram(s: string, t: string): boolean { @@ -233,7 +232,7 @@ function isAnagram(s: string, t: string): boolean { }; ``` -Swift: +### Swift: ```Swift func isAnagram(_ s: String, _ t: String) -> Bool { @@ -257,7 +256,8 @@ func isAnagram(_ s: String, _ t: String) -> Bool { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -292,7 +292,8 @@ class Solution { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn is_anagram(s: String, t: String) -> bool { @@ -312,8 +313,8 @@ impl Solution { } ``` +### Scala: -Scala: ```scala object Solution { def isAnagram(s: String, t: String): Boolean = { @@ -337,8 +338,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public bool IsAnagram(string s, string t) { int sl=s.Length,tl=t.Length; @@ -360,11 +361,12 @@ C#: ## 相关题目 * [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html) -* 49.字母异位词分组 -* 438.找到字符串中所有字母异位词 +* [49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/) +* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)

+ From 19dfa98e4f936f4b5f73886e6c1503c07f913bf8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:30:12 +0800 Subject: [PATCH 037/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200349.=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\344\272\244\351\233\206.md" | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" index c2f6ef46ba..8daf5a35ba 100644 --- "a/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" +++ "b/problems/0349.\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206.md" @@ -10,7 +10,7 @@ > 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费! -## 349. 两个数组的交集 +# 349. 两个数组的交集 [力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/) @@ -22,9 +22,11 @@ 输出结果中的每个元素一定是唯一的。 我们可以不考虑输出结果的顺序。 -## 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -关于本题,我录制了讲解视频:[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),看视频配合题解,事半功倍。 +## 思路 这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。 @@ -118,8 +120,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java import java.util.HashSet; @@ -159,8 +160,9 @@ class Solution { } ``` -Python3: +### Python3: (版本一) 使用字典和集合 + ```python class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -206,7 +208,8 @@ class Solution: ``` -Go: +### Go: + ```go func intersection(nums1 []int, nums2 []int) []int { set:=make(map[int]struct{},0) // 用map模拟set @@ -227,7 +230,7 @@ func intersection(nums1 []int, nums2 []int) []int { } ``` -javaScript: +### JavaScript: ```js /** @@ -255,7 +258,7 @@ var intersection = function(nums1, nums2) { }; ``` -TypeScript: +### TypeScript: 版本一(正常解法): @@ -280,7 +283,7 @@ function intersection(nums1: number[], nums2: number[]): number[] { }; ``` -Swift: +### Swift: ```swift func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { @@ -298,7 +301,8 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -327,7 +331,8 @@ class Solution { } ``` -Rust: +### Rust: + ```rust use std::collections::HashSet; impl Solution { @@ -363,7 +368,8 @@ impl Solution { } ``` -C: +### C: + ```C int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){ @@ -394,7 +400,7 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re } ``` -Scala: +### Scala: 正常解法: ```scala @@ -439,8 +445,8 @@ object Solution { ``` +### C#: -C#: ```csharp public int[] Intersection(int[] nums1, int[] nums2) { if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0) @@ -461,11 +467,10 @@ C#: ``` ## 相关题目 -* 350.两个数组的交集 II +* [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)

- From 764b3e9e51608baf17183a5e36cab0888a6e46f9 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:34:27 +0800 Subject: [PATCH 038/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200202.=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\345\277\253\344\271\220\346\225\260.md" | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" index 7fe8cd8d75..4a77e2b67c 100644 --- "a/problems/0202.\345\277\253\344\271\220\346\225\260.md" +++ "b/problems/0202.\345\277\253\344\271\220\346\225\260.md" @@ -28,7 +28,7 @@ 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 -# 思路 +## 思路 这道题目看上去貌似一道数学问题,其实并不是! @@ -80,10 +80,10 @@ public: -# 其他语言版本 +## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean isHappy(int n) { @@ -107,8 +107,9 @@ class Solution { } ``` -Python: +### Python: (版本一)使用集合 + ```python class Solution: def isHappy(self, n: int) -> bool: @@ -131,7 +132,7 @@ class Solution: n, r = divmod(n, 10) new_num += r ** 2 return new_num - ``` +``` (版本二)使用集合 ```python class Solution: @@ -146,7 +147,7 @@ class Solution: if new_num==1: return True else: n = new_num return False -``` + ``` (版本三)使用数组 ```python class Solution: @@ -161,7 +162,7 @@ class Solution: if new_num==1: return True else: n = new_num return False -``` + ``` (版本四)使用快慢指针 ```python class Solution: @@ -180,7 +181,7 @@ class Solution: n, r = divmod(n, 10) new_num += r ** 2 return new_num -``` + ``` (版本五)使用集合+精简 ```python class Solution: @@ -192,7 +193,7 @@ class Solution: return False seen.add(n) return True -``` + ``` (版本六)使用数组+精简 ```python class Solution: @@ -204,8 +205,9 @@ class Solution: return False seen.append(n) return True -``` -Go: + ``` +### Go: + ```go func isHappy(n int) bool { m := make(map[int]bool) @@ -225,7 +227,7 @@ func getSum(n int) int { } ``` -javaScript: +### JavaScript: ```js var isHappy = function (n) { @@ -303,7 +305,7 @@ var isHappy = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript function isHappy(n: number): boolean { @@ -322,7 +324,7 @@ function isHappy(n: number): boolean { }; ``` -Swift: +### Swift: ```swift // number 每个位置上的数字的平方和 @@ -355,7 +357,8 @@ func isHappy(_ n: Int) -> Bool { } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -386,7 +389,8 @@ class Solution { } ``` -Rust: +### Rust: + ```Rust use std::collections::HashSet; impl Solution { @@ -416,7 +420,8 @@ impl Solution { } ``` -C: +### C: + ```C typedef struct HashNodeTag { int key; /* num */ @@ -473,8 +478,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public class Solution { private int getSum(int n) { @@ -500,3 +505,4 @@ public class Solution { + From 23950d1c3e8541420eaca3f55f0896a84d3ba007 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:43:14 +0800 Subject: [PATCH 039/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\346\225\260\344\271\213\345\222\214.md" | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index ca62e3edc2..e3fb0fb55f 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -5,7 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 1. 两数之和 +# 1. 两数之和 [力扣题目链接](https://leetcode.cn/problems/two-sum/) @@ -21,10 +21,12 @@ 所以返回 [0, 1] +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -## 思路 -建议看一下我录的这期视频:[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),结合本题解来学习,事半功倍。 +## 思路 很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。 @@ -128,8 +130,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; @@ -150,8 +152,9 @@ public int[] twoSum(int[] nums, int target) { } ``` -Python: +### Python: (版本一) 使用字典 + ```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: @@ -211,7 +214,7 @@ class Solution: return [i,j] ``` -Go: +### Go: ```go // 暴力解法 @@ -242,7 +245,7 @@ func twoSum(nums []int, target int) []int { } ``` -Rust +### Rust: ```rust use std::collections::HashMap; @@ -263,9 +266,7 @@ impl Solution { } } ``` -Rust - -``` +```rust use std::collections::HashMap; impl Solution { @@ -284,7 +285,7 @@ impl Solution { } ``` -Javascript +### Javascript: ```javascript var twoSum = function (nums, target) { @@ -299,7 +300,7 @@ var twoSum = function (nums, target) { }; ``` -TypeScript: +### TypeScript: ```typescript function twoSum(nums: number[], target: number): number[] { @@ -317,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] { }; ``` -php +### php: ```php function twoSum(array $nums, int $target): array @@ -337,7 +338,8 @@ function twoSum(array $nums, int $target): array } ``` -Swift: +### Swift: + ```swift func twoSum(_ nums: [Int], _ target: Int) -> [Int] { // 值: 下标 @@ -353,8 +355,8 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] { } ``` +### Scala: -Scala: ```scala object Solution { // 导入包 @@ -377,7 +379,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int[] TwoSum(int[] nums, int target) { @@ -396,7 +399,8 @@ public class Solution { } ``` -Dart: +### Dart: + ```dart List twoSum(List nums, int target) { var tmp = []; @@ -411,7 +415,8 @@ List twoSum(List nums, int target) { } ``` -C: +### C: + ```c From 2948791011238baa02f6beda8bb496e8c52fb6de Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:47:02 +0800 Subject: [PATCH 040/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200454.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\225\260\347\233\270\345\212\240II.md" | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" index 4a16d4f4a7..90c3733449 100644 --- "a/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" +++ "b/problems/0454.\345\233\233\346\225\260\347\233\270\345\212\240II.md" @@ -34,10 +34,12 @@ 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,map使用有技巧!LeetCode:454.四数相加II](https://www.bilibili.com/video/BV1Md4y1Q7Yh),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -本题视频讲解:[学透哈希表,map使用有技巧!LeetCode:454.四数相加II](https://www.bilibili.com/video/BV1Md4y1Q7Yh),结合视频在看本题解,事半功倍。 + +## 思路 本题咋眼一看好像和[0015.三数之和](https://programmercarl.com/0015.三数之和.html),[0018.四数之和](https://programmercarl.com/0018.四数之和.html)差不多,其实差很多。 @@ -92,8 +94,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { @@ -117,8 +119,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 使用字典 + ```python class Solution(object): def fourSumCount(self, nums1, nums2, nums3, nums4): @@ -179,7 +182,7 @@ class Solution: return cnt ``` -Go: +### Go: ```go func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { @@ -201,7 +204,7 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { } ``` -javaScript: +### JavaScript: ```js /** @@ -233,7 +236,7 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) { }; ``` -TypeScript: +### TypeScript: ```typescript function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { @@ -258,7 +261,7 @@ function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: }; ``` -PHP: +### PHP: ```php class Solution { @@ -291,8 +294,8 @@ class Solution { } ``` +### Swift: -Swift: ```swift func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int { // ab和: ab和出现次数 @@ -316,7 +319,8 @@ func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int] } ``` -Rust: +### Rust: + ```rust use std::collections::HashMap; impl Solution { @@ -342,8 +346,8 @@ impl Solution { } ``` +### Scala: -Scala: ```scala object Solution { // 导包 @@ -380,7 +384,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { Dictionary dic = new Dictionary(); @@ -411,3 +416,4 @@ public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + From 0922ede8c7ce3fedbd7494834beb224aa3da6311 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:49:56 +0800 Subject: [PATCH 041/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3.\350\265\216\351\207\221\344\277\241.md" | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" index e74cdf71fd..8122240ecc 100644 --- "a/problems/0383.\350\265\216\351\207\221\344\277\241.md" +++ "b/problems/0383.\350\265\216\351\207\221\344\277\241.md" @@ -34,7 +34,7 @@ canConstruct("aa", "aab") -> true * 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要 -## 暴力解法 +### 暴力解法 那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下: @@ -66,7 +66,7 @@ public: 这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。 -## 哈希解法 +### 哈希解法 因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。 @@ -112,8 +112,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public boolean canConstruct(String ransomNote, String magazine) { @@ -146,8 +146,9 @@ class Solution { ``` -Python: +### Python: (版本一)使用数组 + ```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: @@ -213,7 +214,7 @@ class Solution: return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote)) ``` -Go: +### Go: ```go func canConstruct(ransomNote string, magazine string) bool { @@ -231,7 +232,7 @@ func canConstruct(ransomNote string, magazine string) bool { } ``` -javaScript: +### JavaScript: ```js /** @@ -254,7 +255,7 @@ var canConstruct = function(ransomNote, magazine) { }; ``` -TypeScript: +### TypeScript: ```typescript function canConstruct(ransomNote: string, magazine: string): boolean { @@ -275,8 +276,8 @@ function canConstruct(ransomNote: string, magazine: string): boolean { }; ``` +### PHP: -PHP: ```php class Solution { /** @@ -301,7 +302,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { var record = Array(repeating: 0, count: 26); @@ -324,7 +326,8 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn can_construct(ransom_note: String, magazine: String) -> bool { @@ -347,7 +350,7 @@ impl Solution { } ``` -Scala: +### Scala: 版本一: 使用数组作为哈希表 ```scala @@ -411,8 +414,8 @@ object Solution { } ``` +### C#: -C#: ```csharp public bool CanConstruct(string ransomNote, string magazine) { if(ransomNote.Length > magazine.Length) return false; @@ -434,3 +437,4 @@ public bool CanConstruct(string ransomNote, string magazine) { + From eb2cdf24bf03dd143072a4260fb11b76ececcc37 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:55:09 +0800 Subject: [PATCH 042/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\225\260\344\271\213\345\222\214.md" | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" index 9cca779b21..4951c90cf1 100644 --- "a/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" @@ -26,14 +26,15 @@ [-1, -1, 2] ] +## 算法公开课 -# 思路 - -针对本题,我录制了视频讲解:[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 **注意[0, 0, 0, 0] 这组数据** -## 哈希解法 +## 思路 + +### 哈希解法 两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。 @@ -87,7 +88,7 @@ public: * 空间复杂度: O(n),额外的 set 开销 -## 双指针 +### 双指针 **其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。 @@ -166,9 +167,9 @@ public: * 空间复杂度: O(1) -## 去重逻辑的思考 +### 去重逻辑的思考 -### a的去重 +#### a的去重 说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right] @@ -188,7 +189,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳 if (nums[i] == nums[i + 1]) { // 去重操作 continue; } -``` +``` 那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。 @@ -208,7 +209,7 @@ if (i > 0 && nums[i] == nums[i - 1]) { 这是一个非常细节的思考过程。 -### b与c的去重 +#### b与c的去重 很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分) @@ -225,7 +226,7 @@ while (right > left) { } else { } } -``` +``` 但细想一下,这种去重其实对提升程序运行效率是没有帮助的。 @@ -238,7 +239,7 @@ while (right > left) { 所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。 -# 思考题 +## 思考题 既然三数之和可以使用双指针法,我们之前讲过的[1.两数之和](https://programmercarl.com/0001.两数之和.html),可不可以使用双指针法呢? @@ -254,8 +255,8 @@ while (right > left) { ## 其他语言版本 +### Java: -Java: ```Java class Solution { public List> threeSum(int[] nums) { @@ -297,8 +298,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 双指针 + ```Python class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: @@ -366,7 +368,7 @@ class Solution: return result ``` -Go: +### Go: ```Go func threeSum(nums []int) [][]int { @@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int { } ``` -javaScript: +### JavaScript: ```js var threeSum = function(nums) { @@ -512,7 +514,7 @@ var threeSum = function (nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function threeSum(nums: number[]): number[][] { @@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] { }; ``` -ruby: +### Ruby: + ```ruby def is_valid(strs) symbol_map = {')' => '(', '}' => '{', ']' => '['} @@ -571,8 +574,8 @@ def is_valid(strs) end ``` +### PHP: -PHP: ```php class Solution { /** @@ -613,7 +616,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift // 双指针法 func threeSum(_ nums: [Int]) -> [[Int]] { @@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] { } ``` -Rust: +### Rust: + ```Rust // 哈希解法 use std::collections::HashSet; @@ -718,7 +723,8 @@ impl Solution { } ``` -C: +### C: + ```C //qsort辅助cmp函数 int cmp(const void* ptr1, const void* ptr2) { @@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes } ``` -C#: +### C#: + ```csharp public class Solution { @@ -850,7 +857,8 @@ public class Solution } } ``` -Scala: +### Scala: + ```scala object Solution { // 导包 @@ -898,3 +906,4 @@ object Solution { + From 3fe673d804209e55705b2b7206d86e3fc6e87e18 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:59:05 +0800 Subject: [PATCH 043/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200018.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index a4d41d9bf9..28c20b7acd 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -27,9 +27,11 @@ [-2, 0, 0, 2] ] -# 思路 +## 算法公开课 -针对本题,我录制了视频讲解:[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。 @@ -141,22 +143,16 @@ if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) { if (nums[k] + nums[i] > target && nums[i] >= 0) { break; } -``` +``` 因为只要 nums[k] + nums[i] > target,那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。 不过这种剪枝 其实有点 小绕,大家能够理解 文章给的完整代码的剪枝 就够了。 - - - - - - ## 其他语言版本 +### Java: -Java: ```Java class Solution { public List> fourSum(int[] nums, int target) { @@ -206,8 +202,9 @@ class Solution { } ``` -Python: +### Python: (版本一) 双指针 + ```python class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: @@ -273,7 +270,8 @@ class Solution(object): ``` -Go: +### Go: + ```go func fourSum(nums []int, target int) [][]int { if len(nums) < 4 { @@ -323,7 +321,7 @@ func fourSum(nums []int, target int) [][]int { } ``` -javaScript: +### JavaScript: ```js /** @@ -359,7 +357,7 @@ var fourSum = function(nums, target) { }; ``` -TypeScript: +### TypeScript: ```typescript function fourSum(nums: number[], target: number): number[][] { @@ -400,7 +398,7 @@ function fourSum(nums: number[], target: number): number[][] { }; ``` -PHP: +### PHP: ```php class Solution { @@ -445,7 +443,8 @@ class Solution { } ``` -Swift: +### Swift: + ```swift func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { var res = [[Int]]() @@ -493,7 +492,8 @@ func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] { } ``` -C#: +### C#: + ```csharp public class Solution { @@ -555,7 +555,8 @@ public class Solution } ``` -Rust: +### Rust: + ```Rust use std::cmp::Ordering; impl Solution { @@ -603,7 +604,8 @@ impl Solution { } ``` -Scala: +### Scala: + ```scala object Solution { // 导包 @@ -651,3 +653,4 @@ object Solution { + From ddee6adbbba4db9466cf720307b2e44f10e22140 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:01:01 +0800 Subject: [PATCH 044/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\214\350\241\250\346\200\273\347\273\223.md" | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" index 9ee84f991b..6750636305 100644 --- "a/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" +++ "b/problems/\345\223\210\345\270\214\350\241\250\346\200\273\347\273\223.md" @@ -7,8 +7,10 @@ > 哈希表总结篇如约而至 +# 哈希表总结篇 -# 哈希表理论基础 + +## 哈希表理论基础 在[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)中,我们介绍了哈希表的基础理论知识,不同于枯燥的讲解,这里介绍了都是对刷题有帮助的理论知识点。 @@ -32,9 +34,9 @@ **只有对这些数据结构的底层实现很熟悉,才能灵活使用,否则很容易写出效率低下的程序**。 -# 哈希表经典题目 +## 哈希表经典题目 -## 数组作为哈希表 +### 数组作为哈希表 一些应用场景就是为数组量身定做的。 @@ -51,7 +53,7 @@ **上面两道题目用map确实可以,但使用map的空间消耗要比数组大一些,因为map要维护红黑树或者符号表,而且还要做哈希函数的运算。所以数组更加简单直接有效!** -## set作为哈希表 +### set作为哈希表 在[349. 两个数组的交集](https://programmercarl.com/0349.两个数组的交集.html)中我们给出了什么时候用数组就不行了,需要用set。 @@ -75,7 +77,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底 在[202.快乐数](https://programmercarl.com/0202.快乐数.html)中,我们再次使用了unordered_set来判断一个数是否重复出现过。 -## map作为哈希表 +### map作为哈希表 在[1.两数之和](https://programmercarl.com/0001.两数之和.html)中map正式登场。 @@ -110,7 +112,7 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 所以18. 四数之和,15.三数之和都推荐使用双指针法! -# 总结 +## 总结 对于哈希表的知识相信很多同学都知道,但是没有成体系。 @@ -123,9 +125,8 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层 - -

+ From 00ef6084c6ea18933e7a63923aad71addee70c6e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:32:11 +0800 Subject: [PATCH 045/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200344.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\255\227\347\254\246\344\270\262.md" | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" index 1c74f9aa46..8a4fed4574 100644 --- "a/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0344.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -26,10 +26,12 @@ 输入:["H","a","n","n","a","h"] 输出:["h","a","n","n","a","H"] +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),结合本题解一起看,事半功倍! + +## 思路 先说一说题外话: @@ -138,8 +140,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public void reverseString(char[] s) { @@ -173,8 +175,9 @@ class Solution { ``` -Python: +### Python: (版本一) 双指针 + ```python class Solution: def reverseString(self, s: List[str]) -> None: @@ -247,7 +250,8 @@ class Solution: s[:] = [s[i] for i in range(len(s) - 1, -1, -1)] ``` -Go: +### Go: + ```Go func reverseString(s []byte) { left := 0 @@ -260,7 +264,7 @@ func reverseString(s []byte) { } ``` -javaScript: +### JavaScript: ```js /** @@ -278,7 +282,7 @@ var reverse = function(s) { }; ``` -TypeScript: +### TypeScript: ```typescript /** @@ -299,7 +303,7 @@ function reverseString(s: string[]): void { }; ``` -Swift: +### Swift: ```swift // 双指针 - 元组 @@ -316,7 +320,8 @@ func reverseString(_ s: inout [Character]) { ``` -Rust: +### Rust: + ```Rust impl Solution { pub fn reverse_string(s: &mut Vec) { @@ -332,7 +337,8 @@ impl Solution { } ``` -C: +### C: + ```c void reverseString(char* s, int sSize){ int left = 0; @@ -347,7 +353,8 @@ void reverseString(char* s, int sSize){ } ``` -C#: +### C#: + ```csharp public class Solution { @@ -361,8 +368,8 @@ public class Solution } ``` +### PHP: -PHP: ```php // 双指针 // 一: @@ -392,7 +399,8 @@ function reverse(&$s, $start, $end) { } ``` -Scala: +### Scala: + ```scala object Solution { def reverseString(s: Array[Char]): Unit = { @@ -411,4 +419,3 @@ object Solution { - From 5cb250100b49e1b8109ddf01ba2109b257094b47 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:37:57 +0800 Subject: [PATCH 046/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200541.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\255\227\347\254\246\344\270\262II.md" | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" index 179395b3ac..80e662f9dd 100644 --- "a/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" +++ "b/problems/0541.\345\217\215\350\275\254\345\255\227\347\254\246\344\270\262II.md" @@ -23,9 +23,11 @@ 输入: s = "abcdefg", k = 2 输出: "bacdfeg" -# 思路 +## 算法公开课 -针对本题,我录制了视频讲解:[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),结合本题解一起看,事半功倍! +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目其实也是模拟,实现题目中规定的反转规则就可以了。 @@ -42,8 +44,6 @@ 那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。 -# C++代码 - 使用C++库函数reverse的版本如下: ```CPP @@ -129,7 +129,7 @@ public: ## 其他语言版本 -C: +### C: ```c char * reverseStr(char * s, int k){ @@ -152,7 +152,7 @@ char * reverseStr(char * s, int k){ } ``` -Java: +### Java: ```Java //解法一 @@ -256,7 +256,8 @@ class Solution { } } ``` -Python: +### Python: + ```python class Solution: def reverseStr(self, s: str, k: int) -> str: @@ -281,7 +282,7 @@ class Solution: return ''.join(res) ``` -Python3 (v2): +### Python3 (v2): ```python class Solution: @@ -296,7 +297,7 @@ class Solution: return s ``` -Go: +### Go: ```go func reverseStr(s string, k int) string { @@ -325,7 +326,7 @@ func reverse(b []byte) { } ``` -javaScript: +### JavaScript: ```js @@ -346,7 +347,7 @@ var reverseStr = function(s, k) { ``` -TypeScript: +### TypeScript: ```typescript function reverseStr(s: string, k: number): string { @@ -368,7 +369,7 @@ function reverseStr(s: string, k: number): string { }; ``` -Swift: +### Swift: ```swift func reverseStr(_ s: String, _ k: Int) -> String { @@ -388,7 +389,8 @@ func reverseStr(_ s: String, _ k: Int) -> String { } ``` -C#: +### C#: + ```csharp public class Solution { @@ -403,7 +405,7 @@ public class Solution } } ``` -Scala: +### Scala: 版本一: (正常解法) ```scala @@ -469,7 +471,7 @@ object Solution { } ``` -Rust: +### Rust: ```Rust impl Solution { @@ -503,4 +505,3 @@ impl Solution { - From 963eb8fc8e58094388bf6c0f27be7563d7a488b9 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:41:59 +0800 Subject: [PATCH 047/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87?= =?UTF-8?q?Offer05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\346\215\242\347\251\272\346\240\274.md" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" index dbad781ef3..fed08a5346 100644 --- "a/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" +++ "b/problems/\345\211\221\346\214\207Offer05.\346\233\277\346\215\242\347\251\272\346\240\274.md" @@ -15,7 +15,7 @@ 输入:s = "We are happy." 输出:"We%20are%20happy." -# 思路 +## 思路 如果想把这道题目做到极致,就不要只用额外的辅助空间了! @@ -86,7 +86,7 @@ public: * [142.环形链表II](https://programmercarl.com/0142.环形链表II.html) * [344.反转字符串](https://programmercarl.com/0344.反转字符串.html) -# 拓展 +## 拓展 这里也给大家拓展一下字符串和数组有什么差别, @@ -121,7 +121,8 @@ for (int i = 0; i < a.size(); i++) { ## 其他语言版本 -C: +### C: + ```C char* replaceSpace(char* s){ //统计空格数量 @@ -152,8 +153,8 @@ char* replaceSpace(char* s){ } ``` +### Java: -Java: ```Java //使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制 public static String replaceSpace(String s) { @@ -211,8 +212,8 @@ public String replaceSpace(String s) { } ``` +### Go: -Go: ```go // 遍历添加 func replaceSpace(s string) string { @@ -264,9 +265,10 @@ func replaceSpace(s string) string { +### python: + +因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1) -python: -#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1) (版本一)转换成列表,并且添加相匹配的空间,然后进行填充 ```python class Solution: @@ -328,7 +330,7 @@ class Solution: def replaceSpace(self, s: str) -> str: return s.replace(' ', '%20') ``` -javaScript: +### JavaScript: ```js /** @@ -366,7 +368,7 @@ javaScript: }; ``` -TypeScript: +### TypeScript: ```typescript function replaceSpace(s: string): string { @@ -393,7 +395,7 @@ function replaceSpace(s: string): string { }; ``` -Swift: +### Swift: ```swift func replaceSpace(_ s: String) -> String { @@ -434,7 +436,7 @@ func replaceSpace(_ s: String) -> String { } ``` -Scala: +### Scala: 方式一: 双指针 ```scala @@ -491,8 +493,8 @@ object Solution { } ``` +### PHP: -PHP: ```php function replaceSpace($s){ $sLen = strlen($s); @@ -527,7 +529,7 @@ function spaceLen($s){ } ``` -Rust +### Rust: ```Rust impl Solution { @@ -563,3 +565,4 @@ impl Solution { + From e70ca923440e656d4c62761fcff2ff40e9c17b32 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:47:12 +0800 Subject: [PATCH 048/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\347\232\204\345\215\225\350\257\215.md" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 6dd3cd4975..19ccb725a0 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -28,10 +28,11 @@ 输出: "example good a" 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),结合本题解一起看,事半功倍! +## 思路 **这道题目可以说是综合考察了字符串的多种操作。** @@ -204,8 +205,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -433,9 +433,10 @@ class Solution { } ``` -python: +### python: (版本一)先删除空白,然后整个反转,最后单词反转。 **因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** + ```Python class Solution: def reverseWords(self, s: str) -> str: @@ -467,7 +468,7 @@ class Solution: return " ".join(words) ``` -Go: +### Go: 版本一: @@ -571,7 +572,8 @@ func reverse(b *[]byte, left, right int) { -javaScript: +### JavaScript: + ```js /** * @param {string} s @@ -630,7 +632,7 @@ function reverse(strArr, start, end) { } ``` -TypeScript: +### TypeScript: ```typescript function reverseWords(s: string): string { @@ -689,7 +691,7 @@ function reverseWords(s: string): string { }; ``` -Swift: +### Swift: ```swift func reverseWords(_ s: String) -> String { @@ -766,7 +768,7 @@ func reverseWord(_ s: inout [Character]) { } ``` -Scala: +### Scala: ```scala object Solution { @@ -824,8 +826,8 @@ object Solution { } ``` +### PHP: -PHP: ```php function reverseWords($s) { $this->removeExtraSpaces($s); @@ -872,7 +874,7 @@ function reverseString(&$s, $start, $end) { return ; } ``` -Rust: +### Rust: ```Rust // 根据C++版本二思路进行实现 @@ -924,7 +926,7 @@ pub fn remove_extra_spaces(s: &mut Vec) { } } ``` -C: +### C: ```C // 翻转字符串中指定范围的字符 @@ -972,3 +974,4 @@ char * reverseWords(char * s){ + From 370a4d1c05b1204c652c00bcb90702947df11795 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 15:50:44 +0800 Subject: [PATCH 049/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87?= =?UTF-8?q?Offer58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\255\227\347\254\246\344\270\262.md" | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 6cd8845609..008b7915c3 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -24,7 +24,7 @@ 限制: 1 <= k < s.length <= 10000 -# 思路 +## 思路 为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。 @@ -71,7 +71,7 @@ public: 是不是发现这代码也太简单了,哈哈。 -# 总结 +## 总结 此时我们已经反转好多次字符串了,来一起回顾一下吧。 @@ -86,7 +86,7 @@ public: 好了,反转字符串一共就介绍到这里,相信大家此时对反转字符串的常见操作已经很了解了。 -# 题外话 +## 题外话 一些同学热衷于使用substr,来做这道题。 其实使用substr 和 反转 时间复杂度是一样的 ,都是O(n),但是使用substr申请了额外空间,所以空间复杂度是O(n),而反转方法的空间复杂度是O(1)。 @@ -96,7 +96,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```java class Solution { public String reverseLeftWords(String s, int n) { @@ -141,7 +142,7 @@ class Solution { } ``` -python: +### python: (版本一)使用切片 ```python @@ -211,7 +212,7 @@ class Solution: ``` -Go: +### Go: ```go func reverseLeftWords(s string, n int) string { @@ -234,8 +235,7 @@ func reverse(b []byte, left, right int){ } ``` - -JavaScript: +### JavaScript: ```javascript var reverseLeftWords = function(s, n) { @@ -279,7 +279,7 @@ var reverseLeftWords = function (s, n) { }; ``` -TypeScript: +### TypeScript: ```typescript function reverseLeftWords(s: string, n: number): string { @@ -311,7 +311,7 @@ function reverseLeftWords(s: string, n: number): string { }; ``` -Swift: +### Swift: ```swift func reverseLeftWords(_ s: String, _ n: Int) -> String { @@ -358,8 +358,7 @@ function reverse(&$s, $start, $end) { } ``` - -Scala: +### Scala: ```scala object Solution { @@ -388,7 +387,7 @@ object Solution { } ``` -Rust: +### Rust: ```Rust impl Solution { @@ -419,3 +418,4 @@ impl Solution { + From 5acebcc6f70f37d675f03356ecfc2f8f43b5949b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 16:00:41 +0800 Subject: [PATCH 050/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200028.=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0strStr=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index ac984d1a59..53b57fd534 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -28,7 +28,7 @@ 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。 -# 思路 +## 思路 本题是KMP 经典题目。 @@ -60,13 +60,13 @@ KMP的经典思想就是:**当出现字符串不匹配时,可以记录一部 读完本篇可以顺便把leetcode上28.实现strStr()题目做了。 -# 什么是KMP +### 什么是KMP 说到KMP,先说一下KMP这个名字是怎么来的,为什么叫做KMP呢。 因为是由这三位学者发明的:Knuth,Morris和Pratt,所以取了三位学者名字的首字母。所以叫做KMP -# KMP有什么用 +### KMP有什么用 KMP主要应用在字符串匹配上。 @@ -84,7 +84,7 @@ KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之 下面Carl就带大家把KMP的精髓,next数组弄清楚。 -# 什么是前缀表 +### 什么是前缀表 写过KMP的同学,一定都写过next数组,那么这个next数组究竟是个啥呢? @@ -122,7 +122,7 @@ next数组就是一个前缀表(prefix table)。 那么什么是前缀表:**记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。** -# 最长公共前后缀? +### 最长公共前后缀 文章中字符串的**前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串**。 @@ -144,7 +144,7 @@ next数组就是一个前缀表(prefix table)。 等等.....。 -# 为什么一定要用前缀表 +### 为什么一定要用前缀表 这就是前缀表,那为啥就能告诉我们 上次匹配的位置,并跳过去呢? @@ -163,7 +163,7 @@ next数组就是一个前缀表(prefix table)。 **很多介绍KMP的文章或者视频并没有把为什么要用前缀表?这个问题说清楚,而是直接默认使用前缀表。** -# 如何计算前缀表 +### 如何计算前缀表 接下来就要说一说怎么计算前缀表。 @@ -205,7 +205,7 @@ next数组就是一个前缀表(prefix table)。 最后就在文本串中找到了和模式串匹配的子串了。 -# 前缀表与next数组 +### 前缀表与next数组 很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢? @@ -217,7 +217,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 后面我会提供两种不同的实现代码,大家就明白了。 -# 使用next数组来匹配 +### 使用next数组来匹配 **以下我们以前缀表统一减一之后的next数组来做演示**。 @@ -229,7 +229,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 ![KMP精讲4](https://code-thinking.cdn.bcebos.com/gifs/KMP%E7%B2%BE%E8%AE%B24.gif) -# 时间复杂度分析 +### 时间复杂度分析 其中n为文本串长度,m为模式串长度,因为在匹配的过程中,根据前缀表不断调整匹配的位置,可以看出匹配的过程是O(n),之前还要单独生成next数组,时间复杂度是O(m)。所以整个KMP算法的时间复杂度是O(n+m)的。 @@ -239,7 +239,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减 都知道使用KMP算法,一定要构造next数组。 -# 构造next数组 +### 构造next数组 我们定义一个函数getNext来构建next数组,函数参数为指向next数组的指针,和一个字符串。 代码如下: @@ -338,7 +338,7 @@ void getNext(int* next, const string& s){ 得到了next数组之后,就要用这个来做匹配了。 -# 使用next数组来做匹配 +### 使用next数组来做匹配 在文本串s里 找是否出现过模式串t。 @@ -403,7 +403,7 @@ for (int i = 0; i < s.size(); i++) { // 注意i就从0开始 此时所有逻辑的代码都已经写出来了,力扣 28.实现strStr 题目的整体代码如下: -# 前缀表统一减一 C++代码实现 +### 前缀表统一减一 C++代码实现 ```CPP class Solution { @@ -447,7 +447,7 @@ public: * 时间复杂度: O(n + m) * 空间复杂度: O(m), 只需要保存字符串needle的前缀表 -# 前缀表(不减一)C++实现 +### 前缀表(不减一)C++实现 那么前缀表就不减一了,也不右移的,到底行不行呢? @@ -546,7 +546,7 @@ public: * 空间复杂度: O(m) -# 总结 +## 总结 我们介绍了什么是KMP,KMP可以解决什么问题,然后分析KMP算法里的next数组,知道了next数组就是前缀表,再分析为什么要是前缀表而不是什么其他表。 @@ -563,8 +563,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -691,8 +690,9 @@ class Solution { } ``` -Python3: +### Python3: (版本一)前缀表(减一) + ```python class Solution: def getNext(self, next, s): @@ -781,9 +781,9 @@ class Solution: def strStr(self, haystack: str, needle: str) -> int: return haystack.find(needle) -``` +``` -Go: +### Go: ```go // 方法一:前缀表使用减1实现 @@ -871,7 +871,7 @@ func strStr(haystack string, needle string) int { } ``` -JavaScript版本 +### JavaScript: > 前缀表统一减一 @@ -959,7 +959,7 @@ var strStr = function (haystack, needle) { }; ``` -TypeScript版本: +### TypeScript: > 前缀表统一减一 @@ -1036,7 +1036,7 @@ function strStr(haystack: string, needle: string): number { } ``` -Swift 版本 +### Swift: > 前缀表统一减一 @@ -1196,7 +1196,7 @@ func strStr(_ haystack: String, _ needle: String) -> Int { ``` -PHP: +### PHP: > 前缀表统一减一 ```php @@ -1272,7 +1272,7 @@ function getNext(&$next, $s){ } ``` -Rust: +### Rust: > 前缀表统一不减一 ```Rust @@ -1362,4 +1362,3 @@ impl Solution { - From 202dd382d29eb34f311d5f35f001255b2b770402 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 16:06:12 +0800 Subject: [PATCH 051/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200459.=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\345\255\227\347\254\246\344\270\262.md" | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" index e26d04ad87..f99102ab45 100644 --- "a/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0459.\351\207\215\345\244\215\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > KMP算法还能干这个 # 459.重复的子字符串 @@ -29,9 +27,11 @@ * 输出: True * 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -针对本题,我录制了视频讲解:[字符串这么玩,可有点难度! | LeetCode:459.重复的子字符串](https://www.bilibili.com/video/BV1cg41127fw),结合本题解一起看,事半功倍! +## 思路 暴力的解法, 就是一个for循环获取 子串的终止位置, 然后判断子串是否能重复构成字符串,又嵌套一个for循环,所以是O(n^2)的时间复杂度。 @@ -44,7 +44,7 @@ 主要讲一讲移动匹配 和 KMP两种方法。 -## 移动匹配 +### 移动匹配 当一个字符串s:abcabc,内部由重复的子串组成,那么这个字符串的结构一定是这样的: @@ -80,9 +80,9 @@ public: 如果我们做过 [28.实现strStr](https://programmercarl.com/0028.实现strStr.html) 题目的话,其实就知道,**实现一个 高效的算法来判断 一个字符串中是否出现另一个字符串是很复杂的**,这里就涉及到了KMP算法。 -## KMP +### KMP -### 为什么会使用KMP +#### 为什么会使用KMP 以下使用KMP方式讲解,强烈建议大家先把以下两个视频看了,理解KMP算法,再来看下面讲解,否则会很懵。 * [视频讲解版:帮你把KMP算法学个通透!(理论篇)](https://www.bilibili.com/video/BV1PD4y1o7nd/) @@ -105,7 +105,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 ![图三](https://code-thinking-1253855093.file.myqcloud.com/pics/20220728205249.png) -### 如何找到最小重复子串 +#### 如何找到最小重复子串 这里有同学就问了,为啥一定是开头的ab呢。 其实最关键还是要理解 最长相等前后缀,如图: @@ -123,7 +123,7 @@ KMP算法中next数组为什么遇到字符不匹配的时候可以找到上一 正是因为 最长相等前后缀的规则,当一个字符串由重复子串组成的,最长相等前后缀不包含的子串就是最小重复子串。 -### 简单推理 +#### 简单推理 这里再给出一个数学推导,就容易理解很多。 @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -261,8 +261,7 @@ class Solution { } ``` - -Python: +### Python: (版本一) 前缀表 减一 ```python @@ -346,8 +345,7 @@ class Solution: return False ``` - -Go: +### Go: 这里使用了前缀表统一减一的实现方式 @@ -405,7 +403,7 @@ func repeatedSubstringPattern(s string) bool { } ``` -JavaScript版本 +### JavaScript: > 前缀表统一减一 @@ -479,7 +477,7 @@ var repeatedSubstringPattern = function (s) { }; ``` -TypeScript: +### TypeScript: > 前缀表统一减一 @@ -539,8 +537,7 @@ function repeatedSubstringPattern(s: string): boolean { }; ``` - -Swift: +### Swift: > 前缀表统一减一 ```swift @@ -623,7 +620,7 @@ Swift: } ``` -Rust: +### Rust: >前缀表统一不减一 ```Rust From 4fe1f08acd1a7fe4a6af2ea4920ca97a284ce0c4 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 16:09:03 +0800 Subject: [PATCH 052/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\254\246\344\270\262\346\200\273\347\273\223.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" index c12d1764ad..5c2f016471 100644 --- "a/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" +++ "b/problems/\345\255\227\347\254\246\344\270\262\346\200\273\347\273\223.md" @@ -11,7 +11,7 @@ 那么这次我们来做一个总结。 -# 什么是字符串 +## 什么是字符串 字符串是若干字符组成的有限序列,也可以理解为是一个字符数组,但是很多语言对字符串做了特殊的规定,接下来我来说一说C/C++中的字符串。 @@ -42,7 +42,7 @@ for (int i = 0; i < a.size(); i++) { 所以想处理字符串,我们还是会定义一个string类型。 -# 要不要使用库函数 +## 要不要使用库函数 在文章[344.反转字符串](https://programmercarl.com/0344.反转字符串.html)中强调了**打基础的时候,不要太迷恋于库函数。** @@ -52,7 +52,7 @@ for (int i = 0; i < a.size(); i++) { **如果库函数仅仅是 解题过程中的一小部分,并且你已经很清楚这个库函数的内部实现原理的话,可以考虑使用库函数。** -# 双指针法 +## 双指针法 在[344.反转字符串](https://programmercarl.com/0344.反转字符串.html) ,我们使用双指针法实现了反转字符串的操作,**双指针法在数组,链表和字符串中很常用。** @@ -67,7 +67,7 @@ for (int i = 0; i < a.size(); i++) { 一些同学会使用for循环里调用库函数erase来移除元素,这其实是O(n^2)的操作,因为erase就是O(n)的操作,所以这也是典型的不知道库函数的时间复杂度,上来就用的案例了。 -# 反转系列 +## 反转系列 在反转上还可以在加一些玩法,其实考察的是对代码的掌控能力。 @@ -87,7 +87,7 @@ for (int i = 0; i < a.size(); i++) { 在[字符串:反转个字符串还有这个用处?](https://programmercarl.com/剑指Offer58-II.左旋转字符串.html)中,我们通过**先局部反转再整体反转**达到了左旋的效果。 -# KMP +## KMP KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配了。** @@ -110,7 +110,7 @@ KMP的精髓所在就是前缀表,在[KMP精讲](https://programmercarl.com/00 其中主要**理解j=next[x]这一步最为关键!** -# 总结 +## 总结 字符串类类型的题目,往往想法比较简单,但是实现起来并不容易,复杂的字符串题目非常考验对代码的掌控能力。 @@ -128,3 +128,4 @@ KMP算法是字符串查找最重要的算法,但彻底理解KMP并不容易 + From c3ebc91d4db0fc5b86945b910babd8c4ad2328b2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:27:16 +0800 Subject: [PATCH 053/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8F=8C=E6=8C=87?= =?UTF-8?q?=E9=92=88=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\214\207\351\222\210\346\200\273\347\273\223.md" | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" index 04a8cb9af8..6621e0396b 100644 --- "a/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" +++ "b/problems/\345\217\214\346\214\207\351\222\210\346\200\273\347\273\223.md" @@ -8,7 +8,8 @@ 相信大家已经对双指针法很熟悉了,但是双指针法并不隶属于某一种数据结构,我们在讲解数组,链表,字符串都用到了双指针法,所有有必要针对双指针法做一个总结。 -# 数组篇 +# 双指针总结篇 +## 数组篇 在[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)中,原地移除数组上的元素,我们说到了数组上的元素,不能真正的删除,只能覆盖。 @@ -26,7 +27,7 @@ for (int i = 0; i < array.size(); i++) { 所以此时使用双指针法才展现出效率的优势:**通过两个指针在一个for循环下完成两个for循环的工作。** -# 字符串篇 +## 字符串篇 在[字符串:这道题目,使用库函数一行代码搞定](https://programmercarl.com/0344.反转字符串.html)中讲解了反转字符串,注意这里强调要原地反转,要不然就失去了题目的意义。 @@ -48,7 +49,7 @@ for (int i = 0; i < array.size(); i++) { **主要还是大家用erase用的比较随意,一定要注意for循环下用erase的情况,一般可以用双指针写效率更高!** -# 链表篇 +## 链表篇 翻转链表是现场面试,白纸写代码的好题,考察了候选者对链表以及指针的熟悉程度,而且代码也不长,适合在白纸上写。 @@ -62,7 +63,7 @@ for (int i = 0; i < array.size(); i++) { 那么找到环的入口,其实需要点简单的数学推理,我在文章中把找环的入口清清楚楚的推理的一遍,如果对找环入口不够清楚的同学建议自己看一看[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)。 -# N数之和篇 +## N数之和篇 在[哈希表:解决了两数之和,那么能解决三数之和么?](https://programmercarl.com/0015.三数之和.html)中,讲到使用哈希法可以解决1.两数之和的问题 @@ -87,7 +88,7 @@ for (int i = 0; i < array.size(); i++) { 同样的道理,五数之和,n数之和都是在这个基础上累加。 -# 总结 +## 总结 本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将O(n^2)的时间复杂度,降为$O(n)$。 From de770c60e5ae781a9e65071e03ba4876237ed6fb Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:36:04 +0800 Subject: [PATCH 054/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=88=E4=B8=8E?= =?UTF-8?q?=E9=98=9F=E5=88=97=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\227\347\220\206\350\256\272\345\237\272\347\241\200.md" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" index 0075deb6c2..ad748e489e 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -4,8 +4,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ > 来看看栈和队列不为人知的一面 +# 栈与队列理论基础 + 我想栈和队列的原理大家应该很熟悉了,队列是先进先出,栈是先进后出。 如图所示: @@ -93,3 +96,4 @@ std::queue> third; // 定义以list为底层容器的队列 + From 4b5e198b170bade71d565d8ba3f7e29543f3bbef Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:40:33 +0800 Subject: [PATCH 055/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200232.=E7=94=A8?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\347\216\260\351\230\237\345\210\227.md" | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" index 4a57ee966b..c510fc12a6 100644 --- "a/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" +++ "b/problems/0232.\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227.md" @@ -36,10 +36,11 @@ queue.empty(); // 返回 false * 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 * 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。 -## 思路 +## 算法公开课 -《代码随想录》算法公开课:[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对栈和队列的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 思路 这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。 @@ -132,7 +133,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class MyQueue { @@ -179,8 +180,8 @@ class MyQueue { ``` +### Python: -Python: ```python class MyQueue: @@ -231,8 +232,8 @@ class MyQueue: ``` +### Go: -Go: ```Go type MyQueue struct { stackIn []int //输入栈 @@ -283,7 +284,7 @@ func (this *MyQueue) Empty() bool { } ``` - javaScript: +### JavaScript: ```js // 使用两个数组的栈方法(push, pop) 实现队列 @@ -338,7 +339,7 @@ MyQueue.prototype.empty = function() { }; ``` -TypeScript: +### TypeScript: ```typescript class MyQueue { @@ -374,7 +375,7 @@ class MyQueue { } ``` -Swift: +### Swift: ```swift class MyQueue { @@ -413,7 +414,8 @@ class MyQueue { } ``` -C: +### C: + ```C /* 1.两个type为int的数组(栈),大小为100 @@ -490,8 +492,8 @@ void myQueueFree(MyQueue* obj) { } ``` +### C#: -C#: ```csharp public class MyQueue { Stack inStack; @@ -534,7 +536,8 @@ public class MyQueue { -PHP: +### PHP: + ```php // SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8] // https://www.php.net/manual/zh/class.splstack.php @@ -579,7 +582,8 @@ class MyQueue { } ``` -Scala: +### Scala: + ```scala class MyQueue() { import scala.collection.mutable @@ -621,7 +625,7 @@ class MyQueue() { } ``` -rust: +### Rust: ```rust struct MyQueue { @@ -666,4 +670,3 @@ impl MyQueue { - From c18dbf059544b7bd100ff101cc92a2ee241434e5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:45:24 +0800 Subject: [PATCH 056/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\345\256\236\347\216\260\346\240\210.md" | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" index 94c7940486..13b742f839 100644 --- "a/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" +++ "b/problems/0225.\347\224\250\351\230\237\345\210\227\345\256\236\347\216\260\346\240\210.md" @@ -25,11 +25,11 @@ * 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。 * 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - -《代码随想录》算法公开课:[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对链表的理解。 +## 思路 (这里要强调是单向队列) @@ -114,7 +114,7 @@ public: * 时间复杂度: push为O(n),其他为O(1) * 空间复杂度: O(n) -# 优化 +## 优化 其实这道题目就是用一个队列就够了。 @@ -162,9 +162,9 @@ public: * 空间复杂度: O(n) -# 其他语言版本 +## 其他语言版本 -Java: +### Java: 使用两个 Queue 实现方法1 ```java @@ -404,7 +404,7 @@ class MyStack { } ``` -Python: +### Python: ```python from collections import deque @@ -496,8 +496,7 @@ class MyStack: return not self.que ``` - -Go: +### Go: 使用两个队列实现 ```go @@ -628,9 +627,7 @@ func (this *MyStack) Empty() bool { */ ``` - - -javaScript: +### JavaScript: 使用数组(push, shift)模拟队列 @@ -740,7 +737,7 @@ MyStack.prototype.empty = function() { ``` -TypeScript: +### TypeScript: 版本一:使用两个队列模拟栈 @@ -812,7 +809,7 @@ class MyStack { } ``` -Swift +### Swift: ```Swift // 定义一个队列数据结构 @@ -931,8 +928,9 @@ class MyStack { } } ``` -Scala: +### Scala: 使用两个队列模拟栈: + ```scala import scala.collection.mutable @@ -1015,8 +1013,8 @@ class MyStack() { } ``` +### C#: -C#: ```csharp public class MyStack { Queue queue1; @@ -1051,8 +1049,9 @@ public class MyStack { } ``` -PHP -> 双对列 +### PHP: + +> 双队列 ```php // SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5 >= 5.3.0, PHP 7, PHP 8) // https://www.php.net/manual/zh/class.splqueue.php @@ -1130,6 +1129,8 @@ class MyStack { } ``` +### Rust: + > rust:单队列 ```rust From a1ef2d03f64f6884fade8bfe5d2ed9dbceb129d8 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:49:03 +0800 Subject: [PATCH 057/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...10\347\232\204\346\213\254\345\217\267.md" | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index 213d61b795..045c79ee7f 100644 --- "a/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/problems/0020.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -39,12 +39,13 @@ * 输入: "{[]}" * 输出: true -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频在看本篇题解,更有助于大家对链表的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的拿手好戏!| LeetCode:20. 有效的括号](https://www.bilibili.com/video/BV1AF411w78g),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 思路 -## 题外话 +### 题外话 **括号匹配是使用栈解决的经典问题。** @@ -68,7 +69,7 @@ cd a/b/c/../../ 这里我就不过多展开了,先来看题。 -## 进入正题 +### 进入正题 由于栈结构的特殊性,非常适合做对称匹配类的题目。 @@ -143,8 +144,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public boolean isValid(String s) { @@ -171,7 +172,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法一,仅使用栈,更省空间 class Solution: @@ -213,7 +215,8 @@ class Solution: return True if not stack else False ``` -Go: +### Go: + ```Go func isValid(s string) bool { hash := map[byte]byte{')':'(', ']':'[', '}':'{'} @@ -235,7 +238,8 @@ func isValid(s string) bool { } ``` -Ruby: +### Ruby: + ```ruby def is_valid(strs) symbol_map = {')' => '(', '}' => '{', ']' => '['} @@ -253,7 +257,8 @@ def is_valid(strs) end ``` -Javascript: +### Javascript: + ```javascript var isValid = function (s) { const stack = []; @@ -296,7 +301,7 @@ var isValid = function(s) { }; ``` -TypeScript: +### TypeScript: 版本一:普通版 @@ -348,7 +353,7 @@ function isValid(s: string): boolean { }; ``` -Swift +### Swift: ```swift func isValid(_ s: String) -> Bool { @@ -373,7 +378,8 @@ func isValid(_ s: String) -> Bool { } ``` -C: +### C: + ```C //辅助函数:判断栈顶元素与输入的括号是否为一对。若不是,则返回False int notMatch(char par, char* stack, int stackTop) { @@ -414,8 +420,8 @@ bool isValid(char * s){ } ``` +### C#: -C#: ```csharp public class Solution { public bool IsValid(string s) { @@ -447,7 +453,8 @@ public class Solution { } ``` -PHP: +### PHP: + ```php // https://www.php.net/manual/zh/class.splstack.php class Solution @@ -475,8 +482,8 @@ class Solution } ``` +### Scala: -Scala: ```scala object Solution { import scala.collection.mutable @@ -499,7 +506,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution { From 70888c2b6074fa28f291108b4225735f6c24126f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:53:33 +0800 Subject: [PATCH 058/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\207\215\345\244\215\351\241\271.md" | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index 486d198b4d..ad54f0f88e 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > 匹配问题都是栈的强项 # 1047. 删除字符串中的所有相邻重复项 @@ -30,11 +28,13 @@ * 1 <= S.length <= 20000 * S 仅由小写英文字母组成。 -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项](https://www.bilibili.com/video/BV12a411P7mw),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -《代码随想录》算法视频公开课:[栈的好戏还要继续!| LeetCode:1047. 删除字符串中的所有相邻重复项](https://www.bilibili.com/video/BV12a411P7mw),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 思路 -## 正题 +### 正题 本题要删除相邻相同元素,相对于[20. 有效的括号](https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html)来说其实也是匹配问题,20. 有效的括号 是匹配左右括号,本题是匹配相邻元素,最后都是做消除的操作。 @@ -105,7 +105,7 @@ public: * 时间复杂度: O(n) * 空间复杂度: O(1),返回值不计空间复杂度 -## 题外话 +### 题外话 这道题目就像是我们玩过的游戏对对碰,如果相同的元素挨在一起就要消除。 @@ -125,8 +125,7 @@ public: ## 其他语言版本 - -Java: +### Java: 使用 Deque 作为堆栈 ```Java @@ -203,7 +202,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法一,使用栈 class Solution: @@ -239,7 +239,7 @@ class Solution: return ''.join(res[0: slow]) ``` -Go: +### Go: ```go func removeDuplicates(s string) string { @@ -258,7 +258,7 @@ func removeDuplicates(s string) string { } ``` -javaScript: +### JavaScript: 法一:使用栈 @@ -295,7 +295,7 @@ var removeDuplicates = function(s) { }; ``` -TypeScript: +### TypeScript: ```typescript function removeDuplicates(s: string): string { @@ -318,7 +318,7 @@ function removeDuplicates(s: string): string { }; ``` -C: +### C: 方法一:使用栈 ```c @@ -371,7 +371,8 @@ char * removeDuplicates(char * s){ } ``` -Swift: +### Swift: + ```swift func removeDuplicates(_ s: String) -> String { var stack = [Character]() @@ -386,8 +387,8 @@ func removeDuplicates(_ s: String) -> String { } ``` +### C#: -C#: ```csharp public string RemoveDuplicates(string s) { //拿字符串直接作为栈,省去了栈还要转为字符串的操作 @@ -405,8 +406,8 @@ public string RemoveDuplicates(string s) { } ``` +### PHP: -PHP: ```php class Solution { function removeDuplicates($s) { @@ -431,8 +432,8 @@ class Solution { } ``` +### Scala: -Scala: ```scala object Solution { import scala.collection.mutable @@ -455,7 +456,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution { From 7f4d74049ebc0d42930d5afe51b4c766295b0366 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 14:57:59 +0800 Subject: [PATCH 059/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\345\274\217\346\261\202\345\200\274.md" | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" index 09cc4f96e3..663a68ea5c 100644 --- "a/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" +++ "b/problems/0150.\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > 这不仅仅是一道好题,也展现出计算机的思考方式 # 150. 逆波兰表达式求值 @@ -63,9 +61,13 @@ * 适合用栈操作运算:遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。 -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解。 +## 思路 + +### 正题 在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。 @@ -117,7 +119,7 @@ public: * 空间复杂度: O(n) -## 题外话 +### 题外话 我们习惯看到的表达式都是中缀表达式,因为符合我们的习惯,但是中缀表达式对于计算机来说就不是很友好了。 @@ -134,11 +136,9 @@ public: > During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators, and continued to use it in some models into the 2020s. - - ## 其他语言版本 -java: +### Java: ```Java class Solution { @@ -164,7 +164,7 @@ class Solution { } ``` -python3 +### Python3: ```python from operator import add, sub, mul @@ -201,7 +201,8 @@ class Solution: ``` -Go: +### Go: + ```Go func evalRPN(tokens []string) int { stack := []int{} @@ -228,7 +229,7 @@ func evalRPN(tokens []string) int { } ``` -javaScript: +### JavaScript: ```js var evalRPN = function (tokens) { @@ -259,7 +260,7 @@ var evalRPN = function (tokens) { }; ``` -TypeScript: +### TypeScript: 普通版: @@ -324,7 +325,8 @@ function evalRPN(tokens: string[]): number { }; ``` -Swift: +### Swift: + ```Swift func evalRPN(_ tokens: [String]) -> Int { var stack = [Int]() @@ -357,7 +359,8 @@ func evalRPN(_ tokens: [String]) -> Int { } ``` -C#: +### C#: + ```csharp public int EvalRPN(string[] tokens) { int num; @@ -391,8 +394,8 @@ public int EvalRPN(string[] tokens) { } ``` +### PHP: -PHP: ```php class Solution { function evalRPN($tokens) { @@ -417,7 +420,8 @@ class Solution { } ``` -Scala: +### Scala: + ```scala object Solution { import scala.collection.mutable @@ -447,7 +451,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution { From 5a6bf6d85534e304e73460d67db006f5c1475657 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:01:53 +0800 Subject: [PATCH 060/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...43\346\234\200\345\244\247\345\200\274.md" | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" index f1c4b76ccf..6f420479eb 100644 --- "a/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" +++ "b/problems/0239.\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -28,11 +28,11 @@ * -10^4 <= nums[i] <= 10^4 * 1 <= k <= nums.length +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 - -《代码随想录》算法视频公开课:[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 思路 这是使用单调队列的经典题目。 @@ -196,7 +196,7 @@ public: 空间复杂度因为我们定义一个辅助队列,所以是O(k)。 -# 扩展 +## 扩展 大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本题中的单调队列实现就是固定的写法哈。 @@ -204,10 +204,10 @@ public: -# 其他语言版本 +## 其他语言版本 +### Java: -Java: ```Java //解法一 //自定义数组 @@ -298,7 +298,8 @@ class Solution { } ``` -Python: +### Python: + ```python from collections import deque @@ -338,8 +339,7 @@ class Solution: return result ``` - -Go: +### Go: ```go // 封装单调队列的方式解题 @@ -401,7 +401,8 @@ func maxSlidingWindow(nums []int, k int) []int { } ``` -Javascript: +### Javascript: + ```javascript /** * @param {number[]} nums @@ -449,7 +450,7 @@ var maxSlidingWindow = function (nums, k) { }; ``` -TypeScript: +### TypeScript: ```typescript function maxSlidingWindow(nums: number[], k: number): number[] { @@ -497,7 +498,9 @@ function maxSlidingWindow(nums: number[], k: number): number[] { }; ``` -Swift: +### Swift: + +解法一: ```Swift /// 双向链表 @@ -638,7 +641,8 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { return result } ``` -Scala: +### Scala: + ```scala import scala.collection.mutable.ArrayBuffer object Solution { @@ -686,8 +690,8 @@ class MyQueue { } ``` +### PHP: -PHP: ```php class Solution { /** @@ -764,7 +768,8 @@ class MyQueue{ } ``` -C#: +### C#: + ```csharp class myDequeue{ private LinkedList linkedList = new LinkedList(); @@ -805,7 +810,7 @@ class myDequeue{ } ``` -rust: +### Rust: ```rust impl Solution { From 0e4f91d81d68b53269f33aa994ecab626202243b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:13:29 +0800 Subject: [PATCH 061/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200347.=E5=89=8Dk?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\351\242\221\345\205\203\347\264\240.md" | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" index 6c8b51b1ad..b3063111b4 100644 --- "a/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" +++ "b/problems/0347.\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.md" @@ -5,8 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - > 前K个大数问题,老生常谈,不得不谈 # 347.前 K 个高频元素 @@ -29,9 +27,11 @@ * 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。 * 你可以按任意顺序返回答案。 -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[优先级队列正式登场!大顶堆、小顶堆该怎么用?| LeetCode:347.前 K 个高频元素](https://www.bilibili.com/video/BV1Xg41167Lz),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目主要涉及到如下三块内容: 1. 要统计元素出现频率 @@ -122,7 +122,7 @@ public: * 时间复杂度: O(nlogk) * 空间复杂度: O(n) -# 拓展 +## 拓展 大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。 确实 例如我们在写快排的cmp函数的时候,`return left>right` 就是从大到小,`return left + From f7b87e625a9fab65b30d19b5252aef26c869fbdb Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:15:50 +0800 Subject: [PATCH 062/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=88=E4=B8=8E?= =?UTF-8?q?=E9=98=9F=E5=88=97=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...37\345\210\227\346\200\273\347\273\223.md" | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index c313220165..31ce955489 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -3,9 +3,9 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+# 栈与队列总结篇 - -# 栈与队列的理论基础 +## 栈与队列的理论基础 首先我们在[栈与队列:来看看栈和队列不为人知的一面](https://programmercarl.com/栈与队列理论基础.html)中讲解了栈和队列的理论基础。 @@ -37,9 +37,9 @@ **一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时在去弹出元素就是栈的顺序了。** -# 栈经典题目 +## 栈经典题目 -## 栈在系统中的应用 +### 栈在系统中的应用 如果还记得编译原理的话,编译器在 词法分析的过程中处理括号、花括号等这个符号的逻辑,就是使用了栈这种数据结构。 @@ -59,7 +59,7 @@ cd a/b/c/../../ **所以数据结构与算法的应用往往隐藏在我们看不到的地方!** -## 括号匹配问题 +### 括号匹配问题 在[栈与队列:系统中处处都是栈的应用](https://programmercarl.com/0020.有效的括号.html)中我们讲解了括号匹配问题。 @@ -75,23 +75,23 @@ cd a/b/c/../../ 这里还有一些技巧,在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多了! -## 字符串去重问题 +### 字符串去重问题 在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中讲解了字符串去重问题。 1047. 删除字符串中的所有相邻重复项 思路就是可以把字符串顺序放到一个栈中,然后如果相同的话 栈就弹出,这样最后栈里剩下的元素都是相邻不相同的元素了。 -## 逆波兰表达式问题 +### 逆波兰表达式问题 在[栈与队列:有没有想过计算机是如何处理表达式的?](https://programmercarl.com/0150.逆波兰表达式求值.html)中讲解了求逆波兰表达式。 本题中每一个子表达式要得出一个结果,然后拿这个结果再进行运算,那么**这岂不就是一个相邻字符串消除的过程,和[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中的对对碰游戏是不是就非常像了。** -# 队列的经典题目 +## 队列的经典题目 -## 滑动窗口最大值问题 +### 滑动窗口最大值问题 在[栈与队列:滑动窗口里求最大值引出一个重要数据结构](https://programmercarl.com/0239.滑动窗口最大值.html)中讲解了一种数据结构:单调队列。 @@ -119,7 +119,7 @@ cd a/b/c/../../ 我们用deque作为单调队列的底层数据结构,C++中deque是stack和queue默认的底层实现容器(这个我们之前已经讲过),deque是可以两边扩展的,而且deque里元素并不是严格的连续分布的。 -## 求前 K 个高频元素 +### 求前 K 个高频元素 在[栈与队列:求前 K 个高频元素和队列有啥关系?](https://programmercarl.com/0347.前K个高频元素.html)中讲解了求前 K 个高频元素。 @@ -143,7 +143,7 @@ cd a/b/c/../../ 所以排序的过程的时间复杂度是$O(\log k)$,整个算法的时间复杂度是$O(n\log k)$。 -# 总结 +## 总结 在栈与队列系列中,我们强调栈与队列的基础,也是很多同学容易忽视的点。 @@ -162,3 +162,4 @@ cd a/b/c/../../ + From c6bcd423d6bdab6375e3af8bc7c3c01bfc0518a1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:48:05 +0800 Subject: [PATCH 063/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" index 4098cf1f74..184dba604c 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -8,8 +8,11 @@ # 二叉树理论基础篇 +## 算法公开课 -《代码随想录》算法视频公开课:[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 题目分类 题目分类大纲如下: @@ -189,8 +192,7 @@ struct TreeNode { ## 其他语言版本 - -Java: +### Java: ```java public class TreeNode { @@ -208,8 +210,7 @@ public class TreeNode { } ``` - -Python: +### Python: ```python class TreeNode: @@ -219,7 +220,7 @@ class TreeNode: self.right = right ``` -Go: +### Go: ```go type TreeNode struct { @@ -229,7 +230,7 @@ type TreeNode struct { } ``` -JavaScript: +### JavaScript: ```javascript function TreeNode(val, left, right) { @@ -239,7 +240,7 @@ function TreeNode(val, left, right) { } ``` -TypeScript: +### TypeScript: ```typescript class TreeNode { @@ -254,7 +255,7 @@ class TreeNode { } ``` -Swift: +### Swift: ```Swift class TreeNode { @@ -271,7 +272,7 @@ class TreeNode { } ``` -Scala: +### Scala: ```scala class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) { @@ -281,7 +282,7 @@ class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) } ``` -rust: +### Rust: ```rust #[derive(Debug, PartialEq, Eq)] From 7807b5e7989751cd9e5d2df8da6821438e01c378 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 15:53:16 +0800 Subject: [PATCH 064/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\275\222\351\201\215\345\216\206.md" | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" index 92f342f0db..730b18ac6d 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\351\200\222\345\275\222\351\201\215\345\216\206.md" @@ -4,15 +4,15 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- +> 一看就会,一写就废! # 二叉树的递归遍历 +## 算法公开课 -《代码随想录》算法视频公开课:[每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!](https://www.bilibili.com/video/BV1Wh411S7xt),相信结合视频在看本篇题解,更有助于大家对本题的理解。 - +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!](https://www.bilibili.com/video/BV1Wh411S7xt),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -> 一看就会,一写就废! +## 思路 这次我们要好好谈一谈递归,为什么很多同学看递归算法都是“一看就会,一写就废”。 @@ -109,14 +109,10 @@ void traversal(TreeNode* cur, vector& vec) { 可能有同学感觉前后中序遍历的递归太简单了,要打迭代法(非递归),别急,我们明天打迭代法,打个通透! +## 其他语言版本 +### Java: - - -# 其他语言版本 - - -Java: ```Java // 前序遍历·递归·LC144_二叉树的前序遍历 class Solution { @@ -171,7 +167,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 前序遍历-递归-LC144_二叉树的前序遍历 # Definition for a binary tree node. @@ -219,7 +216,7 @@ class Solution: return left + right + [root.val] ``` -Go: +### Go: 前序遍历: ```go @@ -273,7 +270,7 @@ func postorderTraversal(root *TreeNode) (res []int) { } ``` -Javascript版本: +### Javascript: 前序遍历: ```Javascript @@ -327,7 +324,7 @@ var postorderTraversal = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历 @@ -370,7 +367,7 @@ function postorderTraversal(node: TreeNode | null): number[] { } ``` -C: +### C: ```c //前序遍历: @@ -422,8 +419,9 @@ int* postorderTraversal(struct TreeNode* root, int* returnSize){ } ``` -Swift: +### Swift: 前序遍历:(144.二叉树的前序遍历) + ```Swift func preorderTraversal(_ root: TreeNode?) -> [Int] { var res = [Int]() @@ -473,7 +471,10 @@ func postorder(_ root: TreeNode?, res: inout [Int]) { res.append(root!.val) } ``` -Scala: 前序遍历:(144.二叉树的前序遍历) +### Scala: + + 前序遍历:(144.二叉树的前序遍历) + ```scala object Solution { import scala.collection.mutable.ListBuffer @@ -525,7 +526,7 @@ object Solution { } ``` -rust: +### Rust: ```rust use std::cell::RefCell; From a13bb8292123ceded30c12bf7e713468fc29e50f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 16:07:52 +0800 Subject: [PATCH 065/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\344\273\243\351\201\215\345\216\206.md" | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" index 8b2414652c..69007995f0 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\350\277\255\344\273\243\351\201\215\345\216\206.md" @@ -4,16 +4,17 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+> 听说还可以用非递归的方式 # 二叉树的迭代遍历 -《代码随想录》算法视频公开课: -* [写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2) -* [写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g) -相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 算法公开课 +[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html): -> 听说还可以用非递归的方式 +* **[写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2)** +* **[写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g)** +**相信结合视频在看本篇题解,更有助于大家对本题的理解。** 看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目: @@ -21,13 +22,15 @@ * [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/) * [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) +## 思路 + 为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢? 我们在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中提到了,**递归的实现就是:每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中**,然后递归返回的时候,从栈顶弹出上一次递归的各项参数,所以这就是递归为什么可以返回上一层位置的原因。 此时大家应该知道我们用栈也可以是实现二叉树的前后中序遍历了。 -## 前序遍历(迭代法) +### 前序遍历(迭代法) 我们先看一下前序遍历。 @@ -69,7 +72,7 @@ public: 但接下来,**再用迭代法写中序遍历的时候,会发现套路又不一样了,目前的前序遍历的逻辑无法直接应用到中序遍历上。** -## 中序遍历(迭代法) +### 中序遍历(迭代法) 为了解释清楚,我说明一下 刚刚在迭代的过程中,其实我们有两个操作: @@ -112,7 +115,7 @@ public: ``` -## 后序遍历(迭代法) +### 后序遍历(迭代法) 再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: @@ -142,7 +145,7 @@ public: ``` -# 总结 +## 总结 此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不像递归写法那样代码稍做调整,就可以实现前后中序。 @@ -155,11 +158,9 @@ public: 当然可以,这种写法,还不是很好理解,我们将在下一篇文章里重点讲解,敬请期待! +## 其他语言版本 - -# 其他语言版本 - -Java: +### Java: ```java // 前序遍历顺序:中-左-右,入栈顺序:中-右-左 @@ -233,10 +234,7 @@ class Solution { } ``` - - - -Python: +### Python: ```python # 前序遍历-迭代-LC144_二叉树的前序遍历 @@ -281,7 +279,7 @@ class Solution: # 取栈顶元素右结点 cur = cur.right return result - ``` +``` ```python # 后序遍历-迭代-LC145_二叉树的后序遍历 @@ -303,10 +301,9 @@ class Solution: stack.append(node.right) # 将最终的数组翻转 return result[::-1] -``` - + ``` -Go: +### Go: > 迭代法前序遍历 @@ -400,7 +397,7 @@ func inorderTraversal(root *TreeNode) []int { } ``` -javaScript: +### JavaScript: ```js @@ -464,7 +461,7 @@ var postorderTraversal = function(root, res = []) { }; ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历(迭代法) @@ -519,7 +516,7 @@ function postorderTraversal(root: TreeNode | null): number[] { }; ``` -Swift: +### Swift: ```swift // 前序遍历迭代法 @@ -578,7 +575,8 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] { return result } ``` -Scala: +### Scala: + ```scala // 前序遍历(迭代法) object Solution { @@ -645,7 +643,7 @@ object Solution { } ``` -rust: +### Rust: ```rust use std::cell::RefCell; From cc510357c1fe6f0cf25fd4e800c2a89b03b4659f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 16:22:00 +0800 Subject: [PATCH 066/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\350\277\255\344\273\243\346\263\225.md" | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" index 274642696c..8089af64ec 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\347\232\204\347\273\237\344\270\200\350\277\255\344\273\243\346\263\225.md" @@ -5,10 +5,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+> 统一写法是一种什么感觉 # 二叉树的统一迭代法 -> 统一写法是一种什么感觉 +## 思路 此时我们在[二叉树:一入递归深似海,从此offer是路人](https://programmercarl.com/二叉树的递归遍历.html)中用递归的方式,实现了二叉树前中后序的遍历。 @@ -28,7 +29,7 @@ 如何标记呢,**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法也可以叫做标记法。 -## 迭代法中序遍历 +### 迭代法中序遍历 中序遍历代码如下:(详细注释) @@ -71,7 +72,7 @@ public: 此时我们再来看前序遍历代码。 -## 迭代法前序遍历 +### 迭代法前序遍历 迭代法前序遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -102,7 +103,7 @@ public: }; ``` -## 迭代法后序遍历 +### 迭代法后序遍历 后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -143,15 +144,11 @@ public: 所以大家根据自己的个人喜好,对于二叉树的前中后序遍历,选择一种自己容易理解的递归和迭代法。 +## 其他语言版本 - - - -# 其他语言版本 - - -Java: +### Java: 迭代法前序遍历代码如下: + ```java class Solution { public List preorderTraversal(TreeNode root) { @@ -235,7 +232,7 @@ class Solution { } ``` -Python: +### Python: 迭代法前序遍历: ```python @@ -309,7 +306,8 @@ class Solution: return result ``` -Go: +### Go: + > 前序遍历统一迭代法 ```GO @@ -442,7 +440,7 @@ func postorderTraversal(root *TreeNode) []int { } ``` -javaScript: +### JavaScript: > 前序遍历统一迭代法 @@ -522,7 +520,7 @@ var postorderTraversal = function(root, res = []) { ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历(迭代法) @@ -591,7 +589,8 @@ function postorderTraversal(root: TreeNode | null): number[] { return res; }; ``` -Scala: +### Scala: + ```scala // 前序遍历 object Solution { @@ -667,7 +666,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution{ @@ -747,3 +746,4 @@ impl Solution{ + From 8e34d5ff5749229eeaa7dce33e1ed74c0dd992a2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 19:17:38 +0800 Subject: [PATCH 067/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 284 +++++++++--------- 1 file changed, 149 insertions(+), 135 deletions(-) diff --git "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" index c2ad950814..ce9a247c1f 100644 --- "a/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" +++ "b/problems/0102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206.md" @@ -8,21 +8,23 @@ # 二叉树层序遍历登场! -《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 学会二叉树的层序遍历,可以一口气打完以下十题: -* 102.二叉树的层序遍历 -* 107.二叉树的层次遍历II -* 199.二叉树的右视图 -* 637.二叉树的层平均值 -* 429.N叉树的层序遍历 -* 515.在每个树行中找最大值 -* 116.填充每个节点的下一个右侧节点指针 -* 117.填充每个节点的下一个右侧节点指针II -* 104.二叉树的最大深度 -* 111.二叉树的最小深度 +* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) +* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) +* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) +* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) +* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) +* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) @@ -31,7 +33,7 @@ -# 102.二叉树的层序遍历 +## 102.二叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/) @@ -39,7 +41,7 @@ ![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png) -思路: +### 思路 我们之前讲过了三篇关于二叉树的深度优先遍历的文章: @@ -63,7 +65,7 @@ 代码如下:**这份代码也可以作为二叉树层序遍历的模板,打十个就靠它了**。 -C++代码: +c++代码如下: ```CPP class Solution { @@ -111,7 +113,9 @@ public: }; ``` -java: +### 其他语言版本 + +#### Java: ```Java // 102.二叉树的层序遍历 @@ -167,7 +171,7 @@ class Solution { } ``` -python3代码: +#### Python: ```python @@ -219,12 +223,9 @@ class Solution: self.helper(node.left, level + 1, levels) self.helper(node.right, level + 1, levels) - ``` - - -go: +#### Go: ```go /** @@ -320,7 +321,7 @@ func levelOrder(root *TreeNode) (res [][]int) { } ``` -javascript代码: +#### Javascript: ```javascript var levelOrder = function(root) { @@ -350,7 +351,7 @@ var levelOrder = function(root) { ``` -TypeScript: +#### TypeScript: ```typescript function levelOrder(root: TreeNode | null): number[][] { @@ -377,7 +378,7 @@ function levelOrder(root: TreeNode | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrder(_ root: TreeNode?) -> [[Int]] { @@ -403,7 +404,7 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] { } ``` -Scala: +#### Scala: ```scala // 102.二叉树的层序遍历 @@ -430,7 +431,7 @@ object Solution { } ``` -Rust: +#### Rust: ```rust use std::cell::RefCell; @@ -466,7 +467,7 @@ impl Solution { **此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!** -# 107.二叉树的层次遍历 II +## 107.二叉树的层次遍历 II [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) @@ -474,7 +475,7 @@ impl Solution { ![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png) -思路: +### 思路 相对于102.二叉树的层序遍历,就是最后把result数组反转一下就可以了。 @@ -506,7 +507,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python class Solution: @@ -537,7 +540,7 @@ class Solution: return result[::-1] ``` -Java: +#### Java: ```java // 107. 二叉树的层序遍历 II @@ -618,12 +621,10 @@ class Solution { return ans; } -} -``` - +``` -go: +#### Go: ```GO /** @@ -662,7 +663,7 @@ func levelOrderBottom(root *TreeNode) [][]int { } ``` -javascript代码 +#### Javascript: ```javascript var levelOrderBottom = function(root) { @@ -688,7 +689,7 @@ var levelOrderBottom = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function levelOrderBottom(root: TreeNode | null): number[][] { @@ -711,7 +712,7 @@ function levelOrderBottom(root: TreeNode | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { @@ -737,8 +738,7 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { } ``` - -Scala: +#### Scala: ```scala // 107.二叉树的层次遍历II @@ -764,7 +764,10 @@ object Solution { res.reverse.toList } -Rust: + +``` + +#### Rust: ```rust use std::cell::RefCell; @@ -796,7 +799,7 @@ impl Solution { } ``` -# 199.二叉树的右视图 +## 199.二叉树的右视图 [力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/) @@ -804,7 +807,7 @@ impl Solution { ![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png) -思路: +### 思路 层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。 @@ -832,7 +835,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python # Definition for a binary tree node. @@ -866,8 +871,7 @@ class Solution: return right_view ``` - -Java: +#### Java: ```java // 199.二叉树的右视图 @@ -911,7 +915,7 @@ public class N0199 { } ``` -go: +#### Go: ```GO /** @@ -945,8 +949,7 @@ func rightSideView(root *TreeNode) []int { } ``` - -javascript代码: +#### Javascript: ```javascript var rightSideView = function(root) { @@ -972,7 +975,7 @@ var rightSideView = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function rightSideView(root: TreeNode | null): number[] { @@ -992,7 +995,7 @@ function rightSideView(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func rightSideView(_ root: TreeNode?) -> [Int] { @@ -1017,7 +1020,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] { } ``` -Scala: +#### Scala: ```scala // 199.二叉树的右视图 @@ -1043,7 +1046,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -1076,7 +1079,7 @@ impl Solution { } ``` -# 637.二叉树的层平均值 +## 637.二叉树的层平均值 [力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/) @@ -1084,7 +1087,7 @@ impl Solution { ![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png) -思路: +### 思路 本题就是层序遍历的时候把一层求个总和在取一个均值。 @@ -1115,7 +1118,9 @@ public: ``` -python代码: +### 其他语言版本 + +#### Python: ```python class Solution: @@ -1155,7 +1160,7 @@ class Solution: return averages ``` -java: +#### Java: ```java // 637. 二叉树的层平均值 @@ -1197,7 +1202,7 @@ public class N0637 { } ``` -go: +#### Go: ```GO /** @@ -1235,7 +1240,7 @@ func averageOfLevels(root *TreeNode) []float64 { } ``` -javascript代码: +#### Javascript: ```javascript var averageOfLevels = function(root) { @@ -1262,7 +1267,7 @@ var averageOfLevels = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function averageOfLevels(root: TreeNode | null): number[] { @@ -1286,7 +1291,7 @@ function averageOfLevels(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func averageOfLevels(_ root: TreeNode?) -> [Double] { @@ -1313,7 +1318,7 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] { } ``` -Scala: +#### Scala: ```scala // 637.二叉树的层平均值 @@ -1339,7 +1344,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -1372,7 +1377,7 @@ impl Solution { } ``` -# 429.N叉树的层序遍历 +## 429.N叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) @@ -1390,8 +1395,7 @@ impl Solution { [5,6] ] - -思路: +### 思路 这道题依旧是模板题,只不过一个节点有多个孩子了 @@ -1423,7 +1427,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python """ @@ -1475,7 +1481,7 @@ class Solution: return result ``` -java: +#### Java: ```java // 429. N 叉树的层序遍历 @@ -1535,8 +1541,7 @@ public class N0429 { } ``` - -go: +#### Go: ```GO /** @@ -1567,7 +1572,7 @@ func levelOrder(root *Node) [][]int { } ``` -JavaScript代码: +#### JavaScript: ```JavaScript var levelOrder = function(root) { @@ -1596,7 +1601,7 @@ var levelOrder = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function levelOrder(root: Node | null): number[][] { @@ -1618,7 +1623,7 @@ function levelOrder(root: Node | null): number[][] { }; ``` -Swift: +#### Swift: ```swift func levelOrder(_ root: Node?) -> [[Int]] { @@ -1643,7 +1648,7 @@ func levelOrder(_ root: Node?) -> [[Int]] { } ``` -Scala: +#### Scala: ```scala // 429.N叉树的层序遍历 @@ -1672,7 +1677,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust pub struct Solution; @@ -1720,7 +1725,7 @@ impl Solution { } ``` -# 515.在每个树行中找最大值 +## 515.在每个树行中找最大值 [力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) @@ -1728,7 +1733,7 @@ impl Solution { ![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png) -思路: +### 思路 层序遍历,取每一层的最大值 @@ -1758,7 +1763,9 @@ public: }; ``` -python代码: +### 其他语言版本 + +#### Python: ```python # Definition for a binary tree node. @@ -1794,7 +1801,7 @@ class Solution: return result ``` -java代码: +#### Java: ```java class Solution { @@ -1820,7 +1827,7 @@ class Solution { } ``` -go: +#### Go: ```GO /** @@ -1864,7 +1871,7 @@ func max(x, y int) int { } ``` -javascript代码: +#### Javascript: ```javascript var largestValues = function(root) { @@ -1890,7 +1897,7 @@ var largestValues = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function largestValues(root: TreeNode | null): number[] { @@ -1916,7 +1923,7 @@ function largestValues(root: TreeNode | null): number[] { }; ``` -Swift: +#### Swift: ```swift func largestValues(_ root: TreeNode?) -> [Int] { @@ -1943,7 +1950,7 @@ func largestValues(_ root: TreeNode?) -> [Int] { } ``` -Scala: +#### Scala: ```scala // 515.在每个树行中找最大值 @@ -1970,7 +1977,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -2002,7 +2009,7 @@ impl Solution { } ``` -# 116.填充每个节点的下一个右侧节点指针 +## 116.填充每个节点的下一个右侧节点指针 [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) @@ -2024,7 +2031,7 @@ struct Node { ![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg) -思路: +### 思路 本题依然是层序遍历,只不过在单层遍历的时候记录一下本层的头部节点,然后在遍历的时候让前一个节点指向本节点就可以了 @@ -2063,7 +2070,9 @@ public: }; ``` -java代码: +### 其他语言版本 + +#### Java: ```java class Solution { @@ -2093,7 +2102,7 @@ class Solution { } ``` -python代码: +#### Python: ```python """ @@ -2133,7 +2142,7 @@ class Solution: return root ``` -go: +#### Go: ```GO /** @@ -2173,7 +2182,7 @@ func connect(root *Node) *Node { ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2209,7 +2218,7 @@ var connect = function(root) { ``` -TypeScript: +#### TypeScript: ```typescript function connect(root: Node | null): Node | null { @@ -2234,7 +2243,7 @@ function connect(root: Node | null): Node | null { }; ``` -Swift: +#### Swift: ```swift func connect(_ root: Node?) -> Node? { @@ -2266,7 +2275,7 @@ func connect(_ root: Node?) -> Node? { } ``` -Scala: +#### Scala: ```scala // 116.填充每个节点的下一个右侧节点指针 @@ -2297,11 +2306,11 @@ object Solution { } ``` -# 117.填充每个节点的下一个右侧节点指针II +## 117.填充每个节点的下一个右侧节点指针II [力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) -思路: +### 思路 这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道 @@ -2339,7 +2348,9 @@ public: }; ``` -Java 代码: +### 其他语言版本 + +#### Java: ```java // 二叉树之层次遍历 @@ -2377,7 +2388,7 @@ class Solution { } ``` -python代码: +#### Python: ```python # 层序遍历解法 @@ -2420,7 +2431,7 @@ class Solution: ``` -go: +#### Go: ```GO /** @@ -2459,7 +2470,7 @@ func connect(root *Node) *Node { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2494,7 +2505,7 @@ var connect = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function connect(root: Node | null): Node | null { @@ -2519,7 +2530,7 @@ function connect(root: Node | null): Node | null { }; ``` -Swift: +#### Swift: ```swift func connect(_ root: Node?) -> Node? { @@ -2551,7 +2562,7 @@ func connect(_ root: Node?) -> Node? { } ``` -Scala: +#### Scala: ```scala // 117.填充每个节点的下一个右侧节点指针II @@ -2582,7 +2593,7 @@ object Solution { } ``` -# 104.二叉树的最大深度 +## 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -2600,7 +2611,7 @@ object Solution { 返回它的最大深度 3 。 -思路: +### 思路 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。 @@ -2635,7 +2646,9 @@ public: }; ``` -Java: +### 其他语言版本 + +#### Java: ```Java class Solution { @@ -2661,7 +2674,7 @@ class Solution { } ``` -Python: +#### Python: ```python 3 # Definition for a binary tree node. @@ -2691,7 +2704,7 @@ class Solution: ``` -Go: +#### Go: ```go /** @@ -2726,7 +2739,7 @@ func maxDepth(root *TreeNode) int { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -2759,7 +2772,7 @@ var maxDepth = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function maxDepth(root: TreeNode | null): number { @@ -2779,7 +2792,7 @@ function maxDepth(root: TreeNode | null): number { }; ``` -Swift: +#### Swift: ```swift func maxDepth(_ root: TreeNode?) -> Int { @@ -2804,7 +2817,7 @@ func maxDepth(_ root: TreeNode?) -> Int { } ``` -Scala: +#### Scala: ```scala // 104.二叉树的最大深度 @@ -2829,7 +2842,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -2859,10 +2872,12 @@ impl Solution { } ``` -# 111.二叉树的最小深度 +## 111.二叉树的最小深度 [力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) +### 思路 + 相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。 **需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点** @@ -2895,7 +2910,9 @@ public: }; ``` -Java: +### 其他语言版本 + +#### Java: ```java class Solution { @@ -2925,9 +2942,7 @@ class Solution { } ``` - - -Python 3: +#### Python: ```python 3 # Definition for a binary tree node. @@ -2960,7 +2975,7 @@ class Solution: return depth ``` -Go: +#### Go: ```go /** @@ -2999,7 +3014,7 @@ func minDepth(root *TreeNode) int { } ``` -JavaScript: +#### JavaScript: ```javascript /** @@ -3035,7 +3050,7 @@ var minDepth = function(root) { }; ``` -TypeScript: +#### TypeScript: ```typescript function minDepth(root: TreeNode | null): number { @@ -3056,7 +3071,7 @@ function minDepth(root: TreeNode | null): number { }; ``` -Swift: +#### Swift: ```swift func minDepth(_ root: TreeNode?) -> Int { @@ -3082,7 +3097,7 @@ func minDepth(_ root: TreeNode?) -> Int { } ``` -Scala: +#### Scala: ```scala // 111.二叉树的最小深度 @@ -3108,7 +3123,7 @@ object Solution { } ``` -rust: +#### Rust: ```rust use std::cell::RefCell; @@ -3141,28 +3156,27 @@ impl Solution { } ``` -# 总结 +## 总结 二叉树的层序遍历,**就是图论中的广度优先搜索在二叉树中的应用**,需要借助队列来实现(此时又发现队列的一个应用了)。 来吧,一口气打十个: -* 102.二叉树的层序遍历 -* 107.二叉树的层次遍历II -* 199.二叉树的右视图 -* 637.二叉树的层平均值 -* 429.N叉树的层序遍历 -* 515.在每个树行中找最大值 -* 116.填充每个节点的下一个右侧节点指针 -* 117.填充每个节点的下一个右侧节点指针II -* 104.二叉树的最大深度 -* 111.二叉树的最小深度 +* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) +* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) +* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/) +* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/) +* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) +* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) +* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) **致敬叶师傅!** - -

+ From 1abe3506ea6108a14d32b3176bb44674212cd9b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 14:19:58 +0800 Subject: [PATCH 068/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\344\272\214\345\217\211\346\240\221.md" | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" index b3aea9ed29..1151778364 100644 --- "a/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0226.\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" @@ -16,7 +16,11 @@ 这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈) -# 题外话 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 题外话 这道题目是非常经典的题目,也是比较简单的题目(至少一看就会)。 @@ -24,9 +28,7 @@ 如果做过这道题的同学也建议认真看完,相信一定有所收获! -# 思路 - -《代码随想录》算法视频公开课:[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 思路 我们之前介绍的都是各种方式遍历二叉树,这次要翻转了,感觉还是有点懵逼。 @@ -49,7 +51,7 @@ 那么层序遍历可以不可以呢?**依然可以的!只要把每一个节点的左右孩子翻转一下的遍历方式都是可以的!** -## 递归法 +### 递归法 对于二叉树的递归法的前中后序遍历,已经在[二叉树:前中后序递归遍历](https://programmercarl.com/二叉树的递归遍历.html)详细讲解了。 @@ -102,9 +104,9 @@ public: }; ``` -## 迭代法 +### 迭代法 -### 深度优先遍历 +#### 深度优先遍历 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)中给出了前中后序迭代方式的写法,所以本题可以很轻松的写出如下迭代法的代码: @@ -163,7 +165,7 @@ public: 如果上面这个代码看不懂,回顾一下文章[二叉树:前中后序迭代方式的统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。 -### 广度优先遍历 +#### 广度优先遍历 也就是层序遍历,层数遍历也是可以翻转这棵树的,因为层序遍历也可以把每个节点的左右孩子都翻转一遍,代码如下: @@ -259,7 +261,7 @@ public: ## 其他语言版本 -### Java +### Java: ```Java //DFS递归 class Solution { @@ -310,7 +312,7 @@ class Solution { } ``` -### Python +### Python: 递归法:前序遍历: ```python @@ -466,7 +468,7 @@ class Solution: ``` -### Go +### Go: 递归版本的前序遍历 ```Go @@ -575,7 +577,7 @@ func invertTree(root *TreeNode) *TreeNode { } ``` -### JavaScript +### JavaScript: 使用递归版本的前序遍历 ```javascript @@ -783,7 +785,7 @@ function invertTree(root: TreeNode | null): TreeNode | null { }; ``` -### C +### C: 递归法 ```c @@ -961,7 +963,7 @@ object Solution { } ``` -### rust +### Rust: ```rust impl Solution { @@ -991,7 +993,7 @@ impl Solution { } ``` -### C# +### C#: ```csharp //递归 @@ -1042,3 +1044,4 @@ public class Solution { + From 49a38691744fbce62c7735a722b917448ab3e4a6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 14:46:55 +0800 Subject: [PATCH 069/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200101.=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\344\272\214\345\217\211\346\240\221.md" | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" index 4418c62dc4..36b39740d0 100644 --- "a/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0101.\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221.md" @@ -13,9 +13,11 @@ ![101. 对称二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144607387.png) -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf), 相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 **首先想清楚,判断对称二叉树要比较的是哪两个节点,要比较的可不是左右节点!** @@ -41,7 +43,7 @@ 那么我们先来看看递归法的代码应该怎么写。 -## 递归法 +### 递归法 递归三部曲 @@ -159,13 +161,13 @@ public: **所以建议大家做题的时候,一定要想清楚逻辑,每一步做什么。把题目所有情况想到位,相应的代码写出来之后,再去追求简洁代码的效果。** -## 迭代法 +### 迭代法 这道题目我们也可以使用迭代法,但要注意,这里的迭代法可不是前中后序的迭代写法,因为本题的本质是判断两个树是否是相互翻转的,其实已经不是所谓二叉树遍历的前中后序的关系了。 这里我们可以使用队列来比较两个树(根节点的左右子树)是否相互翻转,(**注意这不是层序遍历**) -### 使用队列 +#### 使用队列 通过队列来判断根节点的左子树和右子树的内侧和外侧是否相等,如动画所示: @@ -207,7 +209,7 @@ public: }; ``` -### 使用栈 +#### 使用栈 细心的话,其实可以发现,这个迭代法,其实是把左右两个子树要比较的元素顺序放进一个容器,然后成对成对的取出来进行比较,那么其实使用栈也是可以的。 @@ -254,12 +256,12 @@ public: 这两道题目基本和本题是一样的,只要稍加修改就可以AC。 -* 100.相同的树 -* 572.另一个树的子树 +* [100.相同的树](https://leetcode.cn/problems/same-tree/) +* [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/) -# 其他语言版本 +## 其他语言版本 -Java +### Java: ```Java /** @@ -364,7 +366,7 @@ Java ``` -Python +### Python: 递归法: ```python @@ -470,7 +472,8 @@ class Solution: return True ``` -Go +### Go: + ```go /** * Definition for a binary tree node. @@ -521,8 +524,7 @@ func isSymmetric(root *TreeNode) bool { } ``` - -JavaScript +### JavaScript: 递归判断是否为对称二叉树: ```javascript @@ -610,7 +612,7 @@ var isSymmetric = function(root) { }; ``` -TypeScript: +### TypeScript: > 递归法 @@ -679,7 +681,7 @@ function isSymmetric(root: TreeNode | null): boolean { }; ``` -Swift: +### Swift: > 递归 ```swift @@ -761,7 +763,7 @@ func isSymmetric3(_ root: TreeNode?) -> Bool { } ``` -Scala +### Scala: > 递归: ```scala @@ -835,7 +837,7 @@ object Solution { } ``` -## Rust +### Rust: 递归: ```rust @@ -900,3 +902,4 @@ impl Solution { + From 4e77ae645077ade4306bbb1ef4219bc4e41c7d29 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 14:57:25 +0800 Subject: [PATCH 070/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\244\247\346\267\261\345\272\246.md" | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" index 7130867bf9..0504437521 100644 --- "a/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" +++ "b/problems/0104.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246.md" @@ -5,6 +5,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -23,17 +24,19 @@ 返回它的最大深度 3 。 -# 思路 +## 算法公开课 -看完本篇可以一起做了如下两道题目: +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -* 104.二叉树的最大深度 -* 559.n叉树的最大深度 +## 思路 + +看完本篇可以一起做了如下两道题目: -《代码随想录》算法视频公开课:[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [559.n叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) -## 递归法 +### 递归法 本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。 @@ -164,7 +167,7 @@ public: }; ``` -## 迭代法 +### 迭代法 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。 @@ -202,10 +205,11 @@ public: }; ``` - 那么我们可以顺便解决一下n叉树的最大深度问题 -# 559.n叉树的最大深度 +## 相关题目推荐 + +### 559.n叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) @@ -219,11 +223,11 @@ public: 我们应返回其最大深度,3。 -思路: +### 思路 依然可以提供递归法和迭代法,来解决这个问题,思路是和二叉树思路一样的,直接给出代码如下: -## 递归法 +#### 递归法 c++代码: @@ -240,7 +244,7 @@ public: } }; ``` -## 迭代法 +#### 迭代法 依然是层序遍历,代码如下: @@ -267,11 +271,11 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## java +### Java: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```java class solution { @@ -344,7 +348,8 @@ class solution { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 + ```java class Solution { /*递归法,后序遍历求root节点的高度*/ @@ -391,9 +396,9 @@ class solution { } ``` -## python +### Python : -### 104.二叉树的最大深度 +104.二叉树的最大深度 递归法: ```python @@ -448,7 +453,7 @@ class Solution: ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 递归法: ```python @@ -522,9 +527,10 @@ class Solution: return max_depth ``` +### Go: + +104.二叉树的最大深度 -## go -### 104.二叉树的最大深度 ```go /** * definition for a binary tree node. @@ -574,7 +580,7 @@ func maxdepth(root *treenode) int { ``` -### 559. n叉树的最大深度 +559. n叉树的最大深度 ```go func maxDepth(root *Node) int { @@ -598,9 +604,9 @@ func maxDepth(root *Node) int { } ``` -## javascript +### Javascript : -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```javascript var maxdepth = function(root) { @@ -649,7 +655,7 @@ var maxDepth = function(root) { }; ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 N叉树的最大深度 递归写法 ```js @@ -683,9 +689,9 @@ var maxDepth = function(root) { }; ``` -## TypeScript +### TypeScript: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```typescript // 后续遍历(自下而上) @@ -728,7 +734,7 @@ function maxDepth(root: TreeNode | null): number { }; ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 ```typescript // 后续遍历(自下而上) @@ -756,9 +762,9 @@ function maxDepth(root: TreeNode | null): number { ``` -## C +### C: -### 104.二叉树的最大深度 +104.二叉树的最大深度 二叉树最大深度递归 ```c @@ -814,9 +820,9 @@ int maxDepth(struct TreeNode* root){ } ``` -## Swift +### Swift: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```swift // 递归 - 后序 @@ -856,7 +862,7 @@ func maxDepth(_ root: TreeNode?) -> Int { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 ```swift // 递归 @@ -893,9 +899,10 @@ func maxDepth1(_ root: Node?) -> Int { } ``` -## Scala +### Scala: + +104.二叉树的最大深度 -### 104.二叉树的最大深度 递归法: ```scala object Solution { @@ -934,7 +941,7 @@ object Solution { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 递归法: ```scala @@ -972,8 +979,8 @@ object Solution { } ``` -## rust -### 0104.二叉树的最大深度 +### Rust: +0104.二叉树的最大深度 递归: ```rust From 600a42730c360df82e626ce91e27efc00342b310 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:03:31 +0800 Subject: [PATCH 071/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\345\260\217\346\267\261\345\272\246.md" | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" index a1fc8a9355..61f9beb785 100644 --- "a/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" +++ "b/problems/0111.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246.md" @@ -26,9 +26,11 @@ 返回它的最小深度 2. -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 看完了这篇[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),再来看看如何求最小深度。 @@ -52,7 +54,7 @@ 什么是叶子节点,左右孩子都为空的节点才是叶子节点! -## 递归法 +### 递归法 来来来,一起递归三部曲: @@ -199,7 +201,7 @@ public: }; ``` -## 迭代法 +### 迭代法 相对于[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),本题还可以使用层序遍历的方式来解决,思路是一样的。 @@ -237,10 +239,10 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java: ```Java class Solution { @@ -300,7 +302,7 @@ class Solution { } ``` -## Python +### Python : 递归法(版本一) @@ -400,9 +402,7 @@ class Solution: return depth ``` - - -## Go +### Go: ```go /** @@ -463,7 +463,7 @@ func minDepth(root *TreeNode) int { ``` -## JavaScript +### JavaScript: 递归法: @@ -509,7 +509,7 @@ var minDepth = function(root) { }; ``` -## TypeScript +### TypeScript: > 递归法 @@ -547,7 +547,7 @@ function minDepth(root: TreeNode | null): number { }; ``` -## Swift +### Swift: > 递归 ```Swift @@ -594,7 +594,7 @@ func minDepth(_ root: TreeNode?) -> Int { ``` -## Scala +### Scala: 递归法: ```scala @@ -633,7 +633,8 @@ object Solution { } ``` -rust: +### Rust: + ```rust impl Solution { // 递归 From 40f0e737046592e9b32075f7dad43b305158f90b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:08:43 +0800 Subject: [PATCH 072/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\347\202\271\344\270\252\346\225\260.md" | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" index 795a6f3777..d54f9b85d2 100644 --- "a/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" +++ "b/problems/0222.\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\347\232\204\350\212\202\347\202\271\344\270\252\346\225\260.md" @@ -29,14 +29,17 @@ * 0 <= Node.val <= 5 * 10^4 * 题目数据保证输入的树是 完全二叉树 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 -《代码随想录》算法视频公开课:[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频在看本篇题解,更有助于大家对本题的理解。 本篇给出按照普通二叉树的求法以及利用完全二叉树性质的求法。 -## 普通二叉树 +### 普通二叉树 首先按照普通二叉树的逻辑来求。 @@ -44,7 +47,7 @@ 递归遍历的顺序依然是后序(左右中)。 -### 递归 +#### 递归 如果对求二叉树深度还不熟悉的话,看这篇:[二叉树:看看这些树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)。 @@ -112,7 +115,7 @@ public: **网上基本都是这个精简的代码版本,其实不建议大家照着这个来写,代码确实精简,但隐藏了一些内容,连遍历的顺序都看不出来,所以初学者建议学习版本一的代码,稳稳的打基础**。 -### 迭代法 +#### 迭代 如果对求二叉树层序遍历还不熟悉的话,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -142,7 +145,7 @@ public: * 时间复杂度:O(n) * 空间复杂度:O(n) -## 完全二叉树 +### 完全二叉树 以上方法都是按照普通二叉树来做的,对于完全二叉树特性不了解的同学可以看这篇 [关于二叉树,你该了解这些!](https://programmercarl.com/二叉树理论基础.html),这篇详细介绍了各种二叉树的特性。 @@ -249,9 +252,9 @@ public: * 时间复杂度:O(log n × log n) * 空间复杂度:O(log n) -# 其他语言版本 +## 其他语言版本 -## Java +### Java: ```java class Solution { // 通用递归解法 @@ -312,7 +315,7 @@ class Solution { } ``` -## Python +### Python: 递归法: ```python @@ -408,7 +411,7 @@ class Solution: # 利用完全二叉树特性 return 1+self.countNodes(root.left)+self.countNodes(root.right) ``` -## Go +### Go: 递归版本 @@ -488,9 +491,7 @@ func countNodes(root *TreeNode) int { } ``` - - -## JavaScript: +### JavaScript: 递归版本 ```javascript @@ -559,7 +560,7 @@ var countNodes = function(root) { }; ``` -## TypeScrpt: +### TypeScrpt: > 递归法 @@ -614,7 +615,7 @@ function countNodes(root: TreeNode | null): number { }; ``` -## C: +### C: 递归法 ```c @@ -690,7 +691,7 @@ int countNodes(struct TreeNode* root){ } ``` -## Swift: +### Swift: > 递归 ```swift @@ -758,7 +759,7 @@ func countNodes(_ root: TreeNode?) -> Int { } ``` -## Scala +### Scala: 递归: ```scala @@ -821,9 +822,9 @@ object Solution { } ``` -rust: +### Rust: -// 递归 +递归 ```rust use std::cell::RefCell; use std::rc::Rc; @@ -838,7 +839,7 @@ impl Solution { } ``` -// 迭代 +迭代 ```rust use std::rc::Rc; use std::cell::RefCell; From ff6ff4adb86b80bc748c34f5f1c679f08d9c1ec2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:12:28 +0800 Subject: [PATCH 073/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...241\344\272\214\345\217\211\346\240\221.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" index e10a612afd..c7df9c8f19 100644 --- "a/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0110.\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.md" @@ -33,8 +33,9 @@ 返回 false 。 +## 算法公开课 -**《代码随想录》算法视频公开课:[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 题外话 @@ -357,7 +358,7 @@ public: ## 其他语言版本 -### Java +### Java: ```Java class Solution { @@ -498,7 +499,7 @@ class Solution { } ``` -### Python +### Python: 递归法: @@ -620,7 +621,7 @@ class Solution: height_map[real_node] = 1 + max(left, right) return True ``` -### Go +### Go: ```Go func isBalanced(root *TreeNode) bool { @@ -652,7 +653,7 @@ func max(a, b int) int { } ``` -### JavaScript +### JavaScript: 递归法: @@ -723,7 +724,7 @@ var isBalanced = function (root) { }; ``` -### TypeScript +### TypeScript: ```typescript // 递归法 @@ -741,7 +742,7 @@ function isBalanced(root: TreeNode | null): boolean { }; ``` -### C +### C: 递归法: @@ -876,7 +877,7 @@ func getHeight(_ root: TreeNode?) -> Int { } ``` -### rust +### Rust: 递归 @@ -912,3 +913,4 @@ impl Solution { + From 95b5bb195d5af819f553cafaede57fd4c990a96d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:18:37 +0800 Subject: [PATCH 074/161] =?UTF-8?q?0257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\234\211\350\267\257\345\276\204.md" | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" index 061535072d..44c0fd85e3 100644 --- "a/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" +++ "b/problems/0257.\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204.md" @@ -18,9 +18,11 @@ 示例: ![257.二叉树的所有路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415161576.png) -# 思路 +## 算法公开课 -**《代码随想录》算法视频公开课:[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这道题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。 @@ -32,7 +34,7 @@ 我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。** -## 递归 +### 递归 1. 递归函数参数以及返回值 @@ -305,7 +307,7 @@ public: **综合以上,第二种递归的代码虽然精简但把很多重要的点隐藏在了代码细节里,第一种递归写法虽然代码多一些,但是把每一个逻辑处理都完整的展现出来了。** -## 拓展 +### 拓展 这里讲解本题解的写法逻辑以及一些更具体的细节,下面的讲解中,涉及到C++语法特性,如果不是C++的录友,就可以不看了,避免越看越晕。 @@ -328,7 +330,7 @@ public: 所以,第一个代码版本中,我才使用 vector 类型的path,这样方便给大家演示代码中回溯的操作。 vector类型的path,不管 每次 路径收集的数字是几位数,总之一定是int,所以就一次 pop_back就可以。 -## 迭代法 +### 迭代法 至于非递归的方式,我们可以依然可以使用前序遍历的迭代方式来模拟遍历路径的过程,对该迭代方式不了解的同学,可以看文章[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。 @@ -368,7 +370,7 @@ public: ``` 当然,使用java的同学,可以直接定义一个成员变量为object的栈`Stack stack = new Stack<>();`,这样就不用定义两个栈了,都放到一个栈里就可以了。 -# 总结 +## 总结 **本文我们开始初步涉及到了回溯,很多同学过了这道题目,可能都不知道自己其实使用了回溯,回溯和递归都是相伴相生的。** @@ -380,13 +382,9 @@ public: 对于本题充分了解递归与回溯的过程之后,有精力的同学可以再去实现迭代法。 +## 其他语言版本 - - - -# 其他语言版本 - -## Java: +### Java: ```Java //解法一 @@ -492,9 +490,9 @@ class Solution { } ``` --- -## Python: - - +### Python: + + 递归法+回溯 ```Python # Definition for a binary tree node. @@ -552,7 +550,7 @@ class Solution: self.traversal(cur.right, path[:], result) ``` - + 递归法+隐形回溯(版本二) ```Python # Definition for a binary tree node. @@ -610,7 +608,7 @@ class Solution: --- -## Go: +### Go: 递归法: @@ -672,7 +670,7 @@ func binaryTreePaths(root *TreeNode) []string { ``` --- -## JavaScript: +### JavaScript: 递归法: @@ -725,7 +723,7 @@ var binaryTreePaths = function(root) { }; ``` -## TypeScript: +### TypeScript: > 递归法 @@ -779,7 +777,7 @@ function binaryTreePaths(root: TreeNode | null): string[] { }; ``` -## Swift: +### Swift: > 递归/回溯 ```swift @@ -846,7 +844,7 @@ func binaryTreePaths(_ root: TreeNode?) -> [String] { } ``` -## Scala: +### Scala: 递归: ```scala @@ -876,7 +874,7 @@ object Solution { } ``` -rust: +### Rust: ```rust // 递归 @@ -907,4 +905,3 @@ impl Solution { - From a9039c0d2dd2c625c98009998677b20089b46524 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:39:36 +0800 Subject: [PATCH 075/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\345\255\220\344\271\213\345\222\214.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" index aa2868df4e..c1ad602d5f 100644 --- "a/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" +++ "b/problems/0404.\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" @@ -16,9 +16,9 @@ ![404.左叶子之和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151927654.png) -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -48,7 +48,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) } ``` -## 递归法 +### 递归法 递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。 @@ -131,11 +131,11 @@ public: return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); } }; -``` +``` 精简之后的代码其实看不出来用的是什么遍历方式了,对于算法初学者以上根据第一个版本来学习。 -## 迭代法 +### 迭代法 本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:迭代法统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中的写法,可以写出一个前序遍历的迭代法。 @@ -177,7 +177,7 @@ public: ## 其他语言版本 -### Java +### Java: **递归** @@ -246,7 +246,7 @@ class Solution { ``` -### Python +### Python: 递归 ```python # Definition for a binary tree node. @@ -316,7 +316,7 @@ class Solution: ``` -### Go +### Go: **递归法** @@ -368,7 +368,7 @@ func sumOfLeftLeaves(root *TreeNode) int { ``` -### JavaScript +### JavaScript: **递归法** @@ -417,7 +417,7 @@ var sumOfLeftLeaves = function(root) { }; ``` -### TypeScript +### TypeScript: > 递归法 @@ -462,7 +462,7 @@ function sumOfLeftLeaves(root: TreeNode | null): number { }; ``` -### Swift +### Swift: **递归法** ```swift @@ -511,7 +511,7 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int { } ``` -### C +### C: 递归法: ```c int sumOfLeftLeaves(struct TreeNode* root){ @@ -561,7 +561,7 @@ int sumOfLeftLeaves(struct TreeNode* root){ } ``` -### Scala +### Scala: **递归:** ```scala @@ -600,7 +600,7 @@ object Solution { } ``` -### Rust +### Rust: **递归** @@ -656,3 +656,4 @@ impl Solution { + From f3a370113058a82be88164ddeabf5443f82f62a0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:41:28 +0800 Subject: [PATCH 076/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20513.=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...246\344\270\213\350\247\222\347\232\204\345\200\274.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" index 743b0df9be..7ef934cc88 100644 --- "a/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" +++ "b/problems/0513.\346\211\276\346\240\221\345\267\246\344\270\213\350\247\222\347\232\204\345\200\274.md" @@ -20,9 +20,9 @@ ![513.找树左下角的值1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153017586.png) -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -614,7 +614,7 @@ object Solution { } ``` -### rust +### Rust **层序遍历** @@ -689,3 +689,4 @@ impl Solution { + From bf6c4e7f7b3c8f118960362ab735f8e4977e0e58 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:53:29 +0800 Subject: [PATCH 077/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 39285a3bc1..240462b6ff 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -21,9 +21,9 @@ 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -32,8 +32,8 @@ 那么接下来我通过详细讲解如下两道题,来回答这个问题: -* 112.路径总和 -* 113.路径总和ii +* [112.路径总和](https://leetcode.cn/problems/path-sum/) +* [113.路径总和ii](https://leetcode.cn/problems/path-sum-ii/) 这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。 @@ -218,7 +218,9 @@ public: 如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 -# 113. 路径总和ii +## 相关题目推荐 + +### 113. 路径总和ii [力扣题目链接](https://leetcode.cn/problems/path-sum-ii/) @@ -232,7 +234,7 @@ public: ![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) -## 思路 +### 思路 113.路径总和ii要遍历整个树,找到所有路径,**所以递归函数不要返回值!** @@ -289,7 +291,7 @@ public: 至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。 -## 总结 +### 总结 本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。 @@ -300,11 +302,11 @@ public: -# 其他语言版本 +## 其他语言版本 -## java +### Java -### 0112.路径总和 +#### 0112.路径总和 ```java class solution { @@ -422,7 +424,7 @@ class solution { } ``` -### 0113.路径总和-ii +#### 0113.路径总和-ii ```java class solution { @@ -529,9 +531,9 @@ class Solution { } ``` -## python +### Python -### 0112.路径总和 +#### 0112.路径总和 (版本一) 递归 ```python @@ -618,7 +620,7 @@ class Solution: -### 0113.路径总和-ii +#### 0113.路径总和-ii (版本一) 递归 ```python @@ -719,9 +721,9 @@ class Solution: ``` -## go +### Go -### 112. 路径总和 +#### 112. 路径总和 ```go //递归法 @@ -746,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -### 113. 路径总和 II +#### 113. 路径总和 II ```go /** @@ -786,9 +788,9 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { } ``` -## javascript +### Javascript -### 0112.路径总和 +#### 0112.路径总和 **递归** @@ -852,7 +854,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -### 0113.路径总和-ii +#### 0113.路径总和-ii **递归** @@ -950,9 +952,9 @@ let pathSum = function(root, targetSum) { }; ``` -## TypeScript +### TypeScript -### 0112.路径总和 +#### 0112.路径总和 **递归法:** @@ -1034,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -### 0112.路径总和 ii +#### 0112.路径总和 ii **递归法:** @@ -1070,9 +1072,9 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { }; ``` -## Swift +### Swift -### 0112.路径总和 +#### 0112.路径总和 **递归** @@ -1141,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -### 0113.路径总和 II +#### 0113.路径总和 II **递归** @@ -1192,10 +1194,10 @@ func traversal(_ cur: TreeNode?, count: Int) { } ``` -## C +### C -> 0112.路径总和 -> 递归法: +#### 0112.路径总和 +递归法: ```c bool hasPathSum(struct TreeNode* root, int targetSum){ @@ -1252,7 +1254,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ } ``` -> 0113.路径总和 II +#### 0113.路径总和 II ```c int** ret; @@ -1317,9 +1319,9 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur } ``` -## Scala +### Scala -### 0112.路径总和 +#### 0112.路径总和 **递归:** @@ -1369,7 +1371,7 @@ object Solution { } ``` -### 0113.路径总和 II +#### 0113.路径总和 II **递归:** @@ -1405,9 +1407,9 @@ object Solution { } ``` -## rust +### Rust -### 112.路径总和.md +#### 0112.路径总和 递归: @@ -1461,7 +1463,7 @@ impl Solution { } ``` -### 113.路径总和-ii +#### 0113.路径总和-ii ```rust impl Solution { @@ -1514,3 +1516,4 @@ impl Solution { + From fe9323900758c0f056299728d1551ab57d1efab3 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 18:00:07 +0800 Subject: [PATCH 078/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200106.=E4=BB=8E?= =?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...40\344\272\214\345\217\211\346\240\221.md" | 40 +++++++++---------- ...57\345\276\204\346\200\273\345\222\214.md" | 38 +++++++++--------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" index a0bab99974..0144fc5c05 100644 --- "a/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0106.\344\273\216\344\270\255\345\272\217\344\270\216\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221.md" @@ -29,9 +29,9 @@ ![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder); 完整代码如下: -### C++完整代码 - ```CPP class Solution { private: @@ -281,8 +279,6 @@ public: 下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割) -### C++优化版本 - ```CPP class Solution { private: @@ -400,8 +396,9 @@ public: }; ``` +## 相关题目推荐 -# 105.从前序与中序遍历序列构造二叉树 +### 105.从前序与中序遍历序列构造二叉树 [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) @@ -418,7 +415,7 @@ public: ![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) -## 思路 +### 思路 本题和106是一样的道理。 @@ -547,7 +544,7 @@ public: }; ``` -# 思考题 +## 思考题 前序和中序可以唯一确定一棵二叉树。 @@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 所以前序和后序不能唯一确定一棵二叉树! -# 总结 +## 总结 之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。 @@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 106.从中序与后序遍历序列构造二叉树 @@ -688,7 +685,7 @@ class Solution { } ``` -## Python +### Python 105.从前序与中序遍历序列构造二叉树 @@ -754,7 +751,7 @@ class Solution: return root ``` -## Go +### Go 106 从中序与后序遍历序列构造二叉树 @@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode { ``` - - -## JavaScript +### JavaScript ```javascript var buildTree = function(inorder, postorder) { @@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) { }; ``` -## TypeScript +### TypeScript > 106.从中序与后序遍历序列构造二叉树 @@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null { }; ``` -## C +### C 106 从中序与后序遍历序列构造二叉树 @@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in } ``` -## Swift +### Swift 105 从前序与中序遍历序列构造二叉树 @@ -1140,7 +1135,7 @@ class Solution_0106 { } ``` -## Scala +### Scala 106 从中序与后序遍历序列构造二叉树 @@ -1188,7 +1183,7 @@ object Solution { } ``` -## rust +### Rust 106 从中序与后序遍历序列构造二叉树 @@ -1238,3 +1233,4 @@ impl Solution { + diff --git "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" index 240462b6ff..be03f719e1 100644 --- "a/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" +++ "b/problems/0112.\350\267\257\345\276\204\346\200\273\345\222\214.md" @@ -306,7 +306,7 @@ public: ### Java -#### 0112.路径总和 +0112.路径总和 ```java class solution { @@ -424,7 +424,7 @@ class solution { } ``` -#### 0113.路径总和-ii +0113.路径总和-ii ```java class solution { @@ -533,7 +533,7 @@ class Solution { ### Python -#### 0112.路径总和 +0112.路径总和 (版本一) 递归 ```python @@ -620,7 +620,7 @@ class Solution: -#### 0113.路径总和-ii +0113.路径总和-ii (版本一) 递归 ```python @@ -723,7 +723,7 @@ class Solution: ``` ### Go -#### 112. 路径总和 +112. 路径总和 ```go //递归法 @@ -748,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -#### 113. 路径总和 II +113. 路径总和 II ```go /** @@ -790,7 +790,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { ### Javascript -#### 0112.路径总和 +0112.路径总和 **递归** @@ -854,7 +854,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -#### 0113.路径总和-ii +0113.路径总和-ii **递归** @@ -954,7 +954,7 @@ let pathSum = function(root, targetSum) { ### TypeScript -#### 0112.路径总和 +0112.路径总和 **递归法:** @@ -1036,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -#### 0112.路径总和 ii +0112.路径总和 ii **递归法:** @@ -1074,7 +1074,7 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { ### Swift -#### 0112.路径总和 +0112.路径总和 **递归** @@ -1143,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -#### 0113.路径总和 II +0113.路径总和 II **递归** @@ -1196,7 +1196,8 @@ func traversal(_ cur: TreeNode?, count: Int) { ### C -#### 0112.路径总和 +0112.路径总和 + 递归法: ```c @@ -1254,7 +1255,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ } ``` -#### 0113.路径总和 II +0113.路径总和 II ```c int** ret; @@ -1321,7 +1322,7 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur ### Scala -#### 0112.路径总和 +0112.路径总和 **递归:** @@ -1371,7 +1372,7 @@ object Solution { } ``` -#### 0113.路径总和 II +0113.路径总和 II **递归:** @@ -1409,7 +1410,7 @@ object Solution { ### Rust -#### 0112.路径总和 +0112.路径总和 递归: @@ -1463,7 +1464,7 @@ impl Solution { } ``` -#### 0113.路径总和-ii +0113.路径总和-ii ```rust impl Solution { @@ -1516,4 +1517,3 @@ impl Solution { - From b4eefe985945fdeaf4e1e34ebbc90b74258d43e2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 18:54:16 +0800 Subject: [PATCH 079/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...200\345\244\247\344\272\214\345\217\211\346\240\221.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" index 33a9176ed1..77d7980f3d 100644 --- "a/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0654.\346\234\200\345\244\247\344\272\214\345\217\211\346\240\221.md" @@ -25,9 +25,9 @@ 给定的数组的大小在 [1, 1000] 之间。 -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -310,7 +310,7 @@ class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: return self.traversal(nums, 0, len(nums)) - ``` +``` (版本三) 使用切片 @@ -587,3 +587,4 @@ impl Solution { + From eadc704c38816cfdb7f21f22d3489dc38ea5aecc Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:18:53 +0800 Subject: [PATCH 080/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200617.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\345\271\266\344\272\214\345\217\211\346\240\221.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 18a245c4db..44092aaebc 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -19,9 +19,9 @@ 注意: 合并必须从两个树的根节点开始。 -# 视频讲解 +# 算法公开课 -**《代码随想录》算法视频公开课:[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -164,7 +164,7 @@ public: }; ``` -## 迭代法 +### 迭代法 使用迭代法,如何同时处理两棵树呢? @@ -716,7 +716,7 @@ object Solution { } ``` -### rust +### Rust 递归: @@ -793,4 +793,3 @@ impl Solution { - From ac239bc437cfa8de6859fa8a98cc04b9cf6d1d4c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:20:25 +0800 Subject: [PATCH 081/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200700.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...221\344\270\255\347\232\204\346\220\234\347\264\242.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" index 13064f9717..bc21bab872 100644 --- "a/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" +++ "b/problems/0700.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\220\234\347\264\242.md" @@ -18,9 +18,9 @@ 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -415,7 +415,7 @@ object Solution { } ``` -### rust +### Rust 递归: @@ -469,3 +469,4 @@ impl Solution { + From 7746734d79d4726143375aaf1a8a8f835483346e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:22:33 +0800 Subject: [PATCH 082/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200098.=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 95b657a58f..a48ec0656b 100644 --- "a/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0098.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -20,18 +20,18 @@ ![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png) -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。 有了这个特性,**验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。** -## 递归法 +### 递归法 可以递归中序遍历将二叉搜索树转变成一个数组,代码如下: @@ -211,7 +211,7 @@ public: 最后这份代码看上去整洁一些,思路也清晰。 -## 迭代法 +### 迭代法 可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html) @@ -245,7 +245,7 @@ public: 在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。 -# 总结 +## 总结 这道题目是一个简单题,但对于没接触过的同学还是有难度的。 @@ -254,10 +254,10 @@ public: 只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java //使用統一迭代法 @@ -369,7 +369,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一)利用中序递增性质,转换成数组 ```python @@ -479,7 +479,7 @@ class Solution: ``` -## Go +### Go ```Go func isValidBST(root *TreeNode) bool { @@ -526,7 +526,7 @@ func isValidBST(root *TreeNode) bool { } ``` -## JavaScript +### JavaScript 辅助数组解决 @@ -595,7 +595,7 @@ var isValidBST = function (root) { }; ``` -## TypeScript +### TypeScript > 辅助数组解决: @@ -637,7 +637,7 @@ function isValidBST(root: TreeNode | null): boolean { }; ``` -## Scala +### Scala 辅助数组解决: ```scala @@ -682,7 +682,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -735,3 +735,4 @@ impl Solution { + From 74bbca292355dbc8ce9316329bfea503234ef0d6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:26:08 +0800 Subject: [PATCH 083/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\347\273\235\345\257\271\345\267\256.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" index 3e4391d62c..56911858dd 100644 --- "a/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" +++ "b/problems/0530.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\345\260\217\347\273\235\345\257\271\345\267\256.md" @@ -19,12 +19,12 @@ 提示:树中至少有 2 个节点。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。 @@ -32,7 +32,7 @@ 遇到在二叉搜索树上求什么最值啊,差值之类的,就把它想成在一个有序数组上求最值,求差值,这样就简单多了。 -## 递归 +### 递归 那么二叉搜索树采用中序遍历,其实就是一个有序数组。 @@ -102,7 +102,7 @@ public: 是不是看上去也并不复杂! -## 迭代 +### 迭代 看过这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式的写法就不能统一一下么?](https://programmercarl.com/二叉树的统一迭代法.html)文章之后,不难写出两种中序遍历的迭代法。 @@ -135,7 +135,7 @@ public: }; ``` -# 总结 +## 总结 **遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。** @@ -145,10 +145,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归 ```java @@ -235,7 +235,7 @@ class Solution { } } ``` -## Python +### Python 递归法(版本一)利用中序递增,结合数组 ```python @@ -313,7 +313,7 @@ class Solution: ``` -## Go: +### Go 中序遍历,然后计算最小差值 ```go @@ -340,7 +340,7 @@ func getMinimumDifference(root *TreeNode) int { } ``` -## JavaScript +### JavaScript 递归 先转换为有序数组 ```javascript /** @@ -415,7 +415,7 @@ var getMinimumDifference = function(root) { } ``` -## TypeScript +### TypeScript > 辅助数组解决 @@ -482,7 +482,7 @@ function getMinimumDifference(root: TreeNode | null): number { }; ``` -## Scala +### Scala 构建二叉树的有序数组: @@ -561,7 +561,7 @@ object Solution { } ``` -## rust +### Rust 构建二叉树的有序数组: @@ -652,3 +652,4 @@ impl Solution { + From c1da5bb2339fbe525eaf1cc4f09702b1caefbc1d Mon Sep 17 00:00:00 2001 From: El nino Date: Sun, 23 Jul 2023 10:12:24 +0800 Subject: [PATCH 084/161] =?UTF-8?q?typo=EF=BC=9B=E4=B8=BA=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=E6=B7=BB=E5=8A=A0=20cpp=20=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E9=AB=98=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" index eb6a26fe6d..af4073f077 100644 --- "a/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\276\350\256\272\346\267\261\346\220\234\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -16,13 +16,13 @@ ## dfs 与 bfs 区别 -提到深度优先搜索(dfs),就不得不说和广度优先有什么区别(bfs) +提到深度优先搜索(dfs),就不得不说和广度优先搜索(bfs)有什么区别 先来了解dfs的过程,很多录友可能对dfs(深度优先搜索),bfs(广度优先搜索)分不清。 先给大家说一下两者大概的区别: -* dfs是可一个方向去搜,不到黄河不回头,直到遇到绝境了,搜不下去了,在换方向(换方向的过程就涉及到了回溯)。 +* dfs是可一个方向去搜,不到黄河不回头,直到遇到绝境了,搜不下去了,再换方向(换方向的过程就涉及到了回溯)。 * bfs是先把本节点所连接的所有节点遍历一遍,走到下一个节点的时候,再把连接节点的所有节点遍历一遍,搜索方向更像是广度,四面八方的搜索过程。 当然以上讲的是,大体可以这么理解,接下来 我们详细讲解dfs,(bfs在用单独一篇文章详细讲解) @@ -60,26 +60,26 @@ 上图演示中,其实我并没有把 所有的 从节点1 到节点6的dfs(深度优先搜索)的过程都画出来,那样太冗余了,但 已经把dfs 关键的地方都涉及到了,关键就两点: -* 搜索方向,是认准一个方向搜,直到碰壁之后在换方向 +* 搜索方向,是认准一个方向搜,直到碰壁之后再换方向 * 换方向是撤销原路径,改为节点链接的下一个路径,回溯的过程。 ## 代码框架 -正式因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。 +正是因为dfs搜索可一个方向,并需要回溯,所以用递归的方式来实现是最方便的。 -很多录友对回溯很陌生,建议先看看码随想录,[回溯算法章节](https://programmercarl.com/回溯算法理论基础.html)。 +很多录友对回溯很陌生,建议先看看代码随想录,[回溯算法章节](https://programmercarl.com/回溯算法理论基础.html)。 有递归的地方就有回溯,那么回溯在哪里呢? 就地递归函数的下面,例如如下代码: -``` +```cpp void dfs(参数) { 处理节点 dfs(图,选择的节点); // 递归 回溯,撤销处理结果 } -``` +``` 可以看到回溯操作就在递归函数的下面,递归和回溯是相辅相成的。 @@ -89,7 +89,7 @@ void dfs(参数) { 我们在回顾一下[回溯法](https://programmercarl.com/回溯算法理论基础.html)的代码框架: -``` +```cpp void backtracking(参数) { if (终止条件) { 存放结果; @@ -102,11 +102,11 @@ void backtracking(参数) { } } -``` +``` 回溯算法,其实就是dfs的过程,这里给出dfs的代码框架: -``` +```cpp void dfs(参数) { if (终止条件) { 存放结果; @@ -136,9 +136,9 @@ void dfs(参数) { 1. 确认递归函数,参数 -``` +```cpp void dfs(参数) -``` +``` 通常我们递归的时候,我们递归搜索需要了解哪些参数,其实也可以在写递归函数的时候,发现需要什么参数,再去补充就可以。 @@ -146,7 +146,7 @@ void dfs(参数) 例如这样: -``` +```cpp vector> result; // 保存符合条件的所有路径 vector path; // 起点到终点的路径 void dfs (图,目前搜索的节点) @@ -158,7 +158,7 @@ void dfs (图,目前搜索的节点) 终止条件很重要,很多同学写dfs的时候,之所以容易死循环,栈溢出等等这些问题,都是因为终止条件没有想清楚。 -``` +```cpp if (终止条件) { 存放结果; return; @@ -173,7 +173,7 @@ if (终止条件) { 一般这里就是一个for循环的操作,去遍历 目前搜索节点 所能到的所有节点。 -``` +```cpp for (选择:本节点所连接的其他节点) { 处理节点; dfs(图,选择的节点); // 递归 From b68e881a659e4ef56446db940fbf12a6a6a09207 Mon Sep 17 00:00:00 2001 From: El nino Date: Sun, 23 Jul 2023 11:08:46 +0800 Subject: [PATCH 085/161] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd70d8cb6b..5fe165a71c 100644 --- a/README.md +++ b/README.md @@ -396,7 +396,7 @@ * [图论:深度优先搜索理论基础](./problems/图论深搜理论基础.md) * [图论:797.所有可能的路径](./problems/0797.所有可能的路径.md) -* [图论:广度优先搜索理论基础](./problems/图论广索理论基础.md) +* [图论:广度优先搜索理论基础](./problems/图论广搜理论基础.md) * [图论:200.岛屿数量.深搜版](./problems/0200.岛屿数量.深搜版.md) * [图论:200.岛屿数量.广搜版](./problems/0200.岛屿数量.广搜版.md) * [图论:695.岛屿的最大面积](./problems/0695.岛屿的最大面积.md) From 55b85b5251c7b4f2060795070341eaf4d97347d0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 17:57:10 +0800 Subject: [PATCH 086/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200501.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?= =?UTF-8?q?=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\344\274\227\346\225\260.md" | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 1da323435b..efbabc4ab6 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" @@ -33,20 +33,20 @@ 进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 这道题目呢,递归法我从两个维度来讲。 首先如果不是二叉搜索树的话,应该怎么解题,是二叉搜索树,又应该如何解题,两种方式做一个比较,可以加深大家对二叉树的理解。 -## 递归法 +### 递归法 -### 如果不是二叉搜索树 +#### 如果不是二叉搜索树 如果不是二叉搜索树,最直观的方法一定是把这个树都遍历了,用map统计频率,把频率排个序,最后取前面高频的元素的集合。 @@ -140,7 +140,7 @@ public: **所以如果本题没有说是二叉搜索树的话,那么就按照上面的思路写!** -### 是二叉搜索树 +#### 是二叉搜索树 **既然是搜索树,它中序遍历就是有序的**。 @@ -271,7 +271,7 @@ public: ``` -## 迭代法 +### 迭代法 只要把中序遍历转成迭代,中间节点的处理逻辑完全一样。 @@ -326,7 +326,7 @@ public: }; ``` -# 总结 +## 总结 本题在递归法中,我给出了如果是普通二叉树,应该怎么求众数。 @@ -345,10 +345,10 @@ public: > **需要强调的是 leetcode上的耗时统计是非常不准确的,看个大概就行,一样的代码耗时可以差百分之50以上**,所以leetcode的耗时统计别太当回事,知道理论上的效率优劣就行了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 暴力法 @@ -472,7 +472,7 @@ class Solution { } } ``` -統一迭代法 +统一迭代法 ```Java class Solution { public int[] findMode(TreeNode root) { @@ -526,7 +526,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一)利用字典 @@ -640,7 +640,7 @@ class Solution: return result ``` -## Go +### Go 计数法,不使用额外空间,利用二叉树性质,中序遍历 ```go @@ -676,7 +676,7 @@ func findMode(root *TreeNode) []int { } ``` -## JavaScript +### JavaScript 使用额外空间map的方法 ```javascript @@ -753,7 +753,7 @@ var findMode = function(root) { }; ``` -## TypeScript +### TypeScript > 辅助Map法 @@ -852,7 +852,7 @@ function findMode(root: TreeNode | null): number[] { }; ``` -## Scala +### Scala 暴力: ```scala @@ -923,7 +923,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -1015,3 +1015,4 @@ pub fn find_mode(root: Option>>) -> Vec { + From 3a2490b92fe00b5e2b6acb602f33328b48e426ad Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:41:57 +0800 Subject: [PATCH 087/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200236.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 0ebd556669..9db7409e04 100644 --- "a/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -34,12 +34,12 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉树中。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 遇到这个题目首先想的是要是能自底向上查找就好了,这样就可以找到公共祖先了。 @@ -226,7 +226,7 @@ public: }; ``` -# 总结 +## 总结 这道题目刷过的同学未必真正了解这里面回溯的过程,以及结果是如何一层一层传上去的。 @@ -243,10 +243,10 @@ public: 本题没有给出迭代法,因为迭代法不适合模拟回溯的过程。理解递归的解法就够了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -273,7 +273,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一) ```python class Solution: @@ -312,7 +312,7 @@ class Solution: return left ``` -## Go +### Go ```Go func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { @@ -343,7 +343,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { } ``` -## JavaScript +### JavaScript ```javascript var lowestCommonAncestor = function(root, p, q) { @@ -370,7 +370,7 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` -## TypeScript +### TypeScript ```typescript function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { @@ -384,7 +384,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` -## Scala +### Scala ```scala object Solution { @@ -404,7 +404,7 @@ object Solution { } ``` -## rust +### Rust ```rust impl Solution { @@ -436,3 +436,4 @@ impl Solution { + From 07e1ccca1925d16b5f9f58050d135d3a326b44af Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:52:09 +0800 Subject: [PATCH 088/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200235.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" index 9777bb0be8..2b8af06017 100644 --- "a/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" +++ "b/problems/0235.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.md" @@ -36,11 +36,11 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉搜索树中。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。 @@ -71,7 +71,7 @@ 可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! -## 递归法 +### 递归法 递归三部曲如下: @@ -203,7 +203,7 @@ public: }; ``` -## 迭代法 +### 迭代法 对于二叉搜索树的迭代法,大家应该在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)就了解了。 @@ -229,7 +229,7 @@ public: 灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕? -# 总结 +## 总结 对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。 @@ -238,10 +238,10 @@ public: 最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归法: ```java @@ -273,7 +273,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一) ```python @@ -326,7 +326,7 @@ class Solution: ``` -## Go +### Go 递归法: ```go @@ -350,7 +350,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { ``` -## JavaScript +### JavaScript 递归法: ```javascript @@ -391,7 +391,7 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` -## TypeScript +### TypeScript > 递归法: @@ -422,7 +422,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` -## Scala +### Scala 递归: @@ -453,7 +453,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -519,3 +519,4 @@ impl Solution { + From 70271161fff40fb3ab1929dcb37b19e3efe469b0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:54:12 +0800 Subject: [PATCH 089/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\205\245\346\223\215\344\275\234.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index fc4351babc..511d161c70 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -23,11 +23,11 @@ * -10^8 <= val <= 10^8 * 新值和原始二叉搜索树中的任意节点值都不同 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。 @@ -43,7 +43,7 @@ 接下来就是遍历二叉搜索树的过程了。 -## 递归 +### 递归 递归三部曲: @@ -165,7 +165,7 @@ public: **网上千篇一律的代码,可能会误导大家认为通过递归函数返回节点 这样的写法是天经地义,其实这里是有优化的!** -## 迭代 +### 迭代 再来看看迭代法,对二叉搜索树迭代写法不熟悉,可以看这篇:[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html) @@ -198,7 +198,7 @@ public: }; ``` -# 总结 +## 总结 首先在二叉搜索树中的插入操作,大家不用恐惧其重构搜索树,其实根本不用重构。 @@ -207,9 +207,9 @@ public: 最后依然给出了迭代的方法,迭代的方法就需要记录当前遍历节点的父节点了,这个和没有返回值的递归函数实现的代码逻辑是一样的。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -254,7 +254,7 @@ class Solution { } ``` ----- -## Python +### Python 递归法(版本一) ```python @@ -371,7 +371,7 @@ class Solution: ``` ----- -## Go +### Go 递归法 @@ -417,7 +417,7 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { } ``` ----- -## JavaScript +### JavaScript 有返回值的递归写法 @@ -530,7 +530,7 @@ var insertIntoBST = function (root, val) { }; ``` -## TypeScript +### TypeScript > 递归-有返回值 @@ -595,7 +595,7 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { ``` -## Scala +### Scala 递归: @@ -632,7 +632,7 @@ object Solution { } ``` -### rust +### Rust 迭代: @@ -697,3 +697,4 @@ impl Solution { + From 0f22ada3df0468334a45059a037ec6a7fa198d85 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:56:18 +0800 Subject: [PATCH 090/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200450.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\347\232\204\350\212\202\347\202\271.md" | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" index 3d73598da9..13599fd849 100644 --- "a/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" +++ "b/problems/0450.\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271.md" @@ -24,15 +24,15 @@ ![450.删除二叉搜索树中的节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20201020171048265.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。 -## 递归 +### 递归 递归三部曲: @@ -161,7 +161,7 @@ public: }; ``` -## 普通二叉树的删除方式 +### 普通二叉树的删除方式 这里我在介绍一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。 @@ -198,7 +198,7 @@ public: 这个代码是简短一些,思路也巧妙,但是不太好想,实操性不强,推荐第一种写法! -## 迭代法 +### 迭代法 删除节点的迭代法还是复杂一些的,但其本质我在递归法里都介绍了,最关键就是删除节点的操作(动画模拟的过程) @@ -246,7 +246,7 @@ public: }; ``` -# 总结 +## 总结 读完本篇,大家会发现二叉搜索树删除节点比增加节点复杂的多。 @@ -264,10 +264,10 @@ public: 迭代法其实不太容易写出来,所以如果是初学者的话,彻底掌握第一种递归写法就够了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -323,7 +323,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一) ```python class Solution: @@ -411,7 +411,7 @@ class Solution: return root ``` -## Go +### Go ```Go // 递归版本 func deleteNode(root *TreeNode, key int) *TreeNode { @@ -497,7 +497,7 @@ func deleteNode(root *TreeNode, key int) *TreeNode { } ``` -## JavaScript +### JavaScript 递归 @@ -588,7 +588,7 @@ var deleteNode = function (root, key) { } ``` -## TypeScript +### TypeScript > 递归法: @@ -652,7 +652,7 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null { }; ``` -## Scala +### Scala ```scala object Solution { @@ -682,7 +682,7 @@ object Solution { } ``` -## rust +### Rust ```rust impl Solution { @@ -720,3 +720,4 @@ impl Solution { + From e14e2169c4c303d5e690705245230323774df795 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:58:10 +0800 Subject: [PATCH 091/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200669.=E4=BF=AE?= =?UTF-8?q?=E5=89=AA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 85922a1dd8..2cfbfefc60 100644 --- "a/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0669.\344\277\256\345\211\252\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -20,17 +20,17 @@ ![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173219142.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 相信看到这道题目大家都感觉是一道简单题(事实上leetcode上也标明是简单)。 但还真的不简单! -## 递归法 +### 递归法 直接想法就是:递归处理,然后遇到 `root->val < low || root->val > high` 的时候直接return NULL,一波修改,赶紧利落。 @@ -188,7 +188,7 @@ public: 只看代码,其实不太好理解节点是如何移除的,这一块大家可以自己再模拟模拟! -## 迭代法 +### 迭代法 因为二叉搜索树的有序性,不需要使用栈模拟递归的过程。 @@ -233,7 +233,7 @@ public: }; ``` -# 总结 +## 总结 修剪二叉搜索树其实并不难,但在递归法中大家可看出我费了很大的功夫来讲解如何删除节点的,这个思路其实是比较绕的。 @@ -243,10 +243,10 @@ public: 本题我依然给出递归法和迭代法,初学者掌握递归就可以了,如果想进一步学习,就把迭代法也写一写。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java **递归** @@ -311,7 +311,7 @@ class Solution { ```` -## Python +### Python 递归法(版本一) ```python @@ -381,7 +381,7 @@ class Solution: ``` -## Go +### Go ```go // 递归 @@ -436,7 +436,7 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode { ``` -## JavaScript版本 +### JavaScript 迭代: @@ -492,7 +492,7 @@ var trimBST = function (root,low,high) { } ``` -## TypeScript +### TypeScript > 递归法 @@ -540,7 +540,7 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n }; ``` -## Scala +### Scala 递归法: @@ -557,7 +557,7 @@ object Solution { } ``` -## rust +### Rust // 递归 @@ -590,3 +590,4 @@ impl Solution { + From 572c08984f9ec9821383b3c9a51dbbf1684eb6af Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:00:08 +0800 Subject: [PATCH 092/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200108.=E5=B0=86?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\220\234\347\264\242\346\240\221.md" | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 056ef3e268..e699005edb 100644 --- "a/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0108.\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\346\215\242\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -20,11 +20,11 @@ ![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 做这道题目之前大家可以了解一下这几道: @@ -71,7 +71,7 @@ **这也是题目中强调答案不是唯一的原因。 理解这一点,这道题目算是理解到位了**。 -## 递归 +### 递归 递归三部曲: @@ -155,7 +155,7 @@ public: **注意:在调用traversal的时候传入的left和right为什么是0和nums.size() - 1,因为定义的区间为左闭右闭**。 -## 迭代法 +### 迭代法 迭代法可以通过三个队列来模拟,一个队列放遍历的节点,一个队列放左区间下标,一个队列放右区间下标。 @@ -203,7 +203,7 @@ public: }; ``` -# 总结 +## 总结 **在[二叉树:构造二叉树登场!](https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html) 和 [二叉树:构造一棵最大的二叉树](https://programmercarl.com/0654.最大二叉树.html)之后,我们顺理成章的应该构造一下二叉搜索树了,一不小心还是一棵平衡二叉搜索树**。 @@ -216,10 +216,10 @@ public: 最后依然给出迭代的方法,其实就是模拟取中间元素,然后不断分割去构造二叉树的过程。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归: 左闭右开 [left,right) ```Java @@ -315,7 +315,7 @@ class Solution { } ``` -## Python +### Python 递归法 ```python class Solution: @@ -377,7 +377,7 @@ class Solution: ``` -## Go +### Go 递归(隐含回溯) @@ -396,7 +396,7 @@ func sortedArrayToBST(nums []int) *TreeNode { } ``` -## JavaScript +### JavaScript 递归 ```javascript @@ -453,7 +453,7 @@ var sortedArrayToBST = function(nums) { return root; }; ``` -## TypeScript +### TypeScript ```typescript function sortedArrayToBST(nums: number[]): TreeNode | null { @@ -469,7 +469,7 @@ function sortedArrayToBST(nums: number[]): TreeNode | null { }; ``` -## C +### C 递归 ```c @@ -490,7 +490,7 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { } ``` -## Scala +### Scala 递归: @@ -511,7 +511,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -536,3 +536,4 @@ impl Solution { + From 6bf34cd8ba3837d16013dd2ac78b5d632f3f2e22 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:02:05 +0800 Subject: [PATCH 093/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200538.=E5=B0=86?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E7=B4=AF=E5=8A=A0=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\347\264\257\345\212\240\346\240\221.md" | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" index 781763a4a5..c403c98f24 100644 --- "a/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" +++ "b/problems/0538.\346\212\212\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\275\254\346\215\242\344\270\272\347\264\257\345\212\240\346\240\221.md" @@ -44,11 +44,11 @@ * 树中的所有值 互不相同 。 * 给定的树为二叉搜索树。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后再遍历其他节点累加?怎么一想这么麻烦呢。 @@ -64,7 +64,7 @@ 那么知道如何遍历这个二叉树,也就迎刃而解了,**从树中可以看出累加的顺序是右中左,所以我们需要反中序遍历这个二叉树,然后顺序累加就可以了**。 -## 递归 +### 递归 遍历顺序如图所示: @@ -131,7 +131,7 @@ public: }; ``` -## 迭代法 +### 迭代法 迭代法其实就是中序模板题了,在[二叉树:前中后序迭代法](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序统一方式迭代法](https://programmercarl.com/二叉树的统一迭代法.html)可以选一种自己习惯的写法。 @@ -166,17 +166,17 @@ public: }; ``` -# 总结 +## 总结 经历了前面各种二叉树增删改查的洗礼之后,这道题目应该比较简单了。 **好了,二叉树已经接近尾声了,接下来就是要对二叉树来一个大总结了**。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java **递归** ```Java @@ -237,7 +237,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一) ```python # Definition for a binary tree node. @@ -318,7 +318,7 @@ class Solution: self.traversal(root) return root -``` +``` 迭代法(版本二) ```python class Solution: @@ -338,9 +338,9 @@ class Solution: pre = cur.val cur =cur.left return root -``` +``` -## Go +### Go 弄一个sum暂存其和值 ```go @@ -362,7 +362,7 @@ func traversal(cur *TreeNode) { } ``` -## JavaScript +### JavaScript 递归 ```javascript @@ -401,7 +401,7 @@ var convertBST = function (root) { }; ``` -##C +### C 递归 ```c @@ -422,7 +422,7 @@ struct TreeNode* convertBST(struct TreeNode* root){ } ``` -## TypeScript +### TypeScript > 递归法 @@ -462,7 +462,7 @@ function convertBST(root: TreeNode | null): TreeNode | null { }; ``` -## Scala +### Scala ```scala object Solution { @@ -481,7 +481,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -535,3 +535,4 @@ impl Solution { + From 75390603854b899d5a164b0cf508ec393aaa3712 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:04:13 +0800 Subject: [PATCH 094/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...21\346\200\273\347\273\223\347\257\207.md" | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" index 4c742a6bbf..829495438d 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" @@ -92,7 +92,7 @@ * 递归:中序,双指针操作 * 迭代:模拟中序,逻辑相同 * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) - + * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) @@ -154,29 +154,13 @@ 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 - **最后,二叉树系列就这么完美结束了,估计这应该是最长的系列了,感谢大家33天的坚持与陪伴,接下来我们又要开始新的系列了「回溯算法」!** - - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - -

+ From 6e613877f24394d2e4da3f97580d5e75be7d3b83 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 10:54:21 +0800 Subject: [PATCH 095/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\350\256\272\345\237\272\347\241\200.md" | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" index f11dbaef52..dff1823c0c 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -6,13 +6,19 @@ # 回溯算法理论基础 -## 题目分类大纲如下: +## 题目分类 回溯算法大纲 -可以配合我的B站视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) 一起学习! +## 算法公开课 -## 什么是回溯法 + + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/),相信结合视频再看本篇题解,更有助于大家对本题的理解。** + +## 理论基础 + +### 什么是回溯法 回溯法也可以叫做回溯搜索法,它是一种搜索的方式。 @@ -22,7 +28,7 @@ **所以以下讲解中,回溯函数也就是递归函数,指的都是一个函数**。 -## 回溯法的效率 +### 回溯法的效率 回溯法的性能如何呢,这里要和大家说清楚了,**虽然回溯法很难,很不好理解,但是回溯法并不是什么高效的算法**。 @@ -34,7 +40,7 @@ 此时大家应该好奇了,都什么问题,这么牛逼,只能暴力搜索。 -## 回溯法解决的问题 +### 回溯法解决的问题 回溯法,一般可以解决如下几种问题: @@ -55,7 +61,7 @@ 记住组合无序,排列有序,就可以了。 -## 如何理解回溯法 +### 如何理解回溯法 **回溯法解决的问题都可以抽象为树形结构**,是的,我指的是所有回溯法的问题都可以抽象为树形结构! @@ -66,7 +72,7 @@ 这块可能初学者还不太理解,后面的回溯算法解决的所有题目中,我都会强调这一点并画图举相应的例子,现在有一个印象就行。 -## 回溯法模板 +### 回溯法模板 这里给出Carl总结的回溯算法模板。 @@ -173,3 +179,4 @@ void backtracking(参数) { + From 6f3fdf7286c92830e0e9feff8fbdca90442af159 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 10:59:00 +0800 Subject: [PATCH 096/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 444e15ce74..702ffafe52 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -5,10 +5,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - - - # 第77题. 组合 [力扣题目链接](https://leetcode.cn/problems/combinations/ ) @@ -27,13 +23,12 @@ [1,4], ] -# 算法公开课 +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -**《代码随想录》算法视频公开课:[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 - -# 思路 +## 思路 本题是回溯法的经典题目。 @@ -345,12 +340,10 @@ public: - - ## 其他语言版本 -### Java: +### Java ```java class Solution { @@ -451,7 +444,7 @@ func dfs(n int, k int, start int) { } ``` -### javascript +### Javascript 剪枝: From f5e50a919d84cc41b00843a403ad3b566ac521b2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:02:27 +0800 Subject: [PATCH 097/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E7=BB=84=E5=90=88?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\344\274\230\345\214\226.md" | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" index 3926d00694..9577d65f3c 100644 --- "a/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" +++ "b/problems/0077.\347\273\204\345\220\210\344\274\230\345\214\226.md" @@ -7,8 +7,11 @@ # 77.组合优化 +## 算法公开课 -**《代码随想录》算法视频公开课:[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解。** + +## 思路 在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们通过回溯搜索法,解决了n个数中求k个数的组合问题。 @@ -46,7 +49,7 @@ public: }; ``` -# 剪枝优化 +## 剪枝优化 我们说过,回溯法虽然是暴力搜索,但也有时候可以有点剪枝优化一下的。 @@ -135,7 +138,7 @@ public: -# 总结 +## 总结 本篇我们针对求组合问题的回溯法代码做了剪枝优化,这个优化如果不画图的话,其实不好理解,也不好讲清楚。 @@ -143,14 +146,10 @@ public: **就酱,学到了就帮Carl转发一下吧,让更多的同学知道这里!** - - - - ## 其他语言版本 +### Java -Java: ```java class Solution { List> result = new ArrayList<>(); @@ -179,7 +178,8 @@ class Solution { } ``` -Python: +### Python + ```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: @@ -199,7 +199,8 @@ class Solution: ``` -Go: +### Go + ```Go var ( path []int @@ -227,7 +228,7 @@ func dfs(n int, k int, start int) { } ``` -javaScript: +### JavaScript ```js var combine = function(n, k) { @@ -249,7 +250,7 @@ var combine = function(n, k) { }; ``` -TypeScript: +### TypeScript ```typescript function combine(n: number, k: number): number[][] { @@ -270,7 +271,7 @@ function combine(n: number, k: number): number[][] { }; ``` -Rust: +### Rust ```Rust impl Solution { @@ -296,7 +297,7 @@ impl Solution { } ``` -C: +### C ```c int* path; @@ -351,7 +352,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ } ``` -Swift: +### Swift ```swift func combine(_ n: Int, _ k: Int) -> [[Int]] { @@ -381,7 +382,7 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { } ``` -Scala: +### Scala ```scala object Solution { @@ -414,3 +415,4 @@ object Solution { + From f30fd2982b532281d156d7c7cd9a9c7471a0a134 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:08:32 +0800 Subject: [PATCH 098/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200216.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CIII=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0077.\347\273\204\345\220\210.md" | 3 +- ...345\220\210\346\200\273\345\222\214III.md" | 34 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git "a/problems/0077.\347\273\204\345\220\210.md" "b/problems/0077.\347\273\204\345\220\210.md" index 702ffafe52..7d71b51763 100644 --- "a/problems/0077.\347\273\204\345\220\210.md" +++ "b/problems/0077.\347\273\204\345\220\210.md" @@ -103,7 +103,7 @@ for (int i = 1; i <= n; i++) { 在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中我们提到了回溯法三部曲,那么我们按照回溯法三部曲开始正式讲解代码了。 -## 回溯法三部曲 +### 回溯法三部曲 * 递归函数的返回值以及参数 @@ -744,3 +744,4 @@ object Solution { + diff --git "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" index 319b2eba71..4de7dc58f3 100644 --- "a/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" +++ "b/problems/0216.\347\273\204\345\220\210\346\200\273\345\222\214III.md" @@ -7,8 +7,6 @@ - - > 别看本篇选的是组合总和III,而不是组合总和,本题和上一篇77.组合相比难度刚刚好! # 216.组合总和III @@ -30,12 +28,12 @@ 输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 本题就是在[1,2,3,4,5,6,7,8,9]这个集合中找到和为n的k个数的组合。 @@ -54,7 +52,7 @@ 图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。 -## 回溯三部曲 +### 回溯三部曲 * **确定递归函数参数** @@ -165,7 +163,7 @@ public: }; ``` -## 剪枝 +### 剪枝 这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。 @@ -238,7 +236,7 @@ public: * 时间复杂度: O(n * 2^n) * 空间复杂度: O(n) -# 总结 +## 总结 开篇就介绍了本题与[77.组合](https://programmercarl.com/0077.组合.html)的区别,相对来说加了元素总和的限制,如果做完[77.组合](https://programmercarl.com/0077.组合.html)再做本题在合适不过。 @@ -249,10 +247,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java 模板方法 @@ -358,7 +356,7 @@ class Solution { } ``` -## Python +### Python ```py class Solution: @@ -383,7 +381,7 @@ class Solution: ``` -## Go +### Go 回溯+减枝 @@ -418,7 +416,7 @@ func dfs(k, n int, start int, sum int) { } ``` -## javaScript +### JavaScript ```js /** @@ -455,7 +453,7 @@ var combinationSum3 = function(k, n) { }; ``` -## TypeScript +### TypeScript ```typescript function combinationSum3(k: number, n: number): number[][] { @@ -479,7 +477,7 @@ function combinationSum3(k: number, n: number): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -516,7 +514,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -575,7 +573,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){ } ``` -## Swift +### Swift ```swift func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { @@ -607,7 +605,7 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { } ``` -## Scala +### Scala ```scala object Solution { From 87b215dd2e46588f3c97b25ba4cd2e3b15dbff61 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:11:15 +0800 Subject: [PATCH 099/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200017.=E7=94=B5?= =?UTF-8?q?=E8=AF=9D=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84?= =?UTF-8?q?=E5=90=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\257\215\347\273\204\345\220\210.md" | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" index cf5e45207b..b3ba1e5e5c 100644 --- "a/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" +++ "b/problems/0017.\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210.md" @@ -21,12 +21,12 @@ 说明:尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[还得用回溯算法!| LeetCode:17.电话号码的字母组合](https://www.bilibili.com/video/BV1yV4y1V7Ug),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[还得用回溯算法!| LeetCode:17.电话号码的字母组合](https://www.bilibili.com/video/BV1yV4y1V7Ug),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 从示例上来说,输入"23",最直接的想法就是两层for循环遍历了吧,正好把组合的情况都输出了。 @@ -40,7 +40,7 @@ 2. 两个字母就两个for循环,三个字符我就三个for循环,以此类推,然后发现代码根本写不出来 3. 输入1 * #按键等等异常情况 -## 数字和字母如何映射 +### 数字和字母如何映射 可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下: @@ -59,7 +59,7 @@ const string letterMap[10] = { }; ``` -## 回溯法来解决n个for循环的问题 +### 回溯法来解决n个for循环的问题 对于回溯法还不了解的同学看这篇:[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html) @@ -134,9 +134,6 @@ for (int i = 0; i < letters.size(); i++) { **但是要知道会有这些异常,如果是现场面试中,一定要考虑到!** - -## C++代码 - 关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中的回溯法模板,不难写出如下C++代码: @@ -233,7 +230,7 @@ public: 所以大家可以按照版本一来写就可以了。 -# 总结 +## 总结 本篇将题目的三个要点一一列出,并重点强调了和前面讲解过的[77. 组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)的区别,本题是多个集合求组合,所以在回溯的搜索过程中,都有一些细节需要注意的。 @@ -241,10 +238,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -286,7 +283,7 @@ class Solution { } ``` -## Python +### Python 回溯 ```python class Solution: @@ -435,7 +432,7 @@ class Solution: -## Go +### Go 主要在于递归中传递下一个数字 @@ -470,7 +467,7 @@ func dfs(digits string, start int) { } ``` -## javaScript +### JavaScript ```js var letterCombinations = function(digits) { @@ -497,7 +494,7 @@ var letterCombinations = function(digits) { }; ``` -## TypeScript +### TypeScript ```typescript function letterCombinations(digits: string): string[] { @@ -531,7 +528,7 @@ function letterCombinations(digits: string): string[] { }; ``` -## Rust +### Rust ```Rust const map: [&str; 10] = [ @@ -563,7 +560,7 @@ impl Solution { } ``` -## C +### C ```c char* path; @@ -625,7 +622,7 @@ char ** letterCombinations(char * digits, int* returnSize){ } ``` -## Swift +### Swift ```swift func letterCombinations(_ digits: String) -> [String] { @@ -666,7 +663,7 @@ func letterCombinations(_ digits: String) -> [String] { } ``` -## Scala: +### Scala ```scala object Solution { @@ -702,3 +699,4 @@ object Solution { + From 2ed8beef418343adccc7ebc755a8c40159b6fe0d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:15:23 +0800 Subject: [PATCH 100/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\220\210\346\200\273\345\222\214.md" | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" index 4d9466c3c0..2a36ce5adb 100644 --- "a/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" +++ "b/problems/0039.\347\273\204\345\220\210\346\200\273\345\222\214.md" @@ -39,11 +39,11 @@ candidates 中的数字可以无限制重复被选取。 [3,5] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 题目中的**无限制重复被选取,吓得我赶紧想想 出现0 可咋办**,然后看到下面提示:1 <= candidates[i] <= 200,我就放心了。 @@ -57,7 +57,7 @@ candidates 中的数字可以无限制重复被选取。 而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。 -## 回溯三部曲 +### 回溯三部曲 * 递归函数参数 @@ -156,7 +156,7 @@ public: }; ``` -## 剪枝优化 +### 剪枝优化 在这个树形结构中: @@ -217,7 +217,7 @@ public: * 时间复杂度: O(n * 2^n),注意这只是复杂度的上界,因为剪枝的存在,真实的时间复杂度远小于此 * 空间复杂度: O(target) -# 总结 +## 总结 本题和我们之前讲过的[77.组合](https://programmercarl.com/0077.组合.html)、[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)有两点不同: @@ -238,10 +238,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java // 剪枝优化 @@ -271,7 +271,7 @@ class Solution { } ``` -## Python +### Python 回溯(版本一) @@ -370,7 +370,7 @@ class Solution: ``` -## Go +### Go 主要在于递归中传递下一个数字 @@ -404,7 +404,7 @@ func dfs(candidates []int, start int, target int) { } ``` -## JavaScript +### JavaScript ```js var combinationSum = function(candidates, target) { @@ -430,7 +430,7 @@ var combinationSum = function(candidates, target) { }; ``` -## TypeScript +### TypeScript ```typescript function combinationSum(candidates: number[], target: number): number[][] { @@ -456,7 +456,7 @@ function combinationSum(candidates: number[], target: number): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -485,7 +485,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -541,7 +541,7 @@ int** combinationSum(int* candidates, int candidatesSize, int target, int* retur } ``` -## Swift +### Swift ```swift func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { @@ -570,7 +570,7 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { } ``` -## Scala +### Scala ```scala object Solution { @@ -604,3 +604,4 @@ object Solution { + From c3827c43442ad816f4575d7d45958c36dfc92c8a Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 11:17:47 +0800 Subject: [PATCH 101/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\220\210\346\200\273\345\222\214II.md" | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" index 9094020ea9..33e4a46f51 100644 --- "a/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" +++ "b/problems/0040.\347\273\204\345\220\210\346\200\273\345\222\214II.md" @@ -41,13 +41,11 @@ candidates 中的每个数字在每个组合中只能使用一次。 ] ``` -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - -# 思路 +## 思路 这道题目和[39.组合总和](https://programmercarl.com/0039.组合总和.html)如下区别: @@ -86,7 +84,7 @@ candidates 中的每个数字在每个组合中只能使用一次。 可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。 -## 回溯三部曲 +### 回溯三部曲 * **递归函数参数** @@ -217,7 +215,7 @@ public: * 时间复杂度: O(n * 2^n) * 空间复杂度: O(n) -## 补充 +### 补充 这里直接用startIndex来去重也是可以的, 就不用used数组了。 @@ -257,7 +255,7 @@ public: ``` -# 总结 +## 总结 本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。 @@ -265,14 +263,10 @@ public: 所以Carl有必要把去重的这块彻彻底底的给大家讲清楚,**就连“树层去重”和“树枝去重”都是我自创的词汇,希望对大家理解有帮助!** +## 其他语言版本 - - -# 其他语言版本 - - -## Java +### Java **使用标记数组** ```Java class Solution { @@ -355,7 +349,7 @@ class Solution { } ``` -## Python +### Python 回溯 ```python class Solution: @@ -442,7 +436,7 @@ class Solution: self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results) path.pop() ``` -## Go +### Go 主要在于如何在回溯中去重 **使用used数组** @@ -518,7 +512,7 @@ func dfs(candidates []int, start int, target int) { } } ``` -## javaScript +### JavaScript ```js /** @@ -588,7 +582,7 @@ var combinationSum2 = function(candidates, target) { }; ``` -## TypeScript +### TypeScript ```typescript function combinationSum2(candidates: number[], target: number): number[][] { @@ -619,7 +613,7 @@ function combinationSum2(candidates: number[], target: number): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -654,7 +648,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -716,7 +710,7 @@ int** combinationSum2(int* candidates, int candidatesSize, int target, int* retu } ``` -## Swift +### Swift ```swift func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] { @@ -749,7 +743,7 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] { ``` -## Scala +### Scala ```scala object Solution { @@ -784,3 +778,4 @@ object Solution { + From 7bd8751a0cf255e17652065d4b1894344032ceb0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:07:54 +0800 Subject: [PATCH 102/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200131.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E5=9B=9E=E6=96=87=E4=B8=B2=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...62\345\233\236\346\226\207\344\270\262.md" | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" index 92fed58a2e..ca73e9f7ca 100644 --- "a/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" +++ "b/problems/0131.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262.md" @@ -23,12 +23,12 @@ ["a","a","b"] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 本题这涉及到两个关键问题: @@ -58,7 +58,7 @@ 此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。 -## 回溯三部曲 +### 回溯三部曲 * 递归函数参数 @@ -124,7 +124,7 @@ for (int i = startIndex; i < s.size(); i++) { **注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1**。 -## 判断回文子串 +### 判断回文子串 最后我们看一下回文子串要如何判断了,判断一个字符串是否是回文。 @@ -147,8 +147,6 @@ for (int i = startIndex; i < s.size(); i++) { 此时关键代码已经讲解完毕,整体代码如下(详细注释了) -## C++整体代码 - 根据Carl给出的回溯算法模板: ```CPP @@ -212,7 +210,7 @@ public: * 时间复杂度: O(n * 2^n) * 空间复杂度: O(n^2) -# 优化 +## 优化 上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在: @@ -272,7 +270,7 @@ public: ``` -# 总结 +## 总结 这道题目在leetcode上是中等,但可以说是hard的题目了,但是代码其实就是按照模板的样子来的。 @@ -306,10 +304,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { List> lists = new ArrayList<>(); @@ -351,7 +349,7 @@ class Solution { } ``` -## Python +### Python 回溯 基本版 ```python class Solution: @@ -473,7 +471,7 @@ class Solution: return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2)) ``` -## Go +### Go ```go var ( path []string // 放已经回文的子串 @@ -512,7 +510,7 @@ func isPalindrome(s string) bool { } ``` -## javaScript +### JavaScript ```js /** @@ -545,7 +543,7 @@ var partition = function(s) { }; ``` -## TypeScript +### TypeScript ```typescript function partition(s: string): string[][] { @@ -582,7 +580,7 @@ function partition(s: string): string[][] { }; ``` -## C +### C ```c char** path; @@ -679,7 +677,7 @@ char*** partition(char* s, int* returnSize, int** returnColumnSizes){ } ``` -## Swift +### Swift ```swift func partition(_ s: String) -> [[String]] { @@ -719,7 +717,7 @@ func partition(_ s: String) -> [[String]] { } ``` -## Rust +### Rust **回溯+函数判断回文串** ```Rust @@ -808,7 +806,7 @@ impl Solution { ``` -## Scala +### Scala ```scala object Solution { @@ -855,3 +853,4 @@ object Solution { + From 6f711891ba9df855e68147bc0f609753bd705cf7 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:11:47 +0800 Subject: [PATCH 103/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200093.=E5=A4=8D?= =?UTF-8?q?=E5=8E=9FIP=E5=9C=B0=E5=9D=80=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\216\237IP\345\234\260\345\235\200.md" | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" index 55e57dde5f..59cd92da59 100644 --- "a/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" +++ "b/problems/0093.\345\244\215\345\216\237IP\345\234\260\345\235\200.md" @@ -40,16 +40,12 @@ * 0 <= s.length <= 3000 * s 仅由数字组成 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - -# 思路 +## 思路 做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。 @@ -63,7 +59,7 @@ ![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933.png) -## 回溯三部曲 +### 回溯三部曲 * 递归参数 @@ -134,7 +130,7 @@ for (int i = startIndex; i < s.size(); i++) { } ``` -## 判断子串是否合法 +### 判断子串是否合法 最后就是在写一个判断段位是否是有效段位了。 @@ -169,8 +165,6 @@ bool isValid(const string& s, int start, int end) { } ``` -## C++代码 - 根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板: @@ -247,7 +241,7 @@ public: * 时间复杂度: O(3^4),IP地址最多包含4个数字,每个数字最多有3种可能的分割方式,则搜索树的最大深度为4,每个节点最多有3个子节点。 * 空间复杂度: O(n) -# 总结 +## 总结 在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。 @@ -259,9 +253,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## java +### Java ```java class Solution { @@ -402,7 +396,7 @@ class Solution { ``` -## python +### Python 回溯(版本一) ```python @@ -478,9 +472,7 @@ class Solution: ``` - - -## Go +### Go ```go var ( @@ -517,7 +509,7 @@ func dfs(s string, start int) { } ``` -## JavaScript +### JavaScript ```js /** @@ -547,7 +539,7 @@ var restoreIpAddresses = function(s) { }; ``` -## TypeScript +### TypeScript ```typescript function isValidIpSegment(str: string): boolean { @@ -586,7 +578,7 @@ function restoreIpAddresses(s: string): string[] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -643,7 +635,7 @@ impl Solution { } ``` -## C +### C ```c //记录结果 char** result; @@ -719,7 +711,7 @@ char ** restoreIpAddresses(char * s, int* returnSize){ } ``` -## Swift +### Swift ```swift // 判断区间段是否合法 @@ -766,7 +758,7 @@ func restoreIpAddresses(_ s: String) -> [String] { } ``` -## Scala +### Scala ```scala object Solution { @@ -813,3 +805,4 @@ object Solution { + From 92695ceeddbc3b9c39d367bd522a44e1b37502b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:16:33 +0800 Subject: [PATCH 104/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200078.=E5=AD=90?= =?UTF-8?q?=E9=9B=86=E9=97=AE=E9=A2=98=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0078.\345\255\220\351\233\206.md" | 33 ++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 21009f6a36..5f3654deae 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -27,12 +27,12 @@   [] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集](https://www.bilibili.com/video/BV1U84y1q7Ci),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集](https://www.bilibili.com/video/BV1U84y1q7Ci),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 求子集问题和[77.组合](https://programmercarl.com/0077.组合.html)和[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)又不一样了。 @@ -52,7 +52,7 @@ 从图中红线部分,可以看出**遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合**。 -## 回溯三部曲 +### 回溯三部曲 * 递归函数参数 @@ -102,8 +102,6 @@ for (int i = startIndex; i < nums.size(); i++) { } ``` -## C++代码 - 根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板: ``` @@ -158,7 +156,7 @@ public: 并不会,因为每次递归的下一层就是从i+1开始的。 -# 总结 +## 总结 相信大家经过了 * 组合问题: @@ -178,10 +176,10 @@ public: **而组合问题、分割问题是收集树形结构中叶子节点的结果**。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { List> result = new ArrayList<>();// 存放符合条件结果的集合 @@ -205,7 +203,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: def subsets(self, nums): @@ -224,7 +222,7 @@ class Solution: path.pop() ``` -## Go +### Go ```Go var ( path []int @@ -248,7 +246,7 @@ func dfs(nums []int, start int) { } ``` -## Javascript +### Javascript ```Javascript var subsets = function(nums) { @@ -267,7 +265,7 @@ var subsets = function(nums) { }; ``` -## TypeScript +### TypeScript ```typescript function subsets(nums: number[]): number[][] { @@ -287,7 +285,7 @@ function subsets(nums: number[]): number[][] { }; ``` -## Rust +### Rust ```Rust impl Solution { @@ -311,7 +309,7 @@ impl Solution { } ``` -## C +### C ```c int* path; @@ -369,7 +367,7 @@ int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) } ``` -## Swift +### Swift ```swift func subsets(_ nums: [Int]) -> [[Int]] { @@ -392,7 +390,7 @@ func subsets(_ nums: [Int]) -> [[Int]] { } ``` -## Scala +### Scala 思路一: 使用本题解思路 @@ -451,3 +449,4 @@ object Solution { + From cbec2c04ed9c107921834854f41b36dfd9cab785 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:50:02 +0800 Subject: [PATCH 105/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200090.=E5=AD=90?= =?UTF-8?q?=E9=9B=86II=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0090.\345\255\220\351\233\206II.md" | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 3238ee52a2..88c8bdade6 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -3,8 +3,6 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- - # 90.子集II [力扣题目链接](https://leetcode.cn/problems/subsets-ii/) @@ -25,9 +23,9 @@ [] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -240,7 +238,7 @@ class Solution { -#### Python3 +### Python3 回溯 利用used数组去重 ```python @@ -646,3 +644,4 @@ object Solution { + From 322f817666b8bd7c65477a9168bb1db5c4f16410 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:51:29 +0800 Subject: [PATCH 106/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200491.=E9=80=92?= =?UTF-8?q?=E5=A2=9E=E5=AD=90=E5=BA=8F=E5=88=97=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\222\345\242\236\345\255\220\345\272\217\345\210\227.md" | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" index 4b21008ea5..e2011372f8 100644 --- "a/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0491.\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227.md" @@ -23,9 +23,9 @@ * 数组中的整数范围是 [-100,100]。 * 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列](https://www.bilibili.com/video/BV1EG4y1h78v/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列](https://www.bilibili.com/video/BV1EG4y1h78v/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -619,4 +619,3 @@ object Solution { - From f08c25631fc39152caa4d3f78ffb6b0d02c3f2e5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:54:15 +0800 Subject: [PATCH 107/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20046.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0046.\345\205\250\346\216\222\345\210\227.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" index de1af64295..1f5263a79e 100644 --- "a/problems/0046.\345\205\250\346\216\222\345\210\227.md" +++ "b/problems/0046.\345\205\250\346\216\222\345\210\227.md" @@ -24,9 +24,9 @@ ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -491,3 +491,4 @@ object Solution { + From f57b19df94a1ed5122571e4482796a8594053af6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:55:37 +0800 Subject: [PATCH 108/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200047.=E5=85=A8?= =?UTF-8?q?=E6=8E=92=E5=88=97II=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0047.\345\205\250\346\216\222\345\210\227II.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index afede33a46..4fed8a5c8c 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -31,9 +31,9 @@ * 1 <= nums.length <= 8 * -10 <= nums[i] <= 10 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II](https://www.bilibili.com/video/BV1R84y1i7Tm/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II](https://www.bilibili.com/video/BV1R84y1i7Tm/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -58,7 +58,7 @@ 在[46.全排列](https://programmercarl.com/0046.全排列.html)中已经详细讲解了排列问题的写法,在[40.组合总和II](https://programmercarl.com/0040.组合总和II.html) 、[90.子集II](https://programmercarl.com/0090.子集II.html)中详细讲解了去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下: -## C++代码 + ```CPP class Solution { @@ -170,7 +170,7 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) { if (i > 0 && nums[i] == nums[i - 1]) { continue; } -``` +``` 其实并不行,一定要加上 `used[i - 1] == false`或者`used[i - 1] == true`,因为 used[i - 1] 要一直是 true 或者一直是false 才可以,而不是 一会是true 一会又是false。 所以这个条件要写上。 @@ -179,7 +179,7 @@ if (i > 0 && nums[i] == nums[i - 1]) { ## 其他语言版本 -### java +### Java ```java class Solution { @@ -221,7 +221,7 @@ class Solution { } ``` -### python +Python ```python class Solution: @@ -526,3 +526,4 @@ object Solution { + From d9fc7a2d0e76b3439f68a165bcfe98553af4b7dd Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 14:59:16 +0800 Subject: [PATCH 109/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=8E=BB=E9=87=8D=E9=97=AE=E9=A2=98=E7=9A=84?= =?UTF-8?q?=E5=8F=A6=E4=B8=80=E7=A7=8D=E5=86=99=E6=B3=95=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\270\200\347\247\215\345\206\231\346\263\225.md" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" index 4e09b9252a..2be79805af 100644 --- "a/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" +++ "b/problems/\345\233\236\346\272\257\347\256\227\346\263\225\345\216\273\351\207\215\351\227\256\351\242\230\347\232\204\345\217\246\344\270\200\347\247\215\345\206\231\346\263\225.md" @@ -246,8 +246,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 ## 其他语言版本 - -Java: +### Java **47.全排列II** @@ -350,7 +349,7 @@ class Solution { } } ``` -Python: +### Python **90.子集II** @@ -432,7 +431,7 @@ class Solution: ``` -JavaScript: +### JavaScript **90.子集II** @@ -510,7 +509,7 @@ function permuteUnique(nums) { }; ``` -TypeScript: +### TypeScript **90.子集II** @@ -592,7 +591,7 @@ function permuteUnique(nums: number[]): number[][] { }; ``` -Rust: +### Rust **90.子集II**: @@ -713,3 +712,4 @@ impl Solution { + From 42c1bd94ab1bddac9f258730d26275ff6cfeeeec Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:03:21 +0800 Subject: [PATCH 110/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200332.=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...11\346\216\222\350\241\214\347\250\213.md" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index fa6414e9ac..3798b48d87 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -29,9 +29,11 @@ * 输出:["JFK","ATL","JFK","SFO","ATL","SFO"] * 解释:另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"]。但是它自然排序更大更靠后。 -## 思路 +## 算法公开课 + +**如果对回溯算法基础还不了解的话,我还特意录制了一期视频,[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 -**如果对回溯算法基础还不了解的话,我还特意录制了一期视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 +## 思路 这道题目还是很难的,之前我们用回溯法解决了如下问题:[组合问题](https://programmercarl.com/0077.组合.html),[分割问题](https://programmercarl.com/0093.复原IP地址.html),[子集问题](https://programmercarl.com/0078.子集.html),[排列问题](https://programmercarl.com/0046.全排列.html)。 @@ -53,7 +55,7 @@ 针对以上问题我来逐一解答! -## 如何理解死循环 +### 如何理解死循环 对于死循环,我来举一个有重复机场的例子: @@ -61,7 +63,7 @@ 为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。** -## 该记录映射关系 +### 该记录映射关系 有多种解法,字母序靠前排在前面,让很多同学望而退步,如何该记录映射关系呢 ? @@ -90,7 +92,7 @@ unordered_map> targets:unordered_map<出发机场, ma **相当于说我不删,我就做一个标记!** -## 回溯法 +### 回溯法 这道题目我使用回溯法,那么下面按照我总结的回溯模板来: @@ -260,8 +262,8 @@ for (pairtarget : targets[result[result.size() - 1]]) ## 其他语言版本 -### java - +### Java + ```java class Solution { private LinkedList res; @@ -346,7 +348,7 @@ class Solution { } ``` -### python +### Python 回溯 使用used数组 ```python @@ -406,7 +408,7 @@ class Solution: path.pop() # 移除目的地 return False # 没有找到有效行程 -``` +``` 回溯 使用字典 逆序 ```python from collections import defaultdict @@ -430,8 +432,8 @@ class Solution: self.backtracking(next_airport, targets, result) # 递归调用回溯函数进行深度优先搜索 result.append(airport) # 将当前机场添加到行程路径中 ``` - -### GO + +### Go ```go type pair struct { target string @@ -577,7 +579,7 @@ function findItinerary(tickets: string[][]): string[] { }; ``` -### C语言 +### C ```C char **result; @@ -638,7 +640,7 @@ char ** findItinerary(char *** tickets, int ticketsSize, int* ticketsColSize, in return result; } ``` - + ### Swift 直接迭代tickets数组: @@ -791,3 +793,4 @@ impl Solution { + From f58a35db26a0fa4f4b4ae552e3713392b9e1d193 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:04:28 +0800 Subject: [PATCH 111/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8E=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0051.N\347\232\207\345\220\216.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0051.N\347\232\207\345\220\216.md" "b/problems/0051.N\347\232\207\345\220\216.md" index 13cdafb829..6bc4fa78c6 100644 --- "a/problems/0051.N\347\232\207\345\220\216.md" +++ "b/problems/0051.N\347\232\207\345\220\216.md" @@ -28,9 +28,9 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上, * 输入:n = 1 * 输出:[["Q"]] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后](https://www.bilibili.com/video/BV1Rd4y1c7Bq/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后](https://www.bilibili.com/video/BV1Rd4y1c7Bq/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -864,3 +864,4 @@ object Solution { + From dacd87b009f8a106172c4e07853bdf2b3aaf664f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:05:52 +0800 Subject: [PATCH 112/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200051.N=E7=9A=87?= =?UTF-8?q?=E5=90=8EII=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0052.N\347\232\207\345\220\216II.md" | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index 90b920bac4..ac774c83f9 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -43,13 +43,11 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并   ".Q.."] ] -# 思路 +## 思路 详看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别 -# C++代码 - ```CPP class Solution { private: @@ -100,8 +98,9 @@ public: }; ``` -# 其他语言补充 -JavaScript +## 其他语言补充 +### JavaScript + ```javascript var totalNQueens = function(n) { let count = 0; @@ -146,7 +145,7 @@ var totalNQueens = function(n) { }; ``` -TypeScript: +### TypeScript ```typescript // 0-该格为空,1-该格有皇后 @@ -199,7 +198,7 @@ function checkValid(chess: GridStatus[][], i: number, j: number, n: number): boo } ``` -C +### C ```c //path[i]为在i行,path[i]列上存在皇后 @@ -258,7 +257,8 @@ int totalNQueens(int n){ return answer; } ``` -Java +### Java + ```java class Solution { int count = 0; @@ -310,4 +310,3 @@ class Solution { - From 028bfdc662a56e8f0c7ff17bd743a2d21fe0f016 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:11:08 +0800 Subject: [PATCH 113/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200037.=E8=A7=A3?= =?UTF-8?q?=E6=95=B0=E7=8B=AC=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0037.\350\247\243\346\225\260\347\213\254.md" | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" index 6edd3c5b5e..1763063eb7 100644 --- "a/problems/0037.\350\247\243\346\225\260\347\213\254.md" +++ "b/problems/0037.\350\247\243\346\225\260\347\213\254.md" @@ -6,8 +6,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) +> 如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) # 37. 解数独 @@ -35,11 +34,9 @@ * 你可以假设给定的数独只有唯一解。 * 给定数独永远是 9x9 形式的。 -# 算法公开课 - -**《代码随想录》算法视频公开课:[回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独](https://www.bilibili.com/video/BV1TW4y1471V/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独](https://www.bilibili.com/video/BV1TW4y1471V/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -764,3 +761,4 @@ object Solution { + From 93cba02a128b8aa9cd5982d36537a802dda03262 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 24 Jul 2023 15:15:04 +0800 Subject: [PATCH 114/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=9E=E6=BA=AF?= =?UTF-8?q?=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\272\257\346\200\273\347\273\223.md" | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" index 21d78bc211..f2e46829cb 100644 --- "a/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" +++ "b/problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" @@ -8,7 +8,9 @@ > 20张树形结构图、14道精选回溯题目,21篇回溯法精讲文章,由浅入深,一气呵成,这是全网最强回溯算法总结! -# 回溯法理论基础 +# 回溯总结篇 + +## 回溯法理论基础 转眼间[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)里已经分享连续讲解了21天的回溯算法,是时候做一个大总结了,本篇高能,需要花费很大的精力来看! @@ -51,10 +53,10 @@ void backtracking(参数) { **事实证明这个模板会伴随整个回溯法系列!** -# 组合问题 - ## 组合问题 +### 组合问题 + 在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们开始用回溯法解决第一道题目:组合问题。 我在文中开始的时候给大家列举k层for循环例子,进而得出都是同样是暴力解法,为什么要用回溯法! @@ -82,9 +84,9 @@ void backtracking(参数) { **在for循环上做剪枝操作是回溯法剪枝的常见套路!** 后面的题目还会经常用到。 -## 组合总和 +### 组合总和 -### 组合总和(一) +#### 组合总和(一) 在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。 @@ -99,7 +101,7 @@ void backtracking(参数) { 所以剪枝的代码可以在for循环加上 `i <= 9 - (k - path.size()) + 1` 的限制! -### 组合总和(二) +#### 组合总和(二) 在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)中讲解的组合总和问题,和[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html),[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)和区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。 @@ -128,7 +130,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; ![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202115929.png) -### 组合总和(三) +#### 组合总和(三) 在[回溯算法:求组合总和(三)](https://programmercarl.com/0040.组合总和II.html)中集合元素会有重复,但要求解集不能包含重复的组合。 @@ -151,7 +153,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 对于去重,其实排列和子集问题也是一样的道理。 -## 多个集合求组合 +### 多个集合求组合 在[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)中,开始用多个集合来求组合,还是熟悉的模板题目,但是有一些细节。 @@ -167,7 +169,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; 其实本题不算难,但也处处是细节,还是要反复琢磨。 -# 切割问题 +## 切割问题 在[回溯算法:分割回文串](https://programmercarl.com/0131.分割回文串.html)中,我们开始讲解切割问题,虽然最后代码看起来好像是一道模板题,但是从分析到学会套用这个模板,是比较难的。 @@ -192,9 +194,9 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; ![131.分割回文串](https://code-thinking-1253855093.file.myqcloud.com/pics/20201118202448642.png) -# 子集问题 +## 子集问题 -## 子集问题(一) +### 子集问题(一) 在[回溯算法:求子集问题!](https://programmercarl.com/0078.子集.html)中讲解了子集问题,**在树形结构中子集问题是要收集所有节点的结果,而组合问题是收集叶子节点的结果**。 @@ -219,7 +221,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 } ``` -## 子集问题(二) +### 子集问题(二) 在[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)中,开始针对子集问题进行去重。 @@ -229,7 +231,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 ![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111217110449.png) -## 递增子序列 +### 递增子序列 在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨! @@ -247,9 +249,9 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 **相信这个图胜过千言万语的解释了**。 -# 排列问题 +## 排列问题 -## 排列问题(一) +### 排列问题(一) [回溯算法:排列问题!](https://programmercarl.com/0046.全排列.html) 又不一样了。 @@ -266,7 +268,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 * 每层都是从0开始搜索而不是startIndex * 需要used数组记录path里都放了哪些元素了 -## 排列问题(二) +### 排列问题(二) 排列问题也要去重了,在[回溯算法:排列问题(二)](https://programmercarl.com/0047.全排列II.html)中又一次强调了“树层去重”和“树枝去重”。 @@ -290,7 +292,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 本题used数组即是记录path里都放了哪些元素,同时也用来去重,一举两得。 -# 去重问题 +## 去重问题 以上我都是统一使用used数组来去重的,其实使用set也可以用来去重! @@ -310,7 +312,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加 used数组可是全局变量,每层与每层之间公用一个used数组,所以空间复杂度是O(n + n),最终空间复杂度还是O(n)。 -# 重新安排行程(图论额外拓展) +## 重新安排行程(图论额外拓展) 之前说过,有递归的地方就有回溯,深度优先搜索也是用递归来实现的,所以往往伴随着回溯。 @@ -327,9 +329,9 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 本题其实是一道深度优先搜索的题目,但是我完全使用回溯法的思路来讲解这道题题目,**算是给大家拓展一下思维方式,其实深搜和回溯也是分不开的,毕竟最终都是用递归**。 -# 棋盘问题 +## 棋盘问题 -## N皇后问题 +### N皇后问题 在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。 @@ -349,7 +351,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 -## 解数独问题 +### 解数独问题 在[回溯算法:解数独](https://programmercarl.com/0037.解数独.html)中要征服回溯法的最后一道山峰。 @@ -373,7 +375,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 **这样,解数独这么难的问题也被我们攻克了**。 -# 性能分析 +## 性能分析 **关于回溯算法的复杂度分析在网上的资料鱼龙混杂,一些所谓的经典面试书籍不讲回溯算法,算法书籍对这块也避而不谈,感觉就像是算法里模糊的边界**。 @@ -410,7 +412,7 @@ N皇后问题分析: **一般说道回溯算法的复杂度,都说是指数级别的时间复杂度,这也算是一个概括吧!** -# 总结 +## 总结 **[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)历时21天,14道经典题目分析,20张树形图,21篇回溯法精讲文章,从组合到切割,从子集到排列,从棋盘问题到最后的复杂度分析**,至此收尾了。 @@ -449,3 +451,4 @@ N皇后问题分析: + From 29c31ca76f32dfc8fc2ba7a593d4a82547ce55d7 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:46:09 +0800 Subject: [PATCH 115/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20455.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E9=A5=BC=E5=B9=B2=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0052.N\347\232\207\345\220\216II.md" | 1 + .../0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0052.N\347\232\207\345\220\216II.md" "b/problems/0052.N\347\232\207\345\220\216II.md" index ac774c83f9..29c2b58818 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -310,3 +310,4 @@ class Solution { + diff --git "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" index ce1987ef1c..c9c1a8525d 100644 --- "a/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" +++ "b/problems/0455.\345\210\206\345\217\221\351\245\274\345\271\262.md" @@ -30,9 +30,9 @@ - 0 <= s.length <= 3 \* 10^4 - 1 <= g[i], s[j] <= 2^31 - 1 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From e713f9c90aee716d53a05b1cb6fb35a802f2d8fc Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:48:15 +0800 Subject: [PATCH 116/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\206\345\212\250\345\272\217\345\210\227.md" | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" index 08de23ae24..943dfe39b6 100644 --- "a/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" +++ "b/problems/0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" @@ -33,11 +33,13 @@ - 输入: [1,2,3,4,5,6,7,8,9] - 输出: 2 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -## 思路 1(贪心解法) +## 思路 + +### 思路 1(贪心解法) 本题要求通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。 @@ -69,7 +71,7 @@ 2. 情况二:数组首尾两端 3. 情况三:单调坡中有平坡 -### 情况一:上下坡中有平坡 +#### 情况一:上下坡中有平坡 例如 [1,2,2,2,1]这样的数组,如图: @@ -87,7 +89,7 @@ 所以我们记录峰值的条件应该是: `(preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)`,为什么这里允许 prediff == 0 ,就是为了 上面我说的这种情况。 -### 情况二:数组首尾两端 +#### 情况二:数组首尾两端 所以本题统计峰值的时候,数组最左面和最右面如何统计呢? @@ -142,7 +144,7 @@ public: 所以此时我们要讨论情况三! -### 情况三:单调坡度有平坡 +#### 情况三:单调坡度有平坡 在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图: @@ -187,7 +189,7 @@ public: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230108174452.png) -## 思路 2(动态规划) +### 思路 2(动态规划) 考虑用动态规划的思想来解决这个问题。 @@ -696,4 +698,3 @@ object Solution { - From fcd25819e9bb9b6bd8b44ea4664a2f9ac13ee37f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:49:56 +0800 Subject: [PATCH 117/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20053.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=92=8C=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...201\350\247\204\345\210\222\357\274\211.md" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 6f3b368676..f1b6470985 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -17,7 +17,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -97,8 +97,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java /** * 1.dp[i]代表当前下标对应的最大值 @@ -140,7 +140,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def maxSubArray(self, nums: List[int]) -> int: @@ -153,7 +154,8 @@ class Solution: return result ``` -Go: +### Go: + ```Go // solution // 1, dp @@ -184,7 +186,7 @@ func max(a,b int) int{ } ``` -JavaScript: +### JavaScript: ```javascript const maxSubArray = nums => { @@ -203,8 +205,7 @@ const maxSubArray = nums => { }; ``` - -Scala: +### Scala: ```scala object Solution { @@ -221,7 +222,7 @@ object Solution { } ``` -TypeScript: +### TypeScript: ```typescript function maxSubArray(nums: number[]): number { @@ -244,3 +245,4 @@ function maxSubArray(nums: number[]): number { + From 7da76551ba655f419020fd596569c47b02bf53b4 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:53:02 +0800 Subject: [PATCH 118/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\234\200\344\275\263\346\227\266\346\234\272II.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" index 89c654fad1..2c2ab225fd 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II.md" @@ -37,9 +37,9 @@ - 1 <= prices.length <= 3 \* 10 ^ 4 - 0 <= prices[i] <= 10 ^ 4 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -316,7 +316,7 @@ function maxProfit(prices: number[]): number { } ``` -### Rust +### Rust: 贪心: @@ -389,7 +389,7 @@ int maxProfit(int* prices, int pricesSize){ } ``` -### Scala +### Scala: 贪心: @@ -411,3 +411,4 @@ object Solution { + From 336884055c722c3c3e6ce43960d7eae176174087 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:55:04 +0800 Subject: [PATCH 119/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200055.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8F=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" index e54c20342b..bedb09abee 100644 --- "a/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" +++ "b/problems/0055.\350\267\263\350\267\203\346\270\270\346\210\217.md" @@ -26,9 +26,9 @@ - 输出: false - 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From a04f7f54746d70794d976c616ed630bbad6070b6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 14:56:46 +0800 Subject: [PATCH 120/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\267\263\350\267\203\346\270\270\346\210\217II.md" | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" index 2f0349b2cc..02c8e4862e 100644 --- "a/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" +++ "b/problems/0045.\350\267\263\350\267\203\346\270\270\346\210\217II.md" @@ -25,9 +25,9 @@ 说明: 假设你总是可以到达数组的最后一个位置。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -53,7 +53,7 @@ **图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)** -## 方法一 +### 方法一 从图中可以看出来,就是移动下标达到了当前覆盖的最远距离下标时,步数就要加一,来增加覆盖距离。最后的步数就是最少步数。 @@ -90,7 +90,7 @@ public: * 空间复杂度: O(1) -## 方法二 +### 方法二 依然是贪心,思路和方法一差不多,代码可以简洁一些。 @@ -469,3 +469,4 @@ impl Solution { + From 9b3d4ac2454afb393931788ab3c8b4822189eef5 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:01:20 +0800 Subject: [PATCH 121/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201005.K=E6=AC=A1?= =?UTF-8?q?=E5=8F=96=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\226\347\232\204\346\225\260\347\273\204\345\222\214.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" index 4cf69d6f41..bed11c7a19 100644 --- "a/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" +++ "b/problems/1005.K\346\254\241\345\217\226\345\217\215\345\220\216\346\234\200\345\244\247\345\214\226\347\232\204\346\225\260\347\273\204\345\222\214.md" @@ -34,9 +34,9 @@ * 1 <= K <= 10000 * -100 <= A[i] <= 100 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 5b4a2e245b16fe3b6877f5d5c5a53f40be1048fd Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:10:42 +0800 Subject: [PATCH 122/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200134.=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0134.\345\212\240\346\262\271\347\253\231.md" | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" index ad9acfbc80..2f9539e8a4 100644 --- "a/problems/0134.\345\212\240\346\262\271\347\253\231.md" +++ "b/problems/0134.\345\212\240\346\262\271\347\253\231.md" @@ -45,12 +45,14 @@ * 解释: 你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +## 思路 -## 暴力方法 + +### 暴力方法 暴力的方法很明显就是O(n^2)的,遍历每一个加油站为起点的情况,模拟一圈。 @@ -85,7 +87,7 @@ public: * 空间复杂度:O(1) -## 贪心算法(方法一) +### 贪心算法(方法一) 直接从全局进行贪心选择,情况如下: @@ -134,7 +136,7 @@ public: 但不管怎么说,解法毕竟还是巧妙的,不用过于执着于其名字称呼。 -## 贪心算法(方法二) +### 贪心算法(方法二) 可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量rest[i]相加一定是大于等于零的。 @@ -633,3 +635,4 @@ object Solution { + From 1cdbefd8ef2a8803136b4e038889e7f91df6936c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:11:55 +0800 Subject: [PATCH 123/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200135.=E5=88=86?= =?UTF-8?q?=E5=8F=91=E7=B3=96=E6=9E=9C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.\345\210\206\345\217\221\347\263\226\346\236\234.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" index cf3ccc8e90..d130bd6820 100644 --- "a/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" +++ "b/problems/0135.\345\210\206\345\217\221\347\263\226\346\236\234.md" @@ -28,9 +28,9 @@ * 输出: 4 * 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -234,7 +234,7 @@ func findMax(num1 int, num2 int) int { } ``` -### Javascript: +### Javascript ```Javascript var candy = function(ratings) { let candys = new Array(ratings.length).fill(1) @@ -376,3 +376,4 @@ object Solution { + From 4f85769d51e99718f3c8ea53b1eafdfc3f218771 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:15:18 +0800 Subject: [PATCH 124/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200860.=E6=9F=A0?= =?UTF-8?q?=E6=AA=AC=E6=B0=B4=E6=89=BE=E9=9B=B6=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\240\346\252\254\346\260\264\346\211\276\351\233\266.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" index 5046df121a..50a0c31a50 100644 --- "a/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" +++ "b/problems/0860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.md" @@ -50,9 +50,9 @@ * 0 <= bills.length <= 10000 * bills[i] 不是 5 就是 10 或是 20  -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -403,3 +403,4 @@ object Solution { + From 1500bb9b3e289eb3d4a693b2efb9aa7f98ff2eb1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:17:26 +0800 Subject: [PATCH 125/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200406.=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\230\351\207\215\345\273\272\351\230\237\345\210\227.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" index 60d1fcab03..9cd78fac62 100644 --- "a/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" +++ "b/problems/0406.\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227.md" @@ -37,9 +37,9 @@ 题目数据确保队列可以被重建 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -402,3 +402,4 @@ object Solution { + From d31e23f5f629564076e184969c093cec7c273dcd Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:20:20 +0800 Subject: [PATCH 126/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E8=BA=AB=E9=AB=98=E9=87=8D=E5=BB=BA=E9=98=9F=E5=88=97=EF=BC=88?= =?UTF-8?q?vector=E5=8E=9F=E7=90=86=E8=AE=B2=E8=A7=A3)=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\206\350\256\262\350\247\243\357\274\211.md" | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index 4f8cab8288..fdb4f58ba4 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -165,19 +165,9 @@ public: 相信在这里学习算法的录友们,都是想在软件行业长远发展的,都是要从事编程的工作,那么一定要深耕好一门编程语言,这个非常重要! - - - - ## 其他语言版本 - -Java: - - -Python: - -Rust: +### Rust ```rust // 版本二,使用list(链表) @@ -206,8 +196,7 @@ impl Solution{ } ``` - -Go: +### Go: Go中slice的`append`操作和C++中vector的扩容机制基本相同。 @@ -224,3 +213,4 @@ Go中slice的`append`操作和C++中vector的扩容机制基本相同。 + From eb320010a411c9420d83606314b0f4909f080d63 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:22:08 +0800 Subject: [PATCH 127/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200452.=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?= =?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\255\345\274\225\347\210\206\346\260\224\347\220\203.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" index ff476f4152..90cd7085d1 100644 --- "a/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" +++ "b/problems/0452.\347\224\250\346\234\200\345\260\221\346\225\260\351\207\217\347\232\204\347\256\255\345\274\225\347\210\206\346\260\224\347\220\203.md" @@ -42,9 +42,9 @@ * points[i].length == 2 * -2^31 <= xstart < xend <= 2^31 - 1 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 From 2acb91c29165ee618334689298681b607348d2df Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:23:46 +0800 Subject: [PATCH 128/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200435.=E6=97=A0?= =?UTF-8?q?=E9=87=8D=E5=8F=A0=E5=8C=BA=E9=97=B4=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\207\215\345\217\240\345\214\272\351\227\264.md" | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" index 61f42d4241..c307532e4f 100644 --- "a/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" +++ "b/problems/0435.\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264.md" @@ -30,9 +30,9 @@ * 输出: 0 * 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,依然是判断重叠区间 | LeetCode:435.无重叠区间](https://www.bilibili.com/video/BV1A14y1c7E1),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -89,9 +89,9 @@ public: 大家此时会发现如此复杂的一个问题,代码实现却这么简单! +## 补充 - -## 补充(1) +### 补充(1) 左边界排序可不可以呢? @@ -144,7 +144,7 @@ public: ``` -## 补充(2) +### 补充(2) 本题其实和[452.用最少数量的箭引爆气球](https://programmercarl.com/0452.用最少数量的箭引爆气球.html)非常像,弓箭的数量就相当于是非交叉区间的数量,只要把弓箭那道题目代码里射爆气球的判断条件加个等号(认为[0,1][1,2]不是相邻区间),然后用总区间数减去弓箭数量 就是要移除的区间数量了。 @@ -311,7 +311,7 @@ func min(a, b int) int { } ``` -### Javascript: +### Javascript - 按右边界排序 ```Javascript var eraseOverlapIntervals = function(intervals) { @@ -447,3 +447,4 @@ impl Solution { + From 481cd1d137ce6497f874c07af8e8c3d007a40bf7 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:25:25 +0800 Subject: [PATCH 129/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200763.=E5=88=92?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AF=8D=E5=8C=BA=E9=97=B4=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\206\345\255\227\346\257\215\345\214\272\351\227\264.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" index 158f76d625..41314456c3 100644 --- "a/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" +++ "b/problems/0763.\345\210\222\345\210\206\345\255\227\346\257\215\345\214\272\351\227\264.md" @@ -24,9 +24,9 @@ * S的长度在[1, 500]之间。 * S只包含小写字母 'a' 到 'z' 。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -409,3 +409,4 @@ impl Solution { + From bde75d3a98b857f533773609d825b6247c6dfcb6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:26:26 +0800 Subject: [PATCH 130/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200056.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8C=BA=E9=97=B4=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" index 8705f840e2..95781b1a1d 100644 --- "a/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" +++ "b/problems/0056.\345\220\210\345\271\266\345\214\272\351\227\264.md" @@ -22,9 +22,9 @@ * 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 * 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -341,3 +341,4 @@ impl Solution { + From 3bbabf40845d916ddecaf7b9e1af60d8198bbc1b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:28:04 +0800 Subject: [PATCH 131/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200738.=E5=8D=95?= =?UTF-8?q?=E8=B0=83=E9=80=92=E5=A2=9E=E7=9A=84=E6=95=B0=E5=AD=97=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\236\347\232\204\346\225\260\345\255\227.md" | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" index d88ebbb096..c2215cf633 100644 --- "a/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/0738.\345\215\225\350\260\203\351\200\222\345\242\236\347\232\204\346\225\260\345\255\227.md" @@ -26,12 +26,14 @@ 说明: N 是在 [0, 10^9] 范围内的一个整数。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +## 思路 -## 暴力解法 + +### 暴力解法 题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时! @@ -62,7 +64,7 @@ public: * 时间复杂度:O(n × m) m为n的数字长度 * 空间复杂度:O(1) -## 贪心算法 +### 贪心算法 题目要求小于等于N的最大单调递增的整数,那么拿一个两位的数字来举例。 @@ -120,7 +122,7 @@ public: ## 其他语言版本 -### Java: +### Java ```java 版本1 class Solution { @@ -163,7 +165,7 @@ class Solution { ``` -### Python: +### Python 暴力 ```python class Solution: @@ -395,3 +397,4 @@ impl Solution { + From 9a8f9780f63e01a8a64edc9c74a9833994d2662a Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:29:35 +0800 Subject: [PATCH 132/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200968.=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\221\346\216\247\344\272\214\345\217\211\346\240\221.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" index e2ba8ebffc..be04bd4755 100644 --- "a/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0968.\347\233\221\346\216\247\344\272\214\345\217\211\346\240\221.md" @@ -38,9 +38,9 @@ * 给定树的节点数的范围是 [1, 1000]。 * 每个节点的值都是 0。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -732,3 +732,4 @@ impl Solution { + From f4a74a43d69c75f3e898c1900a989b8651e2456c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Tue, 25 Jul 2023 15:31:11 +0800 Subject: [PATCH 133/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=B4=AA=E5=BF=83?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" index d52e85513c..c375503d71 100644 --- "a/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\264\252\345\277\203\347\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" @@ -154,3 +154,4 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 + From 0ffbe67f5a3e8e3108e95a526f3e142faac13477 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 25 Jul 2023 16:24:55 +0800 Subject: [PATCH 134/161] =?UTF-8?q?=E6=B7=BB=E5=8A=A059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index f03fcdad35..73e9e4daea 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -688,6 +688,58 @@ public class Solution { } ``` +### Ruby#: +```ruby +def generate_matrix(n) + result = Array.new(n) { Array.new(n, 0) } + #循环次数 + loop_times = 0 + #步长 + step = n - 1 + val = 1 + + + while loop_times < n / 2 + #模拟从左向右 + for i in 0..step - 1 + #行数不变,列数变 + result[loop_times][i+loop_times] = val + val += 1 + end + + #模拟从上到下 + for i in 0..step - 1 + #列数不变,行数变 + result[i+loop_times][n-loop_times-1] = val + val += 1 + end + + #模拟从右到左 + for i in 0..step - 1 + #行数不变,列数变 + result[n-loop_times-1][n-loop_times-i-1] = val + val += 1 + end + + #模拟从下到上 + for i in 0..step - 1 + #列数不变,行数变 + result[n-loop_times-i-1][loop_times] = val + val += 1 + end + + loop_times += 1 + step -= 2 + end + + #如果是奇数,则填充最后一个元素 + result[n/2][n/2] = n**2 if n % 2 + + return result + +end +``` +

From 6ed304e9cdb3f8bd3ef74220190f808ffbcab79d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:51:45 +0800 Subject: [PATCH 135/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" index a99c069002..bff26d1d34 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\347\220\206\350\256\272\345\237\272\347\241\200.md" @@ -12,7 +12,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 什么是动态规划 @@ -135,3 +135,4 @@ + From b295b12d1abdacd0e3ba09d6a9395d494ede0b42 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:53:34 +0800 Subject: [PATCH 136/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200509.=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...220\346\263\242\351\202\243\345\245\221\346\225\260.md" | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 7ace072334..0c073db5bb 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -32,9 +32,9 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1 * 0 <= n <= 30 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -278,7 +278,7 @@ class Solution: return self.fib(n - 1) + self.fib(n - 2) ``` -### Go: +### Go ```Go func fib(n int) int { if n < 2 { @@ -479,3 +479,4 @@ public class Solution + From 9b24f3cdefe2ee2ca10eb97c2cef4224cd864622 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:56:48 +0800 Subject: [PATCH 137/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0070.\347\210\254\346\245\274\346\242\257.md" | 5 +++-- ...203\214\345\214\205\347\211\210\346\234\254.md" | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" index 1b24e49150..1a1f7e31b5 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257.md" @@ -29,9 +29,9 @@ * 1 阶 + 2 阶 * 2 阶 + 1 阶 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -522,3 +522,4 @@ impl Solution { + diff --git "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" index 8c85985fba..4ca7a3710b 100644 --- "a/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" +++ "b/problems/0070.\347\210\254\346\245\274\346\242\257\345\256\214\345\205\250\350\203\214\345\214\205\347\211\210\346\234\254.md" @@ -127,8 +127,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int climbStairs(int n) { @@ -148,7 +148,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -166,8 +166,8 @@ class Solution: return dp[n] ``` +### Go: -Go: ```go func climbStairs(n int) int { //定义 @@ -189,7 +189,8 @@ func climbStairs(n int) int { } ``` -JavaScript: +### JavaScript: + ```javascript var climbStairs = function(n) { const dp = new Array(n + 1).fill(0); @@ -206,7 +207,7 @@ var climbStairs = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript function climbStairs(n: number): number { @@ -226,7 +227,7 @@ function climbStairs(n: number): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -250,4 +251,3 @@ impl Solution { - From 86f794b11814568adac618f462076ad6b0d8edde Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 14:58:05 +0800 Subject: [PATCH 138/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2\261\350\264\271\347\210\254\346\245\274\346\242\257.md" | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" index 9eaaa4e9fe..6fb518c371 100644 --- "a/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" +++ "b/problems/0746.\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257.md" @@ -37,9 +37,9 @@ * cost[i] 将会是一个整型数据,范围为 [0, 999] 。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ----------- @@ -523,3 +523,4 @@ public class Solution + From 0e3500ed1c7b0d6db94d33b6d5db2a39424ac94b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:01:47 +0800 Subject: [PATCH 139/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=20=E5=91=A8=E6=9C=AB=E6=80=BB=E7=BB=93=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + ...0\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" | 1 + 4 files changed, 4 insertions(+) diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index b4baa4adb0..831ce348a1 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210107\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列一) 这周我们正式开始动态规划的学习! diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index a77efa2fd2..71cb49a9db 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210114\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列二) ## 周一 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index 8fd292ed9a..e74dd6bea8 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210225\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列六) 本周我们主要讲解了打家劫舍系列,这个系列也是dp解决的经典问题,那么来看看我们收获了哪些呢,一起来回顾一下吧。 diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" index b814e1b39e..ec442a3979 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20210304\345\212\250\350\247\204\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -1,3 +1,4 @@ +# 本周小结!(动态规划系列七) 本周的主题就是股票系列,来一起回顾一下吧 From 126aaac841078b436f9d0e878a1bef1c0f8aeee1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:06:55 +0800 Subject: [PATCH 140/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\241\20001\350\203\214\345\214\205-1.md" | 22 +++++++++---------- ...47\241\20001\350\203\214\345\214\205-2.md" | 14 +++++++----- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" index bd3191dca0..7511ac8715 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-1.md" @@ -8,8 +8,11 @@ # 动态规划:01背包理论基础 +## 算法公开课 -**《代码随想录》算法视频公开课:[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 这周我们正式开始讲解背包问题! @@ -37,7 +40,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 之前可能有些录友已经可以熟练写出背包了,但只要把这个文章仔细看完,相信你会意外收获! -## 01 背包 +### 01 背包 有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 @@ -67,7 +70,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 以下讲解和图示中出现的数字都是以这个例子为例。 -## 二维dp数组01背包 +### 二维dp数组01背包 依然动规五部曲分析一波。 @@ -226,9 +229,6 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。 - -## 完整c++测试代码 - ```cpp void test_2_wei_bag_problem1() { vector weight = {1, 3, 4}; @@ -275,7 +275,7 @@ int main() { ## 其他语言版本 -### java +### Java ```java public class BagProblem { @@ -396,7 +396,8 @@ public class BagProblem { ``` -### python +### Python + 无参数版 ```python def test_2_wei_bag_problem1(): @@ -456,8 +457,7 @@ if __name__ == "__main__": ``` - -### go +### Go ```go func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { @@ -498,7 +498,7 @@ func main() { } ``` -### javascript +### Javascript ```js function testWeightBagProblem (weight, value, size) { diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index d481e044c5..019947e536 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -3,10 +3,13 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- # 动态规划:01背包理论基础(滚动数组) -**《代码随想录》算法视频公开课:[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 昨天[动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html)中是用二维dp数组来讲解01背包。 @@ -29,7 +32,7 @@ 问背包能背的物品最大价值是多少? -## 一维dp数组(滚动数组) +### 一维dp数组(滚动数组) 对于背包问题其实状态都是可以压缩的。 @@ -154,8 +157,6 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 -## 一维dp01背包完整C++测试代码 - ```CPP void test_1_wei_bag_problem() { vector weight = {1, 3, 4}; @@ -318,7 +319,7 @@ func main() { } ``` -### javaScript +### JavaScript ```js @@ -459,3 +460,4 @@ fn test_wei_bag_problem2() { + From fc19feb04939aee16372c6a6880a4a0ee3373110 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:13:26 +0800 Subject: [PATCH 141/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200416.=E5=88=86?= =?UTF-8?q?=E5=89=B2=E7=AD=89=E5=92=8C=E8=87=AA=E5=B7=B1=200474.=E4=B8=80?= =?UTF-8?q?=E5=92=8C=E9=9B=B6=200494.=E7=9B=AE=E6=A0=87=E5=92=8C=201049.?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E9=87=8FII=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\255\211\345\222\214\345\255\220\351\233\206.md" | 13 +++++++------ .../0474.\344\270\200\345\222\214\351\233\266.md" | 5 +++-- .../0494.\347\233\256\346\240\207\345\222\214.md" | 11 +++++------ ...44\264\347\232\204\351\207\215\351\207\217II.md" | 13 +++++++------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index a886b99a6d..0657c010bb 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -30,9 +30,9 @@ * 1 <= nums.length <= 200 * 1 <= nums[i] <= 100 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之背包问题,这个包能装满吗?| LeetCode:416.分割等和子集](https://www.bilibili.com/video/BV1rt4y1N7jE/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -53,7 +53,7 @@ * [动态规划:关于01背包问题,你该了解这些!](https://programmercarl.com/背包理论基础01背包-1.html) * [动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html) -## 01背包问题 +### 01背包问题 背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。 @@ -479,7 +479,7 @@ func canPartition(nums []int) bool { } ``` -### javaScript: +### JavaScript: ```js var canPartition = function(nums) { @@ -499,7 +499,7 @@ var canPartition = function(nums) { ``` -### Rust +### Rust: ```Rust impl Solution { @@ -681,7 +681,7 @@ function canPartition(nums: number[]): boolean { }; ``` -### Scala +### Scala: 滚动数组: ```scala @@ -730,3 +730,4 @@ object Solution { + diff --git "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" index 7c1206ef35..8f6197ac5c 100644 --- "a/problems/0474.\344\270\200\345\222\214\351\233\266.md" +++ "b/problems/0474.\344\270\200\345\222\214\351\233\266.md" @@ -34,9 +34,9 @@ * strs[i] 仅由 '0' 和 '1' 组成 * 1 <= m, n <= 100 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -538,3 +538,4 @@ impl Solution { + diff --git "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" index 1902d5ed44..4a4e966c14 100644 --- "a/problems/0494.\347\233\256\346\240\207\345\222\214.md" +++ "b/problems/0494.\347\233\256\346\240\207\345\222\214.md" @@ -37,9 +37,9 @@ * 初始的数组的和不会超过 1000 。 * 保证返回的最终结果能被 32 位整数存下。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -67,7 +67,7 @@ target是固定的,sum是固定的,left就可以求出来。 此时问题就是在集合nums中找出和为left的组合。 -## 回溯算法 +### 回溯算法 在回溯算法系列中,一起学过这道题目[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)的录友应该感觉很熟悉,这不就是组合总和问题么? @@ -118,7 +118,7 @@ public: 也可以使用记忆化回溯,但这里我就不在回溯上下功夫了,直接看动规吧 -## 动态规划 +### 动态规划 如何转化为01背包问题呢。 @@ -519,8 +519,6 @@ const findTargetSumWays = (nums, target) => { ### TypeScript -TypeScript: - ```ts function findTargetSumWays(nums: number[], target: number): number { // 把数组分成两个组合left, right.left + right = sum, left - right = target. @@ -590,3 +588,4 @@ impl Solution { + diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index 932029ab7a..cc66131734 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -35,9 +35,9 @@ * 1 <= stones.length <= 30 * 1 <= stones[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -341,7 +341,7 @@ func max(a, b int) int { } ``` -### JavaScript +### JavaScript: ```javascript /** @@ -364,7 +364,7 @@ var lastStoneWeightII = function (stones) { }; ``` -### C +### C: ```c #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -413,7 +413,7 @@ function lastStoneWeightII(stones: number[]): number { }; ``` -### Scala +### Scala: 滚动数组: ```scala @@ -455,7 +455,7 @@ object Solution { } ``` -### Rust +### Rust: ```rust impl Solution { @@ -477,3 +477,4 @@ impl Solution { + From 7ac217942fea957c2d8cfb579e81d80bbd590499 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:28:27 +0800 Subject: [PATCH 142/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=200139.?= =?UTF-8?q?=E5=8D=95=E8=AF=8D=E6=8B=86=E5=88=86=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2=200377.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CIV=200518.?= =?UTF-8?q?=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2II=20=E5=A4=9A=E9=87=8D?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\350\257\215\346\213\206\345\210\206.md" | 23 ++++++++------- ...50\345\271\263\346\226\271\346\225\260.md" | 21 +++++++------- ...66\351\222\261\345\205\221\346\215\242.md" | 20 +++++++------ ...10\346\200\273\345\222\214\342\205\243.md" | 20 +++++++------ ...\351\222\261\345\205\221\346\215\242II.md" | 22 +++++++------- ...05\346\200\273\347\273\223\347\257\207.md" | 1 + ...32\351\207\215\350\203\214\345\214\205.md" | 10 +++---- ...14\345\205\250\350\203\214\345\214\205.md" | 29 ++++++++++--------- 8 files changed, 80 insertions(+), 66 deletions(-) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index 0d88ba3681..d93288ae5a 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -33,9 +33,9 @@ * 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] * 输出: false -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -123,7 +123,7 @@ public: **这个代码就可以AC了,当然回溯算法不是本题的主菜,背包才是!** -## 背包问题 +### 背包问题 单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。 @@ -239,7 +239,7 @@ public: } }; -``` +``` 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: @@ -259,8 +259,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean wordBreak(String s, List wordDict) { @@ -335,7 +335,7 @@ class Solution { } ``` -Python: +### Python: 回溯 ```python @@ -397,7 +397,8 @@ class Solution: -Go: +### Go: + ```Go func wordBreak(s string,wordDict []string) bool { wordDictSet := make(map[string]bool) @@ -433,7 +434,8 @@ func wordBreak(s string, wordDict []string) bool { } ``` -Javascript: +### JavaScript: + ```javascript const wordBreak = (s, wordDict) => { @@ -454,7 +456,7 @@ const wordBreak = (s, wordDict) => { } ``` -TypeScript: +### TypeScript: > 动态规划 @@ -496,7 +498,7 @@ function wordBreak(s: string, wordDict: string[]): boolean { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -519,3 +521,4 @@ impl Solution { + diff --git "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" index 70ab8649fd..a0c138ee80 100644 --- "a/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" +++ "b/problems/0279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260.md" @@ -28,9 +28,9 @@ 提示: * 1 <= n <= 10^4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -106,8 +106,6 @@ dp[5] = min(dp[4] + 1, dp[1] + 1) = 2 最后的dp[n]为最终结果。 -## C++代码 - 以上动规五部曲分析完毕C++代码如下: ```CPP @@ -165,8 +163,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { // 版本一,先遍历物品, 再遍历背包 @@ -219,7 +217,7 @@ class Solution { } ``` -Python: +### Python: 先遍历物品, 再遍历背包 ```python @@ -276,7 +274,8 @@ class Solution: ``` -Go: +### Go: + ```go // 版本一,先遍历物品, 再遍历背包 func numSquares1(n int) int { @@ -327,7 +326,8 @@ func min(a, b int) int { } ``` -Javascript: +### Javascript: + ```Javascript // 先遍历物品,再遍历背包 var numSquares1 = function(n) { @@ -357,7 +357,7 @@ var numSquares2 = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript // 先遍历物品 @@ -389,7 +389,7 @@ function numSquares(n: number): number { }; ``` -Rust: +### Rust: ```rust // 先遍历背包 @@ -439,3 +439,4 @@ impl Solution { + diff --git "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" index 1f3f4df27f..f32fd13e23 100644 --- "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" +++ "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" @@ -39,9 +39,9 @@ * 1 <= coins[i] <= 2^31 - 1 * 0 <= amount <= 10^4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -110,7 +110,6 @@ dp[0] = 0; dp[amount]为最终结果。 -## C++代码 以上分析完毕,C++ 代码如下: ```CPP @@ -187,8 +186,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int coinChange(int[] coins, int amount) { @@ -215,7 +214,7 @@ class Solution { } ``` -Python: +### Python: 先遍历物品 后遍历背包 @@ -288,7 +287,8 @@ class Solution: ``` -Go: +### Go: + ```go // 版本一, 先遍历物品,再遍历背包 func coinChange1(coins []int, amount int) int { @@ -352,7 +352,7 @@ func min(a, b int) int { ``` -Rust: +### Rust: ```rust // 遍历物品 @@ -398,7 +398,8 @@ impl Solution { } ``` -Javascript: +### Javascript: + ```javascript // 遍历物品 const coinChange = (coins, amount) => { @@ -435,7 +436,7 @@ var coinChange = function(coins, amount) { } ``` -TypeScript: +### TypeScript: ```typescript // 遍历物品 @@ -473,3 +474,4 @@ function coinChange(coins: number[], amount: number): number { + diff --git "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" index bd43d52657..d9699c5407 100644 --- "a/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" +++ "b/problems/0377.\347\273\204\345\220\210\346\200\273\345\222\214\342\205\243.md" @@ -31,9 +31,9 @@ 因此输出为 7。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -154,8 +154,7 @@ C++测试用例有两个数相加超过int的数据,所以需要在if里加上 ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -174,7 +173,7 @@ class Solution { } ``` -Python: +### Python: 卡哥版 @@ -207,7 +206,8 @@ class Solution: ``` -Go: +### Go: + ```go func combinationSum4(nums []int, target int) int { //定义dp数组 @@ -226,7 +226,8 @@ func combinationSum4(nums []int, target int) int { } ``` -Javascript: +### Javascript: + ```javascript const combinationSum4 = (nums, target) => { @@ -245,7 +246,7 @@ const combinationSum4 = (nums, target) => { }; ``` -TypeScript: +### TypeScript: ```typescript function combinationSum4(nums: number[], target: number): number { @@ -264,7 +265,7 @@ function combinationSum4(nums: number[], target: number): number { }; ``` -Rust +### Rust: ```Rust impl Solution { @@ -289,3 +290,4 @@ impl Solution { + diff --git "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" index 8da351148b..7c9f0fcefb 100644 --- "a/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" +++ "b/problems/0518.\351\233\266\351\222\261\345\205\221\346\215\242II.md" @@ -41,9 +41,9 @@ * 硬币种类不超过 500 种 * 结果符合 32 位符号整数 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -202,8 +202,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int change(int amount, int[] coins) { @@ -242,7 +242,7 @@ class Solution { } ``` -Python: +### Python: ```python @@ -260,7 +260,8 @@ class Solution: -Go: +### Go: + ```go func change(amount int, coins []int) int { // 定义dp数组 @@ -280,7 +281,8 @@ func change(amount int, coins []int) int { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn change(amount: i32, coins: Vec) -> i32 { @@ -297,7 +299,8 @@ impl Solution { } ``` -Javascript: +### Javascript: + ```javascript const change = (amount, coins) => { let dp = Array(amount + 1).fill(0); @@ -313,7 +316,7 @@ const change = (amount, coins) => { } ``` -TypeScript: +### TypeScript: ```typescript function change(amount: number, coins: number[]): number { @@ -328,7 +331,7 @@ function change(amount: number, coins: number[]): number { }; ``` -Scala: +### Scala: ```scala object Solution { @@ -349,4 +352,3 @@ object Solution { - diff --git "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" index c4e8cd9cff..9be93096fd 100644 --- "a/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\350\203\214\345\214\205\346\200\273\347\273\223\347\257\207.md" @@ -106,3 +106,4 @@ + diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" index 1a856bf5fa..50c2e5bf8c 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\244\232\351\207\215\350\203\214\345\214\205.md" @@ -144,8 +144,7 @@ int main() { ## 其他语言版本 - -Java: +### Java: ```Java public void testMultiPack1(){ @@ -192,7 +191,7 @@ public void testMultiPack2(){ } ``` -Python: +### Python: 改变物品数量为01背包格式(无参版) ```python @@ -315,7 +314,7 @@ if __name__ == "__main__": test_multi_pack(weight, value, nums, bagWeight) ``` -Go: +### Go: ```go package theory @@ -406,7 +405,7 @@ func Test_multiplePack(t *testing.T) { PASS ``` -TypeScript: +### TypeScript: > 版本一(改变数据源): @@ -469,3 +468,4 @@ testMultiPack(); + diff --git "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" index 088a3d50e9..ac4c4a1cbe 100644 --- "a/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" +++ "b/problems/\350\203\214\345\214\205\351\227\256\351\242\230\347\220\206\350\256\272\345\237\272\347\241\200\345\256\214\345\205\250\350\203\214\345\214\205.md" @@ -7,9 +7,13 @@ # 动态规划:完全背包理论基础 -**《代码随想录》算法视频公开课:[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 -## 完全背包 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 + +### 完全背包 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品都有无限个(也就是可以放入背包多次)**,求解将哪些物品装入背包里物品价值总和最大。 @@ -113,8 +117,6 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 } ``` -## C++测试代码 - 完整的C++测试代码如下: ```CPP @@ -181,7 +183,7 @@ int main() { ## 其他语言版本 -Java: +### Java: ```java //先遍历物品,再遍历背包 @@ -221,9 +223,7 @@ private static void testCompletePackAnotherWay(){ -Python: - - +### Python: 先遍历物品,再遍历背包(无参版) ```python @@ -299,7 +299,8 @@ if __name__ == "__main__": ``` -Go: +### Go: + ```go // test_CompletePack1 先遍历物品, 在遍历背包 @@ -352,7 +353,8 @@ func main() { fmt.Println(test_CompletePack2(weight, price, 4)) } ``` -Javascript: +### Javascript: + ```Javascript // 先遍历物品,再遍历背包容量 function test_completePack1() { @@ -385,7 +387,7 @@ function test_completePack2() { } ``` -TypeScript: +### TypeScript: ```typescript // 先遍历物品,再遍历背包容量 @@ -404,7 +406,7 @@ function test_CompletePack(): void { test_CompletePack(); ``` -Scala: +### Scala: ```scala // 先遍历物品,再遍历背包容量 @@ -426,7 +428,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -468,3 +470,4 @@ fn test_complete_pack() { + From 89571ea8f867a22ca0ff69017ea8b741e965c3c4 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:33:10 +0800 Subject: [PATCH 143/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200198.=E6=89=93?= =?UTF-8?q?=E5=AE=B6=E5=8A=AB=E8=88=8D=200213.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DII=200337.=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8DIII=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...223\345\256\266\345\212\253\350\210\215.md" | 18 +++++++++--------- ...3\345\256\266\345\212\253\350\210\215II.md" | 18 ++++++++++-------- ...\345\256\266\345\212\253\350\210\215III.md" | 5 +++-- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" index 80902559a6..a7bc4c998e 100644 --- "a/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" +++ "b/problems/0198.\346\211\223\345\256\266\345\212\253\350\210\215.md" @@ -31,9 +31,9 @@ * 0 <= nums.length <= 100 * 0 <= nums[i] <= 400 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -121,8 +121,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java // 动态规划 class Solution { @@ -194,7 +194,7 @@ class Solution { } ``` -Python: +### Python: 1维DP ```python @@ -255,7 +255,8 @@ class Solution: ``` -Go: +### Go: + ```Go func rob(nums []int) int { n := len(nums) @@ -275,7 +276,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: ```javascript const rob = nums => { @@ -291,7 +292,7 @@ const rob = nums => { }; ``` -TypeScript: +### TypeScript: ```typescript function rob(nums: number[]): number { @@ -314,7 +315,7 @@ function rob(nums: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -338,4 +339,3 @@ impl Solution { - diff --git "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" index ee62b5745e..cd9d596d70 100644 --- "a/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" +++ "b/problems/0213.\346\211\223\345\256\266\345\212\253\350\210\215II.md" @@ -31,9 +31,9 @@ * 1 <= nums.length <= 100 * 0 <= nums[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -104,8 +104,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int rob(int[] nums) { @@ -129,7 +129,7 @@ class Solution { } ``` -Python: +### Python: ```Python class Solution: @@ -219,7 +219,7 @@ class Solution: ``` -Go: +### Go: ```go // 打家劫舍Ⅱ 动态规划 @@ -257,7 +257,8 @@ func max(a, b int) int { } ``` -javascipt: +### JavaScript: + ```javascript var rob = function(nums) { const n = nums.length @@ -279,7 +280,7 @@ const robRange = (nums, start, end) => { return dp[end] } ``` -TypeScript: +### TypeScript: ```typescript function rob(nums: number[]): number { @@ -301,7 +302,7 @@ function robRange(nums: number[], start: number, end: number): number { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -336,3 +337,4 @@ impl Solution { + diff --git "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" index 1708b7a1e8..96d487e215 100644 --- "a/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" +++ "b/problems/0337.\346\211\223\345\256\266\345\212\253\350\210\215III.md" @@ -16,9 +16,9 @@ ![337.打家劫舍III](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223173849619.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成树了,偷不偷呢?| LeetCode:337.打家劫舍3](https://www.bilibili.com/video/BV1H24y1Q7sY),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -523,3 +523,4 @@ impl Solution { + From 6b322684a4ebc096d71d36846496501f6e3ffefa Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:55:12 +0800 Subject: [PATCH 144/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E7=B3=BB=E5=88=97=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\344\275\263\346\227\266\346\234\272.md" | 22 +++++++++---------- ...01\350\247\204\345\210\222\357\274\211.md" | 21 +++++++++--------- ...344\275\263\346\227\266\346\234\272III.md" | 17 +++++++------- ...\344\275\263\346\227\266\346\234\272IV.md" | 17 +++++++------- ...53\345\206\267\345\206\273\346\234\237.md" | 19 +++++++++------- ...01\350\247\204\345\210\222\357\274\211.md" | 21 ++++++++++-------- ...30\346\200\273\347\273\223\347\257\207.md" | 13 +---------- 7 files changed, 63 insertions(+), 67 deletions(-) diff --git "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" index 0630515662..cbdf40e85c 100644 --- "a/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" +++ "b/problems/0121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.md" @@ -24,11 +24,9 @@ * 输出:0 解释:在这种情况下, 没有交易完成, 所以最大利润为 0。 -# 算法公开课 - -**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -202,7 +200,7 @@ public: ## 其他语言版本 -Java: +### Java: > 贪心法: @@ -294,8 +292,7 @@ class Solution { ``` - -Python: +### Python: > 贪心法: ```python @@ -351,7 +348,8 @@ class Solution: return dp1 ``` -Go: +### Go: + > 贪心法: ```Go func maxProfit(prices []int) int { @@ -418,7 +416,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 动态规划 @@ -454,7 +452,7 @@ var maxProfit = function(prices) { }; ``` -TypeScript: +### TypeScript: > 贪心法 @@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number { }; ``` -C#: +### C#: > 贪心法 @@ -533,7 +531,7 @@ public class Solution } ``` -Rust: +### Rust: > 贪心 diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 02f8d287d0..6e08b57c1d 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -34,9 +34,9 @@ * 1 <= prices.length <= 3 * 10 ^ 4 * 0 <= prices[i] <= 10 ^ 4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -133,8 +133,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // 动态规划 class Solution @@ -191,7 +191,7 @@ class Solution { } ``` -Python: +### Python: > 版本一: ```python @@ -221,7 +221,8 @@ class Solution: return dp[(length-1) % 2][1] ``` -Go: +### Go: + ```go // 买卖股票的最佳时机Ⅱ 动态规划 // 时间复杂度:O(n) 空间复杂度:O(n) @@ -250,7 +251,8 @@ func max(a, b int) int { } ``` -Javascript: +### JavaScript: + ```javascript // 方法一:动态规划(dp 数组) const maxProfit = (prices) => { @@ -290,7 +292,7 @@ const maxProfit = (prices) => { } ``` -TypeScript: +### TypeScript: > 动态规划 @@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number { }; ``` -C#: +### C#: > 贪心法 @@ -363,7 +365,7 @@ public class Solution } ``` -Rust: +### Rust: > 贪心 @@ -414,4 +416,3 @@ impl Solution { - diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index a646b7d517..72dd90426c 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -39,9 +39,9 @@ * 1 <= prices.length <= 10^5 * 0 <= prices[i] <= 10^5 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -221,7 +221,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 版本一 @@ -277,7 +277,7 @@ class Solution { } ``` -Python: +### Python: > 版本一: ```python @@ -314,7 +314,7 @@ class Solution: return dp[4] ``` -Go: +### Go: ```go func maxProfit(prices []int) int { @@ -344,7 +344,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 版本一: @@ -383,7 +383,7 @@ const maxProfit = prices => { }; ``` -TypeScript: +### TypeScript: > 版本一 @@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number { }; ``` -Rust: +### Rust: > 版本一 @@ -465,3 +465,4 @@ impl Solution { + diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index 773a910a23..d4dc769893 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -31,9 +31,9 @@ * 0 <= prices.length <= 1000 * 0 <= prices[i] <= 1000 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -173,7 +173,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 版本一: 三维 dp数组 @@ -295,7 +295,7 @@ class Solution { } ``` -Python: +### Python: 版本一 @@ -329,7 +329,7 @@ class Solution: dp[j] = max(dp[j],dp[j-1]+prices[i]) return dp[2*k] ``` -Go: +### Go: 版本一: @@ -404,7 +404,7 @@ func max188(a, b int) int { } ``` -Javascript: +### JavaScript: ```javascript // 方法一:动态规划 @@ -454,7 +454,7 @@ var maxProfit = function(k, prices) { }; ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(k: number, prices: number[]): number { @@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -529,3 +529,4 @@ impl Solution { + diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index cd71136ba6..f4093b67e6 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -20,9 +20,9 @@ * 输出: 3 * 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -174,7 +174,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -273,8 +273,9 @@ class Solution { } ``` -Python: +### Python: 版本一 + ```python from typing import List @@ -319,7 +320,8 @@ class Solution: return max(dp[-1][1], dp[-1][2]) ``` -Go: +### Go: + ```go // 最佳买卖股票时机含冷冻期 动态规划 // 时间复杂度O(n) 空间复杂度O(n) @@ -355,7 +357,7 @@ func max(a, b int) int { } ``` -Javascript: +### Javascript: ```javascript const maxProfit = (prices) => { @@ -397,7 +399,7 @@ const maxProfit = (prices) => { }; ``` -TypeScript: +### TypeScript: > 版本一,与本文思路一致 @@ -455,7 +457,7 @@ function maxProfit(prices: number[]): number { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -484,3 +486,4 @@ impl Solution { + diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 042de94718..7e8e3d7c61 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -32,9 +32,9 @@ * 0 < prices[i] < 50000. * 0 <= fee < 50000. -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -97,8 +97,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java /** * 卖出时支付手续费 @@ -173,9 +173,9 @@ class Solution { } } ``` -``` +### python + -Python: ```python class Solution: def maxProfit(self, prices: List[int], fee: int) -> int: @@ -188,7 +188,8 @@ class Solution: return max(dp[-1][0], dp[-1][1]) ``` -Go: +### Go: + ```go // 买卖股票的最佳时机含手续费 动态规划 // 时间复杂度O(n) 空间复杂度O(n) @@ -211,7 +212,8 @@ func max(a, b int) int { } ``` -Javascript: +### Javascript: + ```javascript const maxProfit = (prices,fee) => { let dp = Array.from(Array(prices.length), () => Array(2).fill(0)); @@ -224,7 +226,7 @@ const maxProfit = (prices,fee) => { } ``` -TypeScript: +### TypeScript: ```typescript function maxProfit(prices: number[], fee: number): number { @@ -245,8 +247,9 @@ function maxProfit(prices: number[], fee: number): number { }; ``` -Rust: +### Rust: **贪心** + ```Rust impl Solution { pub fn max_profit(prices: Vec, fee: i32) -> i32 { diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" index 2b04b7b085..4df21fb761 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222-\350\202\241\347\245\250\351\227\256\351\242\230\346\200\273\347\273\223\347\257\207.md" @@ -471,21 +471,10 @@ public: 「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - -

+ From 038d50957cff8e3e370a1812ff36afa5feb72945 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:58:53 +0800 Subject: [PATCH 145/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200674.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\345\255\220\345\272\217\345\210\227.md" | 24 +++++++++---------- ...22\345\242\236\345\272\217\345\210\227.md" | 18 +++++++------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" index c58c3bf6c4..11cf13d9c5 100644 --- "a/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0300.\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227.md" @@ -33,7 +33,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -124,8 +124,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int lengthOfLIS(int[] nums) { @@ -147,7 +147,7 @@ class Solution { } ``` -Python: +### Python: DP ```python @@ -189,7 +189,8 @@ class Solution: return len(tails) # 返回递增子序列的长度 ``` -Go: +### Go: + ```go // 动态规划求解 func lengthOfLIS(nums []int) int { @@ -248,7 +249,8 @@ func lengthOfLIS(nums []int ) int { } ``` -Javascript +### Javascript: + ```javascript const lengthOfLIS = (nums) => { let dp = Array(nums.length).fill(1); @@ -267,7 +269,7 @@ const lengthOfLIS = (nums) => { }; ``` -TypeScript +### TypeScript: ```typescript function lengthOfLIS(nums: number[]): number { @@ -288,7 +290,8 @@ function lengthOfLIS(nums: number[]): number { }; ``` -Rust: +### Rust: + ```rust pub fn length_of_lis(nums: Vec) -> i32 { let mut dp = vec![1; nums.len() + 1]; @@ -307,13 +310,8 @@ pub fn length_of_lis(nums: Vec) -> i32 { - - - - - -

+ diff --git "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" index 8cc270ec64..0c5e64b34e 100644 --- "a/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" +++ "b/problems/0674.\346\234\200\351\225\277\350\277\236\347\273\255\351\200\222\345\242\236\345\272\217\345\210\227.md" @@ -29,7 +29,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -157,8 +157,7 @@ public: ## 其他语言版本 - -Java: +### Java: > 动态规划: ```java @@ -207,7 +206,7 @@ public static int findLengthOfLCIS(int[] nums) { } ``` -Python: +### Python: DP ```python @@ -261,7 +260,8 @@ class Solution: return result ``` -Go: +### Go: + > 动态规划: ```go func findLengthOfLCIS(nums []int) int { @@ -302,7 +302,8 @@ func findLengthOfLCIS(nums []int) int { } ``` -Rust: +### Rust: + ```rust pub fn find_length_of_lcis(nums: Vec) -> i32 { if nums.is_empty() { @@ -320,7 +321,7 @@ pub fn find_length_of_lcis(nums: Vec) -> i32 { } ``` -Javascript: +### Javascript: > 动态规划: ```javascript @@ -363,7 +364,7 @@ const findLengthOfLCIS = (nums) => { }; ``` -TypeScript: +### TypeScript: > 动态规划: @@ -409,3 +410,4 @@ function findLengthOfLCIS(nums: number[]): number { + From 9b0c0f20dcc1fd75af00188a04886dee7adc8c60 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 16:07:52 +0800 Subject: [PATCH 146/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200392.=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=AD=90=E5=BA=8F=E5=88=97=200718.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84=201035.=E4=B8=8D?= =?UTF-8?q?=E7=9B=B8=E4=BA=A4=E7=9A=84=E7=BA=BF=201143.=E6=9C=80=E9=95=BF?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\345\255\220\345\272\217\345\210\227.md" | 16 +++++++++------- ...15\345\255\220\346\225\260\347\273\204.md" | 18 +++++++++--------- ...70\344\272\244\347\232\204\347\272\277.md" | 17 +++++++++-------- ...61\345\255\220\345\272\217\345\210\227.md" | 19 ++++++++++++------- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" index c10114c05a..6342a41f00 100644 --- "a/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0392.\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227.md" @@ -28,9 +28,9 @@ 两个字符串都只由小写字符组成。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -149,8 +149,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean isSubsequence(String s, String t) { @@ -174,7 +174,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def isSubsequence(self, s: str, t: str) -> bool: @@ -190,7 +191,7 @@ class Solution: return False ``` -JavaScript: +### JavaScript: ```javascript const isSubsequence = (s, t) => { @@ -213,7 +214,7 @@ const isSubsequence = (s, t) => { }; ``` -TypeScript: +### TypeScript: ```typescript function isSubsequence(s: string, t: string): boolean { @@ -237,7 +238,7 @@ function isSubsequence(s: string, t: string): boolean { }; ``` -Go: +### Go: ```go func isSubsequence(s string, t string) bool { @@ -266,3 +267,4 @@ func isSubsequence(s string, t string) bool { + diff --git "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" index 82bf4f59e2..18cc02407f 100644 --- "a/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0718.\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204.md" @@ -25,9 +25,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - - +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,想清楚DP数组的定义 | LeetCode:718.最长重复子数组](https://www.bilibili.com/video/BV178411H7hV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -126,7 +124,7 @@ public: * 时间复杂度:O(n × m),n 为A长度,m为B长度 * 空间复杂度:O(n × m) -## 滚动数组 +### 滚动数组 在如下图中: @@ -257,8 +255,8 @@ class Solution { ## 其他语言版本 +### Java: -Java: ```java // 版本一 class Solution { @@ -300,7 +298,7 @@ class Solution { } ``` -Python: +### Python: 2维DP ```python @@ -395,7 +393,8 @@ class Solution: ``` -Go: +### Go: + ```Go func findLength(A []int, B []int) int { m, n := len(A), len(B) @@ -442,7 +441,7 @@ func max(a, b int) int { } ``` -JavaScript: +### JavaScript: > 动态规划 @@ -489,7 +488,7 @@ const findLength = (nums1, nums2) => { } ``` -TypeScript: +### TypeScript: > 动态规划: @@ -544,3 +543,4 @@ function findLength(nums1: number[], nums2: number[]): number { + diff --git "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" index 7142d75c09..74e94c842f 100644 --- "a/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" +++ "b/problems/1035.\344\270\215\347\233\270\344\272\244\347\232\204\347\272\277.md" @@ -19,7 +19,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -82,8 +82,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { @@ -106,7 +106,8 @@ Java: } ``` -Python: +### Python: + ```python class Solution: def maxUncrossedLines(self, A: List[int], B: List[int]) -> int: @@ -120,8 +121,7 @@ class Solution: return dp[-1][-1] ``` - -Golang: +### Go: ```go func maxUncrossedLines(A []int, B []int) int { @@ -152,7 +152,7 @@ func max(a, b int) int { } ``` -Rust: +### Rust: ```rust pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { @@ -173,7 +173,7 @@ pub fn max_uncrossed_lines(nums1: Vec, nums2: Vec) -> i32 { } ``` -JavaScript: +### JavaScript: ```javascript const maxUncrossedLines = (nums1, nums2) => { @@ -196,7 +196,7 @@ const maxUncrossedLines = (nums1, nums2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function maxUncrossedLines(nums1: number[], nums2: number[]): number { @@ -224,3 +224,4 @@ function maxUncrossedLines(nums1: number[], nums2: number[]): number { + diff --git "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" index 68269b87c5..260b085e14 100644 --- "a/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227.md" @@ -39,7 +39,7 @@ ## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -136,7 +136,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java /* @@ -207,8 +207,9 @@ class Solution { } ``` -Python: +### Python: 2维DP + ```python class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @@ -252,7 +253,8 @@ class Solution: ``` -Go: +### Go: + ```Go func longestCommonSubsequence(text1 string, text2 string) int { t1 := len(text1) @@ -283,7 +285,8 @@ func max(a,b int)int { ``` -Javascript: +### JavaScript: + ```javascript const longestCommonSubsequence = (text1, text2) => { let dp = Array.from(Array(text1.length+1), () => Array(text2.length+1).fill(0)); @@ -302,7 +305,7 @@ const longestCommonSubsequence = (text1, text2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function longestCommonSubsequence(text1: string, text2: string): number { @@ -326,7 +329,8 @@ function longestCommonSubsequence(text1: string, text2: string): number { }; ``` -Rust: +### Rust: + ```rust pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { let (n, m) = (text1.len(), text2.len()); @@ -353,3 +357,4 @@ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 { + From a0edc60d1fd08db91138daf430a97894a354bd98 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 16:21:21 +0800 Subject: [PATCH 147/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB=E7=B3=BB=E5=88=97=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\350\276\221\350\267\235\347\246\273.md" | 19 +++++++++++-------- ...04\345\255\220\345\272\217\345\210\227.md" | 18 ++++++++++-------- ...40\351\231\244\346\223\215\344\275\234.md" | 16 +++++++++------- ...11\346\255\245\351\223\272\345\236\253.md" | 8 ++------ 4 files changed, 32 insertions(+), 29 deletions(-) diff --git "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" index 703e891311..1ed9a86083 100644 --- "a/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" +++ "b/problems/0072.\347\274\226\350\276\221\350\267\235\347\246\273.md" @@ -40,8 +40,8 @@ exection -> execution (插入 'u') * 0 <= word1.length, word2.length <= 500 * word1 和 word2 由小写英文字母组成 -# 算法公开课 -**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -227,8 +227,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java public int minDistance(String word1, String word2) { int m = word1.length(); @@ -256,7 +256,8 @@ public int minDistance(String word1, String word2) { } ``` -Python: +### Python: + ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: @@ -274,7 +275,8 @@ class Solution: return dp[-1][-1] ``` -Go: +### Go: + ```Go func minDistance(word1 string, word2 string) int { m, n := len(word1), len(word2) @@ -310,8 +312,8 @@ func Min(args ...int) int { } ``` +### Javascript: -Javascript: ```javascript const minDistance = (word1, word2) => { let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0)); @@ -338,7 +340,7 @@ const minDistance = (word1, word2) => { }; ``` -TypeScript: +### TypeScript: ```typescript function minDistance(word1: string, word2: string): number { @@ -373,7 +375,7 @@ function minDistance(word1: string, word2: string): number { }; ``` -C: +### C: ```c @@ -405,3 +407,4 @@ int minDistance(char * word1, char * word2){ + diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index 8c82880d58..d925c5dede 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -157,8 +157,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public int numDistinct(String s, String t) { @@ -182,7 +182,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def numDistinct(self, s: str, t: str) -> int: @@ -200,7 +201,8 @@ class Solution: return dp[-1][-1] ``` -Python3: +### Python3: + ```python class SolutionDP2: """ @@ -234,7 +236,8 @@ class SolutionDP2: return dp[-1] ``` -Go: +### Go: + ```go func numDistinct(s string, t string) int { dp:= make([][]int,len(s)+1) @@ -259,8 +262,8 @@ func numDistinct(s string, t string) int { } ``` +### Javascript: -Javascript: ```javascript const numDistinct = (s, t) => { let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0)); @@ -283,7 +286,7 @@ const numDistinct = (s, t) => { }; ``` -TypeScript: +### TypeScript: ```typescript function numDistinct(s: string, t: string): number { @@ -312,9 +315,8 @@ function numDistinct(s: string, t: string): number { ``` - -

+ diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 48d15b0ba9..505a4e33ee 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -17,9 +17,9 @@ * 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea" -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode:583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[LeetCode:583.两个字符串的删除操](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -143,8 +143,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java // dp数组中存储word1和word2最长相同子序列的长度 class Solution { @@ -215,8 +215,8 @@ class Solution { } ``` +### Python: -Python: ```python class Solution: def minDistance(self, word1: str, word2: str) -> int: @@ -234,7 +234,8 @@ class Solution: return dp[-1][-1] ``` -Go: +### Go: + ```go func minDistance(word1 string, word2 string) int { dp := make([][]int, len(word1)+1) @@ -267,7 +268,8 @@ func min(a, b int) int { return b } ``` -Javascript: +### Javascript: + ```javascript // 方法一 var minDistance = (word1, word2) => { @@ -309,7 +311,7 @@ var minDistance = function (word1, word2) { }; ``` -TypeScript: +### TypeScript: > dp版本一: diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index b8adffc775..50287ce20b 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -163,8 +163,8 @@ else { ## 其他语言版本 +### Java: -Java: ```java class Solution { public int minDistance(String word1, String word2) { @@ -193,11 +193,6 @@ class Solution { } ``` -Python: - - -Go: - @@ -205,3 +200,4 @@ Go: + From 4760924db0d9d5f53719ed36fe0b3d201cfb3c6f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 16:25:35 +0800 Subject: [PATCH 148/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200516.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97=200647.?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2=20=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\345\255\220\345\272\217\345\210\227.md" | 13 ++++++------ ...36\346\226\207\345\255\220\344\270\262.md" | 21 ++++++++++++------- ...22\346\200\273\347\273\223\347\257\207.md" | 1 + 3 files changed, 20 insertions(+), 15 deletions(-) diff --git "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" index fcdd57b0f4..8092758389 100644 --- "a/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0516.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227.md" @@ -152,8 +152,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java public class Solution { @@ -175,8 +174,7 @@ public class Solution { } ``` - -Python: +### Python: ```python class Solution: @@ -193,7 +191,7 @@ class Solution: return dp[0][-1] ``` -Go: +### Go: ```Go func longestPalindromeSubseq(s string) int { @@ -222,7 +220,7 @@ func longestPalindromeSubseq(s string) int { } ``` -Javascript: +### Javascript: ```javascript const longestPalindromeSubseq = (s) => { @@ -247,7 +245,7 @@ const longestPalindromeSubseq = (s) => { }; ``` -TypeScript: +### TypeScript: ```typescript function longestPalindromeSubseq(s: string): number { @@ -281,3 +279,4 @@ function longestPalindromeSubseq(s: string): number { + diff --git "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" index 084b9f74e6..fdf83736a0 100644 --- "a/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0647.\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -26,11 +26,13 @@ 提示:输入的字符串长度不会超过 1000 。 -## 暴力解法 +## 思路 + +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后还需要一层遍历判断这个区间是不是回文。所以时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -187,7 +189,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针法 +### 双指针法 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -231,7 +233,7 @@ public: ## 其他语言版本 -Java: +### Java: 动态规划: @@ -337,7 +339,7 @@ class Solution { } ``` -Python: +### Python: > 动态规划: ```python @@ -390,7 +392,8 @@ class Solution: return res ``` -Go: +### Go: + ```Go func countSubstrings(s string) int { res:=0 @@ -416,7 +419,8 @@ func countSubstrings(s string) int { } ``` -Javascript +### Javascript: + > 动态规划 ```javascript const countSubstrings = (s) => { @@ -462,7 +466,7 @@ const countSubstrings = (s) => { } ``` -TypeScript: +### TypeScript: > 动态规划: @@ -524,3 +528,4 @@ function expandRange(s: string, left: number, right: number): number { + diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index 8a1531f8be..c86376d349 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -136,3 +136,4 @@ + From b3d1a88fde09afa258b64812cf776674a024c534 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 20:38:47 +0800 Subject: [PATCH 149/161] =?UTF-8?q?Update=200503.=E4=B8=8B=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=A4=A7=E5=85=83=E7=B4=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\345\244\247\345\205\203\347\264\240II.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 3fd4b3b6db..a090f32c3e 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -266,6 +266,24 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` +Rust +```rust +impl Solution { + pub fn next_greater_elements(nums: Vec) -> Vec { + let mut ans = vec![-1; nums.len() * 2]; + let mut stack = vec![]; + let double = nums.repeat(2); + for (idx, &i) in double.iter().enumerate() { + while !stack.is_empty() && double[*stack.last().unwrap()] < i { + let pos = stack.pop().unwrap(); + ans[pos] = i; + } + stack.push(idx); + } + ans.into_iter().take(nums.len()).collect() + } +} +```

From f3f549d317113718f61509a85937853d3a81a7f7 Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sat, 8 Jul 2023 22:13:12 +0800 Subject: [PATCH 150/161] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8?= =?UTF-8?q?=E6=B0=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index db66095da2..833a7613e2 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -926,6 +926,56 @@ int trap(int* height, int heightSize) { * 空间复杂度 O(1) +Rust + +双指针 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let n = height.len(); + let mut max_left = vec![0; height.len()]; + let mut max_right = vec![0; height.len()]; + max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| { + let lmax = lm.max(height[idx]); + let rmax = rm.max(height[n - 1 - idx]); + *x = lmax; *y = rmax; + (lmax, rmax) + }); + height.iter().enumerate().fold(0, |acc, (idx, x)| { + let h = max_left[idx].min(max_right[idx]); + if h > 0 { h - x + acc } else { acc } + }) + } +} +``` + +单调栈 + +```rust +impl Solution { + pub fn trap(height: Vec) -> i32 { + let mut stack = vec![]; + let mut ans = 0; + for (right_pos, &right_h) in height.iter().enumerate() { + while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h { + let mid_pos = stack.pop().unwrap(); + if !stack.is_empty() { + let left_pos = *stack.last().unwrap(); + let left_h = height[left_pos]; + let top = std::cmp::min(left_h, right_h); + if top > height[mid_pos] { + ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32; + } + } + } + stack.push(right_pos); + } + ans + } +} +``` +

From 7aa2a3efea391bc30047c31d17b179fcdb99221d Mon Sep 17 00:00:00 2001 From: Zhipeng Xu Date: Sun, 9 Jul 2023 14:30:01 +0800 Subject: [PATCH 151/161] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\347\232\204\347\237\251\345\275\242.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index f9a8350818..bc82a860cc 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -670,6 +670,61 @@ function largestRectangleArea(heights: number[]): number { ``` +Rust + +双指针预处理 +```rust + +impl Solution { + pub fn largest_rectangle_area(v: Vec) -> i32 { + let n = v.len(); + let mut left_smaller_idx = vec![-1; n]; + let mut right_smaller_idx = vec![n as i32; n]; + for i in 1..n { + let mut mid = i as i32 - 1; + while mid >= 0 && v[mid as usize] >= v[i] { + mid = left_smaller_idx[mid as usize]; + } + left_smaller_idx[i] = mid; + } + for i in (0..n-1).rev() { + let mut mid = i + 1; + while mid < n && v[mid] >= v[i] { + mid = right_smaller_idx[mid] as usize; + } + right_smaller_idx[i] = mid as i32; + } + let mut res = 0; + for (idx, &e) in v.iter().enumerate() { + res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e); + } + dbg!(res) + } +} +``` + +单调栈 +```rust +impl Solution { + pub fn largest_rectangle_area1(mut v: Vec) -> i32 { + v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值 + v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值 + let mut res = 0; + let mut stack = vec![]; // 递增的栈 + for (idx, &e) in v.iter().enumerate() { + while !stack.is_empty() && v[*stack.last().unwrap()] > e { + let pos = stack.pop().unwrap(); + let prev_pos = *stack.last().unwrap(); + let s = (idx - prev_pos - 1) as i32 * v[pos]; + res = res.max(s); + } + stack.push(idx); + } + res + } +} +``` +

From 0dc6bb5669981d25069fc338566e73b3a8e97828 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 25 Jul 2023 16:24:55 +0800 Subject: [PATCH 152/161] =?UTF-8?q?=E6=B7=BB=E5=8A=A059.=E8=9E=BA=E6=97=8B?= =?UTF-8?q?=E7=9F=A9=E9=98=B5II=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\227\213\347\237\251\351\230\265II.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index f03fcdad35..73e9e4daea 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -688,6 +688,58 @@ public class Solution { } ``` +### Ruby#: +```ruby +def generate_matrix(n) + result = Array.new(n) { Array.new(n, 0) } + #循环次数 + loop_times = 0 + #步长 + step = n - 1 + val = 1 + + + while loop_times < n / 2 + #模拟从左向右 + for i in 0..step - 1 + #行数不变,列数变 + result[loop_times][i+loop_times] = val + val += 1 + end + + #模拟从上到下 + for i in 0..step - 1 + #列数不变,行数变 + result[i+loop_times][n-loop_times-1] = val + val += 1 + end + + #模拟从右到左 + for i in 0..step - 1 + #行数不变,列数变 + result[n-loop_times-1][n-loop_times-i-1] = val + val += 1 + end + + #模拟从下到上 + for i in 0..step - 1 + #列数不变,行数变 + result[n-loop_times-i-1][loop_times] = val + val += 1 + end + + loop_times += 1 + step -= 2 + end + + #如果是奇数,则填充最后一个元素 + result[n/2][n/2] = n**2 if n % 2 + + return result + +end +``` +

From 7c9fcfe09d91fa4e5bc66ee6ab16fac7446426e0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:12:11 +0800 Subject: [PATCH 153/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=A4=8D=E6=9D=82=E5=BA=A6=20O(n)=E8=B6=85=E6=97=B6=20?= =?UTF-8?q?=E6=8B=8D=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\244\232\345\244\247\357\274\237.md" | 22 +++++-------------- ...50\350\277\231\351\207\214\357\274\201.md" | 16 ++++---------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" index a488c0bad4..8be48f38c2 100644 --- "a/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" +++ "b/problems/O(n)\347\232\204\347\256\227\346\263\225\345\261\205\347\204\266\350\266\205\346\227\266\344\272\206\357\274\214\346\255\244\346\227\266\347\232\204n\347\251\266\347\253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" @@ -13,7 +13,7 @@ 计算机究竟1s可以执行多少次操作呢? 接下来探讨一下这个问题。 -# 超时是怎么回事 +## 超时是怎么回事 ![程序超时](https://code-thinking-1253855093.file.myqcloud.com/pics/20200729112716117.png) @@ -25,7 +25,7 @@ 如果n的规模已经足够让O(n)的算法运行时间超过了1s,就应该考虑log(n)的解法了。 -# 从硬件配置看计算机的性能 +## 从硬件配置看计算机的性能 计算机的运算速度主要看CPU的配置,以2015年MacPro为例,CPU配置:2.7 GHz Dual-Core Intel Core i5 。 @@ -44,7 +44,7 @@ 所以我们的程序在计算机上究竟1s真正能执行多少次操作呢? -# 做个测试实验 +## 做个测试实验 在写测试程序测1s内处理多大数量级数据的时候,有三点需要注意: @@ -155,7 +155,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符 至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。 -# 完整测试代码 +## 完整测试代码 ```CPP #include @@ -212,7 +212,7 @@ int main() { ``` -# 总结 +## 总结 本文详细分析了在leetcode上做题程序为什么会有超时,以及从硬件配置上大体知道CPU的执行速度,然后亲自做一个实验来看看O(n)的算法,跑一秒钟,这个n究竟是做大,最后给出不同时间复杂度,一秒内可以运算出来的n的大小。 @@ -220,17 +220,6 @@ int main() { 这样,大家应该对程序超时时候的数据规模有一个整体的认识了。 -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -238,3 +227,4 @@ Go: + diff --git "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" index c479dddc67..95c7567c75 100644 --- "a/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" +++ "b/problems/\345\205\263\344\272\216\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\344\275\240\344\270\215\347\237\245\351\201\223\347\232\204\351\203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" @@ -8,6 +8,8 @@ 所以重新整理的时间复杂度文章,正式和大家见面啦! +# 时间复杂度 + ## 究竟什么是时间复杂度 **时间复杂度是一个函数,它定性描述该算法的运行时间**。 @@ -145,7 +147,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 **当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。 -# 总结 +## 总结 本篇讲解了什么是时间复杂度,复杂度是用来干什么,以及数据规模对时间复杂度的影响。 @@ -157,17 +159,6 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项 如果感觉「代码随想录」很不错,赶快推荐给身边的朋友同学们吧,他们发现和「代码随想录」相见恨晚! -## 其他语言版本 - - -Java: - - -Python: - - -Go: - @@ -175,3 +166,4 @@ Go: + From e0c5da76e6a616a1cf3da18059c59947a90f877d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:18:47 +0800 Subject: [PATCH 154/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8D=95=E8=B0=83?= =?UTF-8?q?=E6=A0=88=E7=B3=BB=E5=88=97=E9=A2=98=E7=9B=AE=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 20 +++++++-------- ...47\347\232\204\347\237\251\345\275\242.md" | 25 ++++++++++--------- ...4\345\244\247\345\205\203\347\264\240I.md" | 17 +++++++------ ...\345\244\247\345\205\203\347\264\240II.md" | 19 ++++++++------ ...17\346\227\245\346\270\251\345\272\246.md" | 15 +++++------ 5 files changed, 53 insertions(+), 43 deletions(-) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 833a7613e2..4f0682ba6f 100644 --- "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" +++ "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" @@ -29,7 +29,7 @@ * 输出:9 -# 思路 +## 思路 接雨水问题在面试中还是常见题目的,有必要好好讲一讲。 @@ -39,7 +39,7 @@ * 动态规划 * 单调栈 -## 暴力解法 +### 暴力解法 本题暴力解法也是也是使用双指针。 @@ -137,7 +137,7 @@ public: 力扣后面修改了后台测试数据,所以以上暴力解法超时了。 -## 双指针优化 +### 双指针优化 在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。 @@ -184,7 +184,7 @@ public: }; ``` -## 单调栈解法 +### 单调栈解法 关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -194,7 +194,7 @@ public: 而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。 -### 准备工作 +#### 准备工作 那么本题使用单调栈有如下几个问题: @@ -248,7 +248,7 @@ stack st; // 存着下标,计算的时候用下标对应的柱子高度 明确了如上几点,我们再来看处理逻辑。 -### 单调栈处理逻辑 +#### 单调栈处理逻辑 以下操作过程其实和 [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 也是一样的,建议先做 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。 @@ -596,7 +596,7 @@ class Solution: ``` -### Go +### Go: ```go func trap(height []int) int { @@ -802,7 +802,7 @@ var trap = function(height) { }; ``` -### TypeScript +### TypeScript: 暴力解法: @@ -925,8 +925,7 @@ int trap(int* height, int heightSize) { * 时间复杂度 O(n) * 空间复杂度 O(1) - -Rust +### Rust: 双指针 @@ -980,3 +979,4 @@ impl Solution { + diff --git "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" index bc82a860cc..4d949941cb 100644 --- "a/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" +++ "b/problems/0084.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242.md" @@ -20,7 +20,7 @@ * 1 <= heights.length <=10^5 * 0 <= heights[i] <= 10^4 -# 思路 +## 思路 本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解! @@ -28,7 +28,7 @@ 我们先来看一下暴力解法的解法: -## 暴力解法 +### 暴力解法 ```CPP class Solution { @@ -55,7 +55,7 @@ public: 如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。 -## 双指针解法 +### 双指针解法 本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。 @@ -98,7 +98,7 @@ public: }; ``` -## 单调栈 +### 单调栈 本地单调栈的解法和接雨水的题目是遥相呼应的。 @@ -169,7 +169,7 @@ public: } }; -``` +``` 细心的录友会发现,我在 height数组上后,都加了一个元素0, 为什么这么做呢? @@ -229,7 +229,7 @@ public: ## 其他语言版本 -Java: +### Java: 暴力解法: ```java @@ -335,7 +335,7 @@ class Solution { } ``` -Python3: +### Python3: ```python @@ -468,7 +468,7 @@ class Solution: ``` -Go: +### Go: > 单调栈 @@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int { ``` -JavaScript: +### JavaScript: + ```javascript //双指针 js中运行速度最快 var largestRectangleArea = function(heights) { @@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) { return maxArea; }; ``` -TypeScript: +### TypeScript: > 暴力法(会超时): @@ -669,8 +670,7 @@ function largestRectangleArea(heights: number[]): number { }; ``` - -Rust +### Rust: 双指针预处理 ```rust @@ -730,3 +730,4 @@ impl Solution { + diff --git "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" index 411a47df9b..6bcafafba2 100644 --- "a/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" +++ "b/problems/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.md" @@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位 * nums1和nums2中所有整数 互不相同 * nums1 中的所有整数同样出现在 nums2 中 -# 思路 +## 思路 做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html) @@ -191,7 +191,8 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 -Java +### Java + ```java class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { @@ -248,7 +249,8 @@ class Solution { } } ``` -Python3: +### Python3 + ```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: @@ -269,7 +271,7 @@ class Solution: return result ``` -Go: +### Go > 未精简版本 ```go @@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int { } ``` -JavaScript: +### JavaScript ```JS var nextGreaterElement = function (nums1, nums2) { @@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) { }; ``` -TypeScript: +### TypeScript ```typescript function nextGreaterElement(nums1: number[], nums2: number[]): number[] { @@ -387,7 +389,7 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] { }; ``` -Rust +### Rust ```rust impl Solution { @@ -419,3 +421,4 @@ impl Solution { + diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index a090f32c3e..023e4d7e0e 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -22,7 +22,7 @@ * -10^9 <= nums[i] <= 10^9 -# 思路 +## 思路 做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。 @@ -138,7 +138,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] nextGreaterElements(int[] nums) { @@ -162,7 +163,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: class Solution: @@ -196,7 +198,8 @@ class Solution: stack.append(i) return ans ``` -Go: +### Go: + ```go func nextGreaterElements(nums []int) []int { length := len(nums) @@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int { } ``` -JavaScript: +### JavaScript: ```JS /** @@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) { return res; }; ``` -TypeScript: +### TypeScript: ```typescript function nextGreaterElements(nums: number[]): number[] { @@ -266,7 +269,8 @@ function nextGreaterElements(nums: number[]): number[] { }; ``` -Rust +### Rust: + ```rust impl Solution { pub fn next_greater_elements(nums: Vec) -> Vec { @@ -290,3 +294,4 @@ impl Solution { + diff --git "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" index d2da37371a..fc1a80631c 100644 --- "a/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" +++ "b/problems/0739.\346\257\217\346\227\245\346\270\251\345\272\246.md" @@ -211,8 +211,7 @@ public: ## 其他语言版本 - -Java: +### Java: ```java class Solution { @@ -270,7 +269,8 @@ class Solution { } ``` -Python: +### Python: + > 未精简版本 ```python @@ -307,7 +307,7 @@ class Solution: return answer ``` -Go: +### Go: > 暴力法 @@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int { } ``` -JavaScript: +### JavaScript: ```javascript // 版本一 @@ -429,7 +429,7 @@ var dailyTemperatures = function(temperatures) { }; ``` -TypeScript: +### TypeScript: > 精简版: @@ -455,7 +455,7 @@ function dailyTemperatures(temperatures: number[]): number[] { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -482,3 +482,4 @@ impl Solution { + From dc9fb7cb0bc7bff583e66bf3e3699af961f60a8c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:28:14 +0800 Subject: [PATCH 155/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\275\254\346\225\260\347\273\204.md" | 15 ++++++------- ...3.\347\247\273\345\212\250\351\233\266.md" | 21 +++++++++---------- ...\345\272\217\346\225\260\347\273\204II.md" | 3 +-- ...61\350\204\211\346\225\260\347\273\204.md" | 17 ++++++++------- ...72\347\216\260\346\254\241\346\225\260.md" | 16 +++++++------- ...27\347\232\204\346\225\260\345\255\227.md" | 17 ++++++++------- 6 files changed, 46 insertions(+), 43 deletions(-) diff --git "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" index 3581969439..d60612e92b 100644 --- "a/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" +++ "b/problems/0189.\346\227\213\350\275\254\346\225\260\347\273\204.md" @@ -33,7 +33,7 @@ 向右旋转 2 步: [3,99,-1,-100]。 -# 思路 +## 思路 这道题目在字符串里其实很常见,我把字符串反转相关的题目列一下: @@ -83,9 +83,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -106,7 +106,7 @@ class Solution { } ``` -## Python +### Python 方法一:局部翻转 + 整体翻转 ```python @@ -139,7 +139,7 @@ class Solution: # 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。 ``` -## Go +### Go ```go func rotate(nums []int, k int) { @@ -157,7 +157,7 @@ func reverse(nums []int){ } ``` -## JavaScript +### JavaScript ```js var rotate = function (nums, k) { @@ -178,7 +178,7 @@ var rotate = function (nums, k) { }; ``` -## TypeScript +### TypeScript ```typescript function rotate(nums: number[], k: number): void { @@ -205,3 +205,4 @@ function reverseByRange(nums: number[], left: number, right: number): void { + diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index 22d6428c10..ee3f429150 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -3,10 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

- -# 动态规划:一样的套路,再求一次完全平方数 - -# 283. 移动零 +# 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) @@ -22,7 +19,7 @@ 尽量减少操作次数。 -# 思路 +## 思路 做这道题目之前,大家可以做一做[27.移除元素](https://programmercarl.com/0027.移除元素.html) @@ -58,9 +55,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java public void moveZeroes(int[] nums) { @@ -77,7 +74,7 @@ public void moveZeroes(int[] nums) { } ``` -Python: +### Python: ```python def moveZeroes(self, nums: List[int]) -> None: @@ -100,7 +97,7 @@ Python: fast += 1 ``` -Go: +### Go: ```go func moveZeroes(nums []int) { @@ -116,7 +113,8 @@ func moveZeroes(nums []int) { } ``` -JavaScript: +### JavaScript: + ```javascript var moveZeroes = function(nums) { let slow = 0; @@ -133,7 +131,7 @@ var moveZeroes = function(nums) { }; ``` -TypeScript: +### TypeScript: ```typescript function moveZeroes(nums: number[]): void { @@ -159,3 +157,4 @@ function moveZeroes(nums: number[]): void { + diff --git "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" index c4654f16d1..72be8fa732 100644 --- "a/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" +++ "b/problems/0922.\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204II.md" @@ -147,8 +147,6 @@ class Solution { } ``` -### java - ```java //方法一:采用额外的数组空间 class Solution { @@ -384,3 +382,4 @@ function sortArrayByParityII(nums: number[]): number[] { + diff --git "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" index f43a210837..48c29eb479 100644 --- "a/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" +++ "b/problems/0941.\346\234\211\346\225\210\347\232\204\345\261\261\350\204\211\346\225\260\347\273\204.md" @@ -33,7 +33,7 @@ * 输出:true -# 思路 +## 思路 判断是山峰,主要就是要严格的保存左边到中间,和右边到中间是递增的。 @@ -71,9 +71,9 @@ public: 如果想系统学一学双指针的话, 可以看一下这篇[双指针法:总结篇!](https://programmercarl.com/双指针总结.html) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -101,7 +101,7 @@ class Solution { } ``` -## Python3 +### Python3 ```python class Solution: @@ -118,7 +118,7 @@ class Solution: ``` -## Go +### Go ```go func validMountainArray(arr []int) bool { @@ -142,7 +142,7 @@ func validMountainArray(arr []int) bool { } ``` -## JavaScript +### JavaScript ```js var validMountainArray = function(arr) { @@ -157,7 +157,7 @@ var validMountainArray = function(arr) { }; ``` -## TypeScript +### TypeScript ```typescript function validMountainArray(arr: number[]): boolean { @@ -177,7 +177,7 @@ function validMountainArray(arr: number[]): boolean { }; ``` -## C# +### C# ```csharp public class Solution { @@ -201,3 +201,4 @@ public class Solution { + diff --git "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" index 1a7a001989..83ebbcb7bc 100644 --- "a/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" +++ "b/problems/1207.\347\213\254\344\270\200\346\227\240\344\272\214\347\232\204\345\207\272\347\216\260\346\254\241\346\225\260.md" @@ -31,7 +31,7 @@ * -1000 <= arr[i] <= 1000 -# 思路 +## 思路 这道题目数组在是哈希法中的经典应用,如果对数组在哈希法中的使用还不熟悉的同学可以看这两篇:[数组在哈希法中的应用](https://programmercarl.com/0242.有效的字母异位词.html)和[哈希法:383. 赎金信](https://programmercarl.com/0383.赎金信.html) @@ -71,9 +71,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -97,7 +97,8 @@ class Solution { } ``` -Python: +### Python: + ```python # 方法 1: 数组在哈西法的应用 class Solution: @@ -133,10 +134,8 @@ class Solution: ``` +### JavaScript: -Go: - -JavaScript: ``` javascript // 方法一:使用数组记录元素出现次数 var uniqueOccurrences = function(arr) { @@ -171,7 +170,7 @@ var uniqueOccurrences = function(arr) { }; ``` -TypeScript: +### TypeScript: > 借用数组: @@ -209,3 +208,4 @@ function uniqueOccurrences(arr: number[]): boolean { + diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 7c26876957..c706ba216e 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -39,7 +39,7 @@ * 2 <= nums.length <= 500 * 0 <= nums[i] <= 100 -# 思路 +## 思路 两层for循环暴力查找,时间复杂度明显为$O(n^2)$。 @@ -113,9 +113,9 @@ public: 可以排序之后加哈希,时间复杂度为$O(n\log n)$ -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```Java public int[] smallerNumbersThanCurrent(int[] nums) { @@ -136,7 +136,8 @@ public int[] smallerNumbersThanCurrent(int[] nums) { } ``` -Python: +### Python: + ```python class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: @@ -151,7 +152,8 @@ class Solution: return res ``` -Go: +### Go: + ```go func smallerNumbersThanCurrent(nums []int) []int { // map,key[数组中出现的数] value[比这个数小的个数] @@ -180,7 +182,8 @@ func smallerNumbersThanCurrent(nums []int) []int { } ``` -JavaScript: +### JavaScript: + ```javascript // 方法一:使用哈希表记录位置 var smallerNumbersThanCurrent = function(nums) { @@ -217,7 +220,7 @@ var smallerNumbersThanCurrent = function(nums) { }; ``` -TypeScript: +### TypeScript: > 暴力法: From 56f37806cac317895ca1785b2e526343ff07426d Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:32:16 +0800 Subject: [PATCH 156/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\227\347\254\246\344\270\262.md" | 15 +++++----- ...36\346\226\207\351\223\276\350\241\250.md" | 1 + ...70\347\224\250\345\255\227\347\254\246.md" | 28 ++++++++++++------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" index a507638c35..e07ab746d9 100644 --- "a/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0205.\345\220\214\346\236\204\345\255\227\347\254\246\344\270\262.md" @@ -29,7 +29,7 @@ 提示:可以假设 s 和 t 长度相同。 -# 思路 +## 思路 字符串没有说都是小写字母之类的,所以用数组不合适了,用map来做映射。 @@ -61,9 +61,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -87,7 +87,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -110,7 +110,7 @@ class Solution: return True ``` -## Go +### Go ```go func isIsomorphic(s string, t string) bool { @@ -132,7 +132,7 @@ func isIsomorphic(s string, t string) bool { } ``` -## JavaScript +### JavaScript ```js var isIsomorphic = function(s, t) { @@ -156,7 +156,7 @@ var isIsomorphic = function(s, t) { }; ``` -## TypeScript +### TypeScript ```typescript function isIsomorphic(s: string, t: string): boolean { @@ -183,3 +183,4 @@ function isIsomorphic(s: string, t: string): boolean { + diff --git "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" index 18b397e32f..fef942fc49 100644 --- "a/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/problems/0234.\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null { + diff --git "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" index a53148b313..1138d7fc2f 100644 --- "a/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" +++ "b/problems/1002.\346\237\245\346\211\276\345\270\270\347\224\250\345\255\227\347\254\246.md" @@ -30,7 +30,7 @@ words[i] 由小写英文字母组成 -# 思路 +## 思路 这道题意一起就有点绕,不是那么容易懂,其实就是26个小写字符中有字符 在所有字符串里都出现的话,就输出,重复的也算。 @@ -140,7 +140,7 @@ public: ## 其他语言版本 -Java: +### Java: ```Java class Solution { @@ -174,7 +174,8 @@ class Solution { } } ``` -Python +### Python + ```python class Solution: def commonChars(self, words: List[str]) -> List[str]: @@ -218,7 +219,8 @@ class Solution: return l ``` -javaScript +### JavaScript + ```js var commonChars = function (words) { let res = [] @@ -285,7 +287,8 @@ var commonChars = function(words) { } ``` -TypeScript +### TypeScript + ```ts console.time("test") let str: string = "" @@ -321,7 +324,8 @@ TypeScript return str.split("") ``` -GO +### GO + ```golang func commonChars(words []string) []string { length:=len(words) @@ -357,7 +361,8 @@ func min(a,b int)int{ } ``` -Swift: +### Swift: + ```swift func commonChars(_ words: [String]) -> [String] { var res = [String]() @@ -397,7 +402,8 @@ func commonChars(_ words: [String]) -> [String] { } ``` -C: +### C: + ```c //若两个哈希表定义为char数组(每个单词的最大长度不会超过100,因此可以用char表示),可以提高时间和空间效率 void updateHashTable(int* hashTableOne, int* hashTableTwo) { @@ -449,7 +455,8 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){ return ret; } ``` -Scala: +### Scala: + ```scala object Solution { def commonChars(words: Array[String]): List[String] = { @@ -483,7 +490,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -522,3 +529,4 @@ impl Solution { + From bbb2a60f8a68bf03b466e3b567ada9cfcd06d24f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:37:16 +0800 Subject: [PATCH 157/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\345\220\214\347\232\204\346\240\221.md" | 21 +++++++++------- ...02\347\202\271\346\214\207\351\222\210.md" | 19 +++++++------- ...60\345\255\227\344\271\213\345\222\214.md" | 25 +++++++++++-------- ...21\345\217\230\345\271\263\350\241\241.md" | 18 +++++++------ 4 files changed, 47 insertions(+), 36 deletions(-) diff --git "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" index 96acacf61c..56a6c8840f 100644 --- "a/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" +++ "b/problems/0100.\347\233\270\345\220\214\347\232\204\346\240\221.md" @@ -19,7 +19,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png) -# 思路 +## 思路 在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。 @@ -115,7 +115,7 @@ public: 当然我可以把如上代码整理如下: -## 递归 +### 递归 ```CPP class Solution { @@ -134,7 +134,7 @@ public: }; ``` -## 迭代法 +### 迭代法 ```CPP class Solution { @@ -166,9 +166,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 递归法 @@ -205,7 +205,8 @@ class Solution { } } ``` -Python: +### Python: + ```python # 递归法 class Solution: @@ -236,7 +237,8 @@ class Solution: que.append(rightNode.right) return True ``` -Go: +### Go: + > 递归法 ```go func isSameTree(p *TreeNode, q *TreeNode) bool { @@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool { } ``` -JavaScript: +### JavaScript: > 递归法 @@ -296,7 +298,7 @@ var isSameTree = (p, q) => { }; ``` -TypeScript: +### TypeScript: > 递归法-先序遍历 @@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + diff --git "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" index 31bb6822cc..003ef75afe 100644 --- "a/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" +++ "b/problems/0116.\345\241\253\345\205\205\346\257\217\344\270\252\350\212\202\347\202\271\347\232\204\344\270\213\344\270\200\344\270\252\345\217\263\344\276\247\350\212\202\347\202\271\346\214\207\351\222\210.md" @@ -30,7 +30,7 @@ struct Node { ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png) -# 思路 +## 思路 注意题目提示内容,: * 你只能使用常量级额外空间。 @@ -38,7 +38,7 @@ struct Node { 基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。 -## 递归 +### 递归 一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。 @@ -83,7 +83,7 @@ public: }; ``` -## 迭代(层序遍历) +### 迭代(层序遍历) 本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -114,9 +114,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 递归法 @@ -169,7 +169,7 @@ class Solution { } ``` -## Python +### Python ```python # 递归法 @@ -210,7 +210,7 @@ class Solution: nodePre.next = None # 本层最后一个节点指向None return root ``` -## Go +### Go ```go // 迭代法 func connect(root *Node) *Node { @@ -259,7 +259,7 @@ func connect(root *Node) *Node { } ``` -## JavaScript +### JavaScript ```js const connect = root => { @@ -287,7 +287,7 @@ const connect = root => { }; ``` -## TypeScript +### TypeScript (注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’) @@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null { + diff --git "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" index 445e108aa9..ebb36071cf 100644 --- "a/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" +++ "b/problems/0129.\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214.md" @@ -10,7 +10,7 @@ [力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) -# 思路 +## 思路 本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。 @@ -24,7 +24,7 @@ 那么先按递归三部曲来分析: -## 递归三部曲 +### 递归三部曲 如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html) @@ -116,7 +116,7 @@ path.pop_back(); // 回溯 ``` **把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。 -## 整体C++代码 +整体C++代码 关键逻辑分析完了,整体C++代码如下: @@ -162,16 +162,16 @@ public: }; ``` -# 总结 +## 总结 过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。 **我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!** -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java class Solution { @@ -219,7 +219,8 @@ class Solution { } ``` -Python: +### Python: + ```python class Solution: def sumNumbers(self, root: TreeNode) -> int: @@ -246,7 +247,7 @@ class Solution: backtrace(root) return res ``` -Go: +### Go: ```go func sumNumbers(root *TreeNode) int { @@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) { -JavaScript: +### JavaScript: + ```javascript var sumNumbers = function(root) { const listToInt = path => { @@ -315,7 +317,7 @@ var sumNumbers = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function sumNumbers(root: TreeNode | null): number { @@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number { }; ``` -C: +### C: ```c //sum记录总和 @@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){ + diff --git "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" index 0f81745c36..57e56b8fe3 100644 --- "a/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" +++ "b/problems/1382.\345\260\206\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\345\217\230\345\271\263\350\241\241.md" @@ -28,7 +28,7 @@ * 树节点的数目在 1 到 10^4 之间。 * 树节点的值互不相同,且在 1 到 10^5 之间。 -# 思路 +## 思路 这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。 @@ -71,9 +71,10 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 + +### Java: -Java: ```java class Solution { ArrayList res = new ArrayList(); @@ -99,7 +100,8 @@ class Solution { } } ``` -Python: +### Python: + ```python class Solution: def balanceBST(self, root: TreeNode) -> TreeNode: @@ -121,7 +123,7 @@ class Solution: traversal(root) return getTree(res, 0, len(res) - 1) ``` -Go: +### Go: ```go /** @@ -163,7 +165,8 @@ func balanceBST(root *TreeNode) *TreeNode { ``` -JavaScript: +### JavaScript: + ```javascript var balanceBST = function(root) { const res = []; @@ -188,7 +191,7 @@ var balanceBST = function(root) { }; ``` -TypeScript: +### TypeScript: ```typescript function balanceBST(root: TreeNode | null): TreeNode | null { @@ -218,3 +221,4 @@ function buildTree(arr: number[], left: number, right: number): TreeNode | null + From e9b0d46f3535546063192c170606bffa8ff75a32 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:41:58 +0800 Subject: [PATCH 158/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=B4=AA=E5=BF=83?= =?UTF-8?q?=E4=B8=8E=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=20=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E9=A2=98=E7=9B=AE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\346\226\207\345\255\220\344\270\262.md" | 23 ++++++++++--------- ...\345\233\236\346\226\207\344\270\262II.md" | 13 ++++++----- ...a2\345\217\202\350\256\256\351\231\242.md" | 17 +++++++------- ...27\347\232\204\344\270\252\346\225\260.md" | 13 ++++++----- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" index b1987b87b3..614f60514c 100644 --- "a/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" +++ "b/problems/0005.\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.md" @@ -30,17 +30,17 @@ * 输出:"a" -# 思路 +## 思路 本题和[647.回文子串](https://programmercarl.com/0647.回文子串.html) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串 -## 暴力解法 +### 暴力解法 两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。 时间复杂度:O(n^3) -## 动态规划 +### 动态规划 动规五部曲: @@ -208,7 +208,7 @@ public: * 时间复杂度:O(n^2) * 空间复杂度:O(n^2) -## 双指针 +### 双指针 动态规划的空间复杂度是偏高的,我们再看一下双指针法。 @@ -258,9 +258,9 @@ public: -# 其他语言版本 +## 其他语言版本 -Java: +### Java: ```java // 双指针 动态规划 @@ -327,7 +327,7 @@ class Solution { } ``` -Python: +### Python: ```python class Solution: @@ -377,7 +377,7 @@ class Solution: return s[start:end] ``` -Go: +### Go: ```go func longestPalindrome(s string) string { @@ -411,7 +411,7 @@ func longestPalindrome(s string) string { ``` -JavaScript: +### JavaScript: ```js //动态规划解法 @@ -527,7 +527,7 @@ var longestPalindrome = function(s) { }; ``` -C: +### C: 动态规划: ```c @@ -615,7 +615,7 @@ char * longestPalindrome(char * s){ } ``` -C#: +### C#: 動態規則: ```c# @@ -681,3 +681,4 @@ public class Solution { + diff --git "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" index 9b164dfbb5..eb91a1899f 100644 --- "a/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" +++ "b/problems/0132.\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262II.md" @@ -34,7 +34,7 @@ * 1 <= s.length <= 2000 * s 仅由小写英文字母组成 -# 思路 +## 思路 我们在讲解回溯法系列的时候,讲过了这道题目[回溯算法:131.分割回文串](https://programmercarl.com/0131.分割回文串.html)。 @@ -201,9 +201,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return dp[-1] ``` -## Go +### Go ```go func minCut(s string) int { @@ -330,7 +330,7 @@ func min(i, j int) int { } ``` -## JavaScript +### JavaScript ```js var minCut = function(s) { @@ -376,3 +376,4 @@ var minCut = function(s) { + diff --git "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" index a1420a66db..db6b43df8e 100644 --- "a/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" +++ "b/problems/0649.Dota2\345\217\202\350\256\256\351\231\242.md" @@ -42,7 +42,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利。 -# 思路 +## 思路 这道题 题意太绕了,我举一个更形象的例子给大家捋顺一下。 @@ -70,7 +70,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一 如果对贪心算法理论基础还不了解的话,可以看看这篇:[关于贪心算法,你该了解这些!](https://programmercarl.com/贪心算法理论基础.html) ,相信看完之后对贪心就有基本的了解了。 -# 代码实现 +## 代码实现 实现代码,在每一轮循环的过程中,去过模拟优先消灭身后的对手,其实是比较麻烦的。 @@ -111,9 +111,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -145,7 +145,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -173,7 +173,7 @@ class Solution: return "Radiant" if R else "Dire" ``` -## Go +### Go ```go @@ -214,7 +214,7 @@ func predictPartyVictory(senateStr string) string { } ``` -## JavaScript +### JavaScript ```js var predictPartyVictory = function(senateStr) { @@ -244,7 +244,7 @@ var predictPartyVictory = function(senateStr) { }; ``` -## TypeScript +### TypeScript ```typescript function predictPartyVictory(senate: string): string { @@ -287,3 +287,4 @@ function predictPartyVictory(senate: string): string { + diff --git "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" index da80a4e039..0277f24989 100644 --- "a/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" +++ "b/problems/0673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260.md" @@ -24,7 +24,7 @@ * 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。 -# 思路 +## 思路 这道题可以说是 [300.最长上升子序列](https://programmercarl.com/0300.最长上升子序列.html) 的进阶版本 @@ -221,9 +221,9 @@ public: 还有O(nlog n)的解法,使用树状数组,今天有点忙就先不写了,感兴趣的同学可以自行学习一下,这里有我之前写的树状数组系列博客:https://blog.csdn.net/youngyangyang04/category_871105.html (十年前的陈年老文了) -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -257,7 +257,7 @@ class Solution { } ``` -## Python +### Python ```python class Solution: @@ -286,7 +286,7 @@ class Solution: return result; ``` -## Go +### Go ```go @@ -332,7 +332,7 @@ func findNumberOfLIS(nums []int) int { } ``` -## JavaScript +### JavaScript ```js var findNumberOfLIS = function(nums) { @@ -364,3 +364,4 @@ var findNumberOfLIS = function(nums) { + From a5eb340ee630091b199407bea8d3191b546708a0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 27 Jul 2023 14:48:04 +0800 Subject: [PATCH 159/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=9B=BE=E8=AE=BA?= =?UTF-8?q?=20=E5=B9=B6=E6=9F=A5=E9=9B=86=20=E6=A8=A1=E6=8B=9F=20=E4=BD=8D?= =?UTF-8?q?=E8=BF=90=E7=AE=97=20=E9=A2=9D=E5=A4=96=E9=A2=98=E7=9B=AE=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\200\344\270\252\346\216\222\345\210\227.md" | 13 +++++++------ ...15\225\350\257\215\346\216\245\351\276\231.md" | 15 +++++++-------- ...61\277\347\232\204\345\221\250\351\225\277.md" | 13 ++++++++----- ...77\224\345\233\236\345\216\237\347\202\271.md" | 15 ++++++++------- ...06\227\344\275\231\350\277\236\346\216\245.md" | 13 +++++++------ ...\227\344\275\231\350\277\236\346\216\245II.md" | 11 ++++++----- ...25\260\347\233\256\346\216\222\345\272\217.md" | 15 ++++++++------- 7 files changed, 51 insertions(+), 44 deletions(-) diff --git "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" index 34aa1086c0..3cfb673a29 100644 --- "a/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" +++ "b/problems/0031.\344\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" @@ -34,7 +34,7 @@ * 输出:[1] -# 思路 +## 思路 一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在: @@ -92,9 +92,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -159,7 +159,7 @@ class Solution { } ``` -## Python +### Python >直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数 ```python class Solution: @@ -191,7 +191,7 @@ class Solution: """ ``` -## Go +### Go ```go //卡尔的解法 @@ -216,7 +216,7 @@ func reverse(a []int,begin,end int){ } ``` -## JavaScript +### JavaScript ```js //卡尔的解法(吐槽一下JavaScript的sort和其他语言的不太一样,只想到了拷贝数组去排序再替换原数组来实现nums的[i + 1, nums.length)升序排序) @@ -272,3 +272,4 @@ var nextPermutation = function(nums) { + diff --git "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" index 20ad518295..97bc66d096 100644 --- "a/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" +++ "b/problems/0127.\345\215\225\350\257\215\346\216\245\351\276\231.md" @@ -29,7 +29,7 @@ * 解释:endWord "cog" 不在字典中,所以无法进行转换。 -# 思路 +## 思路 以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。 @@ -97,9 +97,9 @@ public: 当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java public int ladderLength(String beginWord, String endWord, List wordList) { @@ -196,7 +196,7 @@ class Solution { } ``` -## Python +### Python ``` class Solution: @@ -221,7 +221,7 @@ class Solution: queue.append(newWord) return 0 ``` -## Go +### Go ```go func ladderLength(beginWord string, endWord string, wordList []string) int { wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0 @@ -274,7 +274,7 @@ func getCandidates(word string) []string { } ``` -## JavaScript +### JavaScript ```javascript var ladderLength = function(beginWord, endWord, wordList) { // 将wordList转成Set,提高查询速度 @@ -310,7 +310,7 @@ var ladderLength = function(beginWord, endWord, wordList) { }; ``` -## TypeScript +### TypeScript ```typescript function ladderLength( beginWord: string, @@ -364,4 +364,3 @@ function diffonechar(word1: string, word2: string): boolean { - diff --git "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" index 18f1d01eb2..14fa98dc10 100644 --- "a/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" +++ "b/problems/0463.\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277.md" @@ -90,7 +90,7 @@ public: ## 其他语言版本 -Java: +### Java: ```java // 解法一 @@ -191,8 +191,8 @@ class Solution { ``` -Python: -### 解法1: +### Python: + 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 ```python @@ -228,7 +228,8 @@ class Solution: ``` -Go: +### Go: + ```go func islandPerimeter(grid [][]int) int { m, n := len(grid), len(grid[0]) @@ -249,7 +250,8 @@ func islandPerimeter(grid [][]int) int { } ``` -JavaScript: +### JavaScript: + ```javascript //解法一 var islandPerimeter = function(grid) { @@ -305,3 +307,4 @@ var islandPerimeter = function(grid) { + diff --git "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" index ab4bf1525c..f0d3339173 100644 --- "a/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" +++ "b/problems/0657.\346\234\272\345\231\250\344\272\272\350\203\275\345\220\246\350\277\224\345\233\236\345\216\237\347\202\271.md" @@ -29,7 +29,7 @@ -# 思路 +## 思路 这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。 @@ -64,9 +64,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java // 时间复杂度:O(n) @@ -86,7 +86,7 @@ class Solution { } ``` -## Python +### Python ```python # 时间复杂度:O(n) @@ -107,7 +107,7 @@ class Solution: return x == 0 and y == 0 ``` -## Go +### Go ```go func judgeCircle(moves string) bool { @@ -131,7 +131,7 @@ func judgeCircle(moves string) bool { } ``` -## JavaScript +### JavaScript ```js // 时间复杂度:O(n) @@ -150,7 +150,7 @@ var judgeCircle = function(moves) { ``` -## TypeScript +### TypeScript ```ts var judgeCircle = function (moves) { @@ -185,3 +185,4 @@ var judgeCircle = function (moves) { + diff --git "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" index c4d62d9b46..8124cc7eea 100644 --- "a/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" +++ "b/problems/0684.\345\206\227\344\275\231\350\277\236\346\216\245.md" @@ -25,7 +25,7 @@ * edges 中无重复元素 * 给定的图是连通的  -# 思路 +## 思路 这道题目也是并查集基础题目。 @@ -150,9 +150,9 @@ public: 可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -205,7 +205,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -256,7 +256,7 @@ class Solution: return [] ``` -## Go +### Go ```go @@ -312,7 +312,7 @@ func findRedundantConnection(edges [][]int) []int { } ``` -## JavaScript +### JavaScript ```js const n = 1005; @@ -365,3 +365,4 @@ var findRedundantConnection = function(edges) { + diff --git "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" index 8c56afdc08..31b2ad247e 100644 --- "a/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" +++ "b/problems/0685.\345\206\227\344\275\231\350\277\236\346\216\245II.md" @@ -213,9 +213,9 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java @@ -335,7 +335,7 @@ class Solution { } ``` -## Python +### Python ```python @@ -426,7 +426,7 @@ class Solution: return self.getRemoveEdge(edges) ``` -## Go +### Go ```go @@ -527,7 +527,7 @@ func findRedundantDirectedConnection(edges [][]int) []int { ``` -## JavaScript +### JavaScript ```js const N = 1010; // 如题:二维数组大小的在3到1000范围内 @@ -623,3 +623,4 @@ var findRedundantDirectedConnection = function(edges) { + diff --git "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" index b898b7f25c..cc7a7007c7 100644 --- "a/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" +++ "b/problems/1356.\346\240\271\346\215\256\346\225\260\345\255\227\344\272\214\350\277\233\345\210\266\344\270\2131\347\232\204\346\225\260\347\233\256\346\216\222\345\272\217.md" @@ -46,7 +46,7 @@ -# 思路 +## 思路 这道题其实是考察如何计算一个数的二进制中1的数量。 @@ -87,7 +87,7 @@ int bitCount(int n) { 下面我就使用方法二,来做这道题目: -## C++代码 + ```C++ class Solution { @@ -116,9 +116,9 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -151,7 +151,7 @@ class Solution { -## Python +### Python ```python class Solution: @@ -167,7 +167,7 @@ class Solution: return count ``` -## Go +### Go ```go func sortByBits(arr []int) []int { @@ -205,7 +205,7 @@ func bitCount(n int) int { } ``` -## JavaScript +### JavaScript ```js var sortByBits = function(arr) { @@ -227,3 +227,4 @@ var sortByBits = function(arr) { + From 9693ad4e456bf583b96e1f8a5cb965ee0f6b4f63 Mon Sep 17 00:00:00 2001 From: programmercarl <826123027@qq.com> Date: Thu, 27 Jul 2023 15:40:30 +0800 Subject: [PATCH 160/161] Update --- README.md | 25 +- problems/qita/join.md | 249 ++++++++++++++++++ ...16\346\234\254\351\241\271\347\233\256.md" | 15 -- ...72\344\272\214\345\217\211\346\240\221.md" | 2 + ...CM\346\250\241\345\274\217\357\274\237.md" | 4 + ...43\347\240\201\351\243\216\346\240\274.md" | 2 +- 6 files changed, 263 insertions(+), 34 deletions(-) create mode 100644 problems/qita/join.md delete mode 100644 "problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" diff --git a/README.md b/README.md index dd70d8cb6b..e5eb97bc7a 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 > 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 +> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.programmercarl.com/other/gongkaike.html) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 > 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 +> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://www.programmercarl.com/qita/join.html)了解提交代码的方式。 > 9. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! @@ -51,19 +51,12 @@ ## 如何使用该刷题攻略 -电脑端还看不到留言,大家可以在公众号[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png),左下角有「刷题攻略」,这是手机版刷题攻略,看完就会发现有很多录友(代码随想录的朋友们)在文章下留言打卡,这份刷题顺序和题解已经陪伴了上万录友了,同时也说明文章的质量是经过上万人的考验! - -欢迎每一位学习算法的小伙伴加入到这个学习阵营来! - -**目前已经更新了,数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心,八个专题了,正在讲解动态规划!** +按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,最这个专题的归纳总结。 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 - -目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,大部分题目都搭配了20分钟左右的视频讲解**,视频质量很好,口碑很好,大家可以去看看,视频列表:[代码随想录视频讲解](https://www.bilibili.com/video/BV1fA4y1o715)。 - **这里每一篇题解,都是精品,值得仔细琢磨**。 我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 @@ -100,14 +93,11 @@ * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) * 求职 + * [ACM模式练习网站,卡码网](https://kamacoder.com/) * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) + * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) + * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) - * [北京有这些互联网公司,你都知道么?](./problems/前序/北京互联网公司总结.md) - * [上海有这些互联网公司,你都知道么?](./problems/前序/上海互联网公司总结.md) - * [深圳有这些互联网公司,你都知道么?](./problems/前序/深圳互联网公司总结.md) - * [广州有这些互联网公司,你都知道么?](./problems/前序/广州互联网公司总结.md) - * [成都有这些互联网公司,你都知道么?](./problems/前序/成都互联网公司总结.md) - * [杭州有这些互联网公司,你都知道么?](./problems/前序/杭州互联网公司总结.md) * 算法性能分析 * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) @@ -506,7 +496,7 @@ # 关于作者 -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 +大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 @@ -527,4 +517,3 @@
- diff --git a/problems/qita/join.md b/problems/qita/join.md new file mode 100644 index 0000000000..3b3e2d4029 --- /dev/null +++ b/problems/qita/join.md @@ -0,0 +1,249 @@ + + +# 如何在Github上提交PR(pull request) + + +* 如何提交代码 +* 合入不规范 + * 提交信息不规范 + * Markdown 代码格式 + * pull request里的commit数量 + * 代码注释 + * 说明具体是哪种方法 + * 代码规范 + * 代码逻辑 + * 处理冲突 + +以下在 [https://github.com/youngyangyang04/leetcode-master](https://github.com/youngyangyang04/leetcode-master) 上提交pr为为例 + +## 如何合入代码 + +首先来说一说如何合入代码,不少录友还不太会使用github,所以这里也做一下科普。 + +我特意申请一个新的Github账号,给大家做一个示范。 + +需要强调一下,一个commit就只更新一道题目,不要很多题目一起放在一个commit里,那样就很乱。 + +首先LeetCode-Master每天都有更新,如何保持你fork到自己的仓库是最新的版本呢。 + +点击这里Fetch upstream。 + +
+ +点击之后,这里就会显示最新的信息了 +
+ +注意这时是你的远端仓库为最新版本,本地还不是最新的,本地要git pull一下。 + +基于最新的版本,大家在去提交代码。 + +如何提交代码呢,首先把自己的代码提交到自己的fork的远端仓库中,然后open pull request,如图: + +
+ +点击 open pull request之后,就是如下画面,一个pull request有多个commit。 + +
+ +然后就是给pull request 添加备注,pull request是对本次commit的一个总结。如果一个pull request就一个commit,那么就和commit的备注保持一次。 然后点击 create pull request 就可以了 + +
+ +此时你就提交成功了,我会在项目中的pull requests 处理列表里看到你的请求。 +
+ +然后如果你发现自己的代码没有合入多半是有问题,如果有问题都有会在pull request里给出留言的, + +## 注意事项 + +### 提交信息不规范 + + +大家提交代码的时候有两个地方需要写备注,一个是commit,一个是pull request,pull request包好多个commit。 + +commit 说清楚本文件多了哪些修改,而pull request则是对本次合入的所有commit做一个总结性描述。 + +commit备注,举例:添加Rust python3,那么commit备注就是:添加0001两数之和 Rust python3 版本 + +而pull request 如果只有一个commit,那么就也是:添加0001两数之和 Rust python3 版本 + +如果是多个commit ,则把本次commit都描述一遍。 + +### Markdown 语法 + +关于 Markdown 代码格式,例如 添加C++代码,需要有代码块语法 + +\`\`\`C++ +C++代码 +\`\`\` + +例如这个commit,在添加java代码的时候,就直接添加代码 +
+ +正确的格式应该是这样: +
+ +一般发现问题,我也会在代码中给出评论: + +
+ +这样大家也可以学习一些 提交代码的规范方面的知识 + + +有的录友 是添加的代码块语法,但没有标记是哪种语言,这样的话 代码就不会针对某种语言高亮显示了,也比较影响阅读,例如: + +
+ +提交python代码的话,要注释好,是python2还是python3 + +例如这样: + +
+ +当然python2的话,只这么写就行 + +\`\`\`python +python代码 +\`\`\` + +### pull request里的commit数量 + + +有的录友是一个pull request 里有很多commit (一个commit是一道题目的代码)。 + +有的录友是一个pull request 里有有一个commit。 + +
+ +其实如果大家是平时一天写了两三道题目的话,那么分三个commit,一个pull request提交上来就行。 + +一个pull request 一个commit也可以,这样大家就会麻烦一点。 + +但注意一个pull request也不要放太多的commit,一旦有一道题目代码不合格,我没有合入,就这个pull request里影响其他所有代码的合入了。 + +### 代码注释 + +提交的代码最好要有注释,这样也方便读者理解。 + +例如这位录友,在提交Java代码的时候,按照题解的意思对Java版本的代码进行的注释,这就很棒👍 + +
+ +
+ +当然如果大家感觉 已有的代码 不符合以上要求的话,例如 代码思路不够清晰不够规范,注释不够友好,依然欢迎提交优化代码,要记得详细注释哦。 + +
+ +### 说明具体是哪种方法 + +有的题解有两种甚至三四种解法,在添加代码的时候,注释上也清楚具体是哪一种方法的版本。 + +下面这位录友做的就很好 + +
+ + +
+ +有的题解,是一起给出了多道题目的讲解,例如项目中0102.二叉树的层序遍历.md 中有八道题目,那么大家添加代码的时候 应该在代码注释上,或者 直接写上 是哪个题目的代码。 + + +### 代码规范 + + +大家提交代码要规范,当然代码可以在力扣上运行通过是最基本的。 + +虽然我主张没有绝对正确的代码风格,但既然是给LeetCode-Master提交代码,尽量遵循Google编程规范。 + +经常看我的代码的录友应该都知道,我的代码格严格按照 Google C++ 编程规范来的,这样看上去会比较整洁。 + +大家提交代码的时候遇到规范性问题,例如哪里应该由空格,哪里没有空格,可以参考我的代码来。 + +有一位录友在提交代码的时候会把之前的代码 做一下规范性的调整,这就很棒。 + +
+ +**代码规范从你我做起!** + + +### 代码逻辑 + +**提交的代码要按照题解思路来写**。 + +虽然大家自己发挥想象空间是好的,但是题解还是要一脉相承,读者看完题解,发现代码和题解不是一个思路的话,那和重新读代码有啥区别了。 + +所以和题解不是一个思路的代码,除非详细注释了自己的思路 或者 写一段自己代码的描述说明思路和优化的地方,否则我就不会通过合入了哈。 + +大家的代码 最好也将关键地方放上注释,这样有助于别人快速理解你的代码。 + + +### 处理冲突 + +在合入的过程中还要处理冲突的代码, 理解大家代码的思路,解决冲突,然后在力扣提交一下,确保是没问题。 + +例如同一道题目, 一位录友提交了, 我还没处理如何,另一位录友也对这道题也提交了代码,这样就会发生冲突 +
+ +大家提交代码的热情太高了,我有时候根本处理不过来,但我必须当天处理完,否则第二天代码冲突会越来越多。 +
+ +一天晚分别有两位录友提交了 30多道 java代码,全部冲突,解决冲突处理的我脖子疼[哭] + +那么在处理冲突的时候 保留谁的代码,删点谁的代码呢? + +我一定是看谁 代码逻辑和题解一致,代码风格好,注释友好,就保留谁的。 + +所以例如当你想提交Java代码的时候,即使发现该题解已经有Java版本了,只要你的代码写的好,一样可以提交,我评审合格一样可以合入代码库。 + + +### 不要做额外修改 + +确保这种额外文件不要提交。 + +
+ +还有添加不同方法的时候,直接用正文格式写,哪种方法就可以了,不要添加目录 ,例如这样,这样整篇文章目录结构就有影响了。 + +
+ +前面不要加 `## 前序遍历(迭代法)`,直接写`前序遍历(迭代法)`就可以了。 + +当然这里也没有给代码块标记上对应的语言,应该是 + +\`\`\` Go +Go语言代码 +\`\`\` + + +## 对代码保持敬畏 + +有的录友甚至提交的代码并不是本题的代码,虽然我是鼓励大家提交代码的,但是大家贡献代码的时候也要对 自己的代码有敬畏之心,自己的代码是要给很多读者看的。 + +* 代码运行无误 +* 写的够不够简洁 +* 注释清不清晰 +* 备注规不规范 + +这也是培养大家以后协调工作的一种能力。 + +## 优化 + +目前 leetcode-master中大部分题解已经补充了其他语言,但如果你发现了可以优化的地方,依然可以提交PR来优化。 + +甚至发现哪里有语病,也欢迎提交PR来修改,例如下面:就是把【下表】 纠正为【下标】 + +
+ +不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 + +
+ +## 特别注意 + +git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 + +提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 + + + diff --git "a/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" "b/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" deleted file mode 100644 index 76a2c61e8a..0000000000 --- "a/problems/qita/\345\217\202\344\270\216\346\234\254\351\241\271\347\233\256.md" +++ /dev/null @@ -1,15 +0,0 @@ - -优化已有代码 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210821161813.png) - -**push代码之前 一定要 先pull最新代码**,否则提交的pr可能会有删除其他录友代码的操作。 - -一个pr 不要修改过多文件,因为一旦有一个 文件修改有问题,就不能合入,影响其他文件的合入了。 - -git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 - -提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 - - -不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210927113149.png) diff --git "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" index b64464031f..48781eda88 100644 --- "a/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/\345\211\215\345\272\217/ACM\346\250\241\345\274\217\345\246\202\344\275\225\346\236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" @@ -1,6 +1,8 @@ # 力扣上如何自己构造二叉树输入用例? +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + 经常有录友问,二叉树的题目中输入用例在ACM模式下应该怎么构造呢? 力扣上的题目,输入用例就给了一个数组,怎么就能构造成二叉树呢? diff --git "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" "b/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" index 0b9d230ff9..0012f88e67 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\200\344\271\210\346\230\257\346\240\270\345\277\203\344\273\243\347\240\201\346\250\241\345\274\217\357\274\214\344\273\200\344\271\210\345\217\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" @@ -5,6 +5,10 @@ 什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。 + +**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。 + + 而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样: ```CPP diff --git "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" index b508346080..8fd8b40ca9 100644 --- "a/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" +++ "b/problems/\345\211\215\345\272\217/\344\273\243\347\240\201\351\243\216\346\240\274.md" @@ -29,7 +29,7 @@ 这里我简单说一说规范问题。 -**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:googlec++编程规范,就可以领取。(涉及到微信后台的回复,没更改) +**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:编程规范,就可以领取。 **具体的规范要以自己团队风格为主**,融入团队才是最重要的。 From c6cf76bc00dfcb53a89115fe58ce0dde99390a53 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 28 Jul 2023 10:31:10 +0800 Subject: [PATCH 161/161] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\346\225\260\344\271\213\345\222\214.md" | 3 +- ...73\351\231\244\345\205\203\347\264\240.md" | 9 ++- ...47\345\255\220\345\272\217\345\222\214.md" | 14 ++-- ...72\346\227\213\347\237\251\351\230\265.md" | 2 +- ...\346\227\213\347\237\251\351\230\265II.md" | 2 +- ...15\345\220\214\350\267\257\345\276\204.md" | 4 +- ...\345\220\214\350\267\257\345\276\204II.md" | 4 +- "problems/0090.\345\255\220\351\233\206II.md" | 1 + ...11\346\220\234\347\264\242\346\240\221.md" | 4 +- ...14\347\232\204\345\215\225\350\257\215.md" | 3 +- ...\344\275\263\346\227\266\346\234\272IV.md" | 1 - ...7.\345\271\277\346\220\234\347\211\210.md" | 2 +- ...3.\347\247\273\345\212\250\351\233\266.md" | 1 + ...11\346\216\222\350\241\214\347\250\213.md" | 3 +- ...64\346\225\260\346\213\206\345\210\206.md" | 5 +- ...11\345\222\214\345\255\220\351\233\206.md" | 3 +- ...\345\244\247\345\205\203\347\264\240II.md" | 1 - ...66\344\272\214\345\217\211\346\240\221.md" | 3 +- ...00\345\244\247\351\235\242\347\247\257.md" | 23 +++--- ...14\345\210\206\346\237\245\346\211\276.md" | 5 +- ...53\346\211\213\347\273\255\350\264\271.md" | 5 +- ...75\347\232\204\350\267\257\345\276\204.md" | 11 +-- ...47\344\272\272\345\267\245\345\262\233.md" | 7 +- ...04\345\255\227\347\254\246\344\270\262.md" | 7 +- ...55\345\277\203\350\212\202\347\202\271.md" | 3 +- ...30\345\234\250\350\267\257\345\276\204.md" | 1 + ...11\346\255\245\351\223\272\345\236\253.md" | 1 - ...54\345\255\227\347\254\246\344\270\262.md" | 5 +- ...22\346\200\273\347\273\223\347\257\207.md" | 3 +- ...50\346\234\253\346\200\273\347\273\223.md" | 2 + ...37\345\210\227\346\200\273\347\273\223.md" | 1 + ...06\350\256\262\350\247\243\357\274\211.md" | 3 +- ...27\346\263\225\346\250\241\346\235\277.md" | 73 ++++++++++--------- ...47\241\20001\350\203\214\345\214\205-2.md" | 2 + ...50\346\200\273\347\273\223\347\257\207.md" | 1 + 35 files changed, 116 insertions(+), 102 deletions(-) diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index bf1e173ec6..712bc3f0df 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -318,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] { }; ``` -### php: +### PhP: ```php function twoSum(array $nums, int $target): array @@ -501,3 +501,4 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + diff --git "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" index ce9eccf00b..40ee3a2eae 100644 --- "a/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" +++ "b/problems/0027.\347\247\273\351\231\244\345\205\203\347\264\240.md" @@ -152,10 +152,10 @@ public: ## 相关题目推荐 -* 26.删除排序数组中的重复项 -* 283.移动零 -* 844.比较含退格的字符串 -* 977.有序数组的平方 +* [26.删除排序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/) +* [283.移动零](https://leetcode.cn/problems/move-zeroes/) +* [844.比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/) +* [977.有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) ## 其他语言版本 @@ -444,3 +444,4 @@ public class Solution { + diff --git "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" index fe4e4ed3e7..639c54bc7e 100644 --- "a/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" +++ "b/problems/0053.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214.md" @@ -16,11 +16,13 @@ - 输出: 6 - 解释:  连续子数组  [4,-1,2,1] 的和最大,为  6。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -## 暴力解法 +## 思路 + +### 暴力解法 暴力解法的思路,第一层 for 就是设置起始位置,第二层 for 循环遍历数组寻找最大值 @@ -48,7 +50,7 @@ public: 以上暴力的解法 C++勉强可以过,其他语言就不确定了。 -## 贪心解法 +### 贪心解法 **贪心贪的是哪里呢?** @@ -104,7 +106,7 @@ public: 当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。 -## 常见误区 +### 常见误区 误区一: @@ -122,7 +124,7 @@ public: 其实并不会,因为还有一个变量 result 一直在更新 最大的连续和,只要有更大的连续和出现,result 就更新了,那么 result 已经把 4 更新了,后面 连续和变成 3,也不会对最后结果有影响。 -## 动态规划 +### 动态规划 当然本题还可以用动态规划来做,在代码随想录动态规划章节我会详细介绍,如果大家想在想看,可以直接跳转:[动态规划版本详解](https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF) diff --git "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" index a38e8237b9..d855f1a166 100644 --- "a/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" +++ "b/problems/0054.\350\236\272\346\227\213\347\237\251\351\230\265.md" @@ -6,7 +6,7 @@ -## 54.螺旋矩阵 +# 54.螺旋矩阵 [力扣题目链接](https://leetcode.cn/problems/spiral-matrix/) diff --git "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" index 73e9e4daea..78d9385a23 100644 --- "a/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" +++ "b/problems/0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" @@ -688,7 +688,7 @@ public class Solution { } ``` -### Ruby#: +### Ruby: ```ruby def generate_matrix(n) result = Array.new(n) { Array.new(n, 0) } diff --git "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" index 5111e30e10..985c7575af 100644 --- "a/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" +++ "b/problems/0062.\344\270\215\345\220\214\350\267\257\345\276\204.md" @@ -50,9 +50,9 @@ * 1 <= m, n <= 100 * 题目数据保证答案小于等于 2 * 10^9 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" index cb305b4189..3d243a7a8f 100644 --- "a/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" +++ "b/problems/0063.\344\270\215\345\220\214\350\267\257\345\276\204II.md" @@ -46,9 +46,9 @@ * 1 <= m, n <= 100 * obstacleGrid[i][j] 为 0 或 1 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0090.\345\255\220\351\233\206II.md" "b/problems/0090.\345\255\220\351\233\206II.md" index 88c8bdade6..13080cd9e7 100644 --- "a/problems/0090.\345\255\220\351\233\206II.md" +++ "b/problems/0090.\345\255\220\351\233\206II.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 90.子集II [力扣题目链接](https://leetcode.cn/problems/subsets-ii/) diff --git "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" index 368a5747b1..8d58cc5a87 100644 --- "a/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" +++ "b/problems/0096.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" @@ -16,9 +16,9 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 diff --git "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" index 19ccb725a0..f0ff674636 100644 --- "a/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" +++ "b/problems/0151.\347\277\273\350\275\254\345\255\227\347\254\246\344\270\262\351\207\214\347\232\204\345\215\225\350\257\215.md" @@ -433,7 +433,7 @@ class Solution { } ``` -### python: +### Python: (版本一)先删除空白,然后整个反转,最后单词反转。 **因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)** @@ -974,4 +974,3 @@ char * reverseWords(char * s){ - diff --git "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" index d4dc769893..e4c5c48400 100644 --- "a/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" +++ "b/problems/0188.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272IV.md" @@ -529,4 +529,3 @@ impl Solution { - diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" index c20fe4f1aa..cd3ae70d71 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\345\271\277\346\220\234\347\211\210.md" @@ -197,7 +197,6 @@ class Solution { } ``` -## 其他语言版本 ### Python BFS solution ```python @@ -244,3 +243,4 @@ class Solution: ``` + diff --git "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" index ee3f429150..42232cc0c6 100644 --- "a/problems/0283.\347\247\273\345\212\250\351\233\266.md" +++ "b/problems/0283.\347\247\273\345\212\250\351\233\266.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 283. 移动零:动态规划:一样的套路,再求一次完全平方数 [力扣题目链接](https://leetcode.cn/problems/move-zeroes/) diff --git "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" index 3798b48d87..2795e31384 100644 --- "a/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" +++ "b/problems/0332.\351\207\215\346\226\260\345\256\211\346\216\222\350\241\214\347\250\213.md" @@ -31,7 +31,7 @@ ## 算法公开课 -**如果对回溯算法基础还不了解的话,我还特意录制了一期视频,[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) ,相信结合视频再看本篇题解,更有助于大家对本题的理解。** ## 思路 @@ -793,4 +793,3 @@ impl Solution { - diff --git "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" index 3ff8dedb03..cba82f6cae 100644 --- "a/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" +++ "b/problems/0343.\346\225\264\346\225\260\346\213\206\345\210\206.md" @@ -22,9 +22,9 @@ * 说明: 你可以假设 n 不小于 2 且不大于 58。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -473,3 +473,4 @@ object Solution { + diff --git "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" index 0657c010bb..2b2be1037d 100644 --- "a/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" +++ "b/problems/0416.\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206.md" @@ -4,7 +4,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 416. 分割等和子集 +# 416. 分割等和子集 [力扣题目链接](https://leetcode.cn/problems/partition-equal-subset-sum/) @@ -730,4 +730,3 @@ object Solution { - diff --git "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" index 023e4d7e0e..d211a68072 100644 --- "a/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" +++ "b/problems/0503.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240II.md" @@ -294,4 +294,3 @@ impl Solution { - diff --git "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" index 44092aaebc..18839a2638 100644 --- "a/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" +++ "b/problems/0617.\345\220\210\345\271\266\344\272\214\345\217\211\346\240\221.md" @@ -19,7 +19,7 @@ 注意: 合并必须从两个树的根节点开始。 -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 @@ -793,3 +793,4 @@ impl Solution { + diff --git "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" index 37a601bcf8..186f044c40 100644 --- "a/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" +++ "b/problems/0695.\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257.md" @@ -22,7 +22,7 @@ * 输出:6 * 解释:答案不应该是 11 ,因为岛屿只能包含水平或垂直这四个方向上的 1 。 -# 思路 +## 思路 注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。 @@ -38,7 +38,7 @@ * [DFS理论基础](https://programmercarl.com/图论深搜理论基础.html) * [BFS理论基础](https://programmercarl.com/图论广搜理论基础.html) -## DFS +### DFS 很多同学,写dfs其实也是凭感觉来,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过! @@ -134,7 +134,7 @@ public: 以上两种写法的区别,我在题解: [DFS,BDF 你没注意的细节都给你列出来了!LeetCode:200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/)做了详细介绍。 -## BFS +### BFS 关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/图论广搜理论基础.html) @@ -188,9 +188,9 @@ public: ``` -# 其它语言版本 -## Java -### DFS +## 其它语言版本 +### Java +#### DFS ```java // DFS class Solution { @@ -236,7 +236,7 @@ class Solution { ``` -### BFS +#### BFS ```java //BFS class Solution { @@ -290,7 +290,7 @@ class Solution { } } ``` -### DFS 優化(遇到島嶼後,就把他淹沒) +#### DFS 優化(遇到島嶼後,就把他淹沒) ```java //这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。 public int maxAreaOfIsland(int[][] grid) { @@ -319,8 +319,8 @@ public int dfs(int[][] grid,int i,int j){ } ``` -## Python -### BFS +### Python +#### BFS ```python class Solution: def __init__(self): @@ -359,7 +359,7 @@ class Solution: self.count += 1 queue.append((new_x, new_y)) ``` -### DFS +#### DFS ```python class Solution: def __init__(self): @@ -394,3 +394,4 @@ class Solution: + diff --git "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" index 52abf57851..2fc754bb4e 100644 --- "a/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" +++ "b/problems/0704.\344\272\214\345\210\206\346\237\245\346\211\276.md" @@ -150,8 +150,8 @@ public: * [35.搜索插入位置](https://programmercarl.com/0035.搜索插入位置.html) * [34.在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html) -* 69.x 的平方根 -* 367.有效的完全平方数 +* [69.x 的平方根](https://leetcode.cn/problems/sqrtx/) +* [367.有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/) @@ -766,3 +766,4 @@ object Solution { + diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" index 52b2be3bcc..db39864908 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271.md" @@ -39,7 +39,7 @@ 本题相对于[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html),多添加了一个条件就是手续费。 -## 贪心算法 +### 贪心算法 在[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html)中使用贪心策略不用关心具体什么时候买卖,只要收集每天的正利润,最后稳稳的就是最大利润了。 @@ -93,7 +93,7 @@ public: 大家也可以发现,情况三,那块代码是可以删掉的,我是为了让代码表达清晰,所以没有精简。 -## 动态规划 +### 动态规划 我在公众号「代码随想录」里将在下一个系列详细讲解动态规划,所以本题解先给出我的C++代码(带详细注释),感兴趣的同学可以自己先学习一下。 @@ -364,3 +364,4 @@ object Solution { + diff --git "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" index 2ea4ae4760..ec8288c608 100644 --- "a/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" +++ "b/problems/0797.\346\211\200\346\234\211\345\217\257\350\203\275\347\232\204\350\267\257\345\276\204.md" @@ -149,7 +149,7 @@ public: ``` -# 总结 +## 总结 本题是比较基础的深度优先搜索模板题,这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。 @@ -159,7 +159,7 @@ public: ## 其他语言版本 -Java +### Java ```Java // 深度优先遍历 @@ -190,7 +190,8 @@ class Solution { } ``` -Python +### Python + ```python class Solution: def __init__(self): @@ -216,9 +217,9 @@ class Solution: self.path.pop() # 回溯 ``` -### Go + +

- diff --git "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" index 9112fabde2..d78798253a 100644 --- "a/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" +++ "b/problems/0827.\346\234\200\345\244\247\344\272\272\345\267\245\345\262\233.md" @@ -29,7 +29,7 @@ * 输出: 4 * 解释: 没有0可以让我们变成1,面积依然为 4。 -# 思路 +## 思路 本题的一个暴力想法,应该是遍历地图尝试 将每一个 0 改成1,然后去搜索地图中的最大的岛屿面积。 @@ -219,9 +219,9 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -286,4 +286,3 @@ class Solution { - diff --git "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" index ac56f6f990..c7f5220288 100644 --- "a/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/0844.\346\257\224\350\276\203\345\220\253\351\200\200\346\240\274\347\232\204\345\255\227\347\254\246\344\270\262.md" @@ -38,7 +38,7 @@ 本文将给出 空间复杂度O(n)的栈模拟方法 以及空间复杂度是O(1)的双指针方法。 -## 普通方法(使用栈的思路) +### 普通方法(使用栈的思路) 这道题目一看就是要使用栈的节奏,这种匹配(消除)问题也是栈的擅长所在,跟着一起刷题的同学应该知道,在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html),我就已经提过了一次使用栈来做类似的事情了。 @@ -100,7 +100,7 @@ public: * 时间复杂度:O(n + m) * 空间复杂度:O(n + m) -## 优化方法(从后向前双指针) +### 优化方法(从后向前双指针) 当然还可以有使用 O(1) 的空间复杂度来解决该问题。 @@ -289,7 +289,7 @@ class Solution { } ``` -### python +### Python ```python class Solution: @@ -591,3 +591,4 @@ impl Solution { + diff --git "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" index d44c14769e..9bcc7ef9ca 100644 --- "a/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" +++ "b/problems/1791.\346\211\276\345\207\272\346\230\237\345\236\213\345\233\276\347\232\204\344\270\255\345\277\203\350\212\202\347\202\271.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 1791.找出星型图的中心节点 [题目链接](https://leetcode.cn/problems/find-center-of-star-graph/) @@ -55,7 +56,7 @@ public: return -1; } }; -``` +``` 以上代码中没有使用 unordered_map,因为遍历的时候,开辟新空间会浪费时间,而采用 vector,这是 空间换时间的一种策略。 diff --git "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" index 5f1d894333..29e50ab8b0 100644 --- "a/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" +++ "b/problems/1971.\345\257\273\346\211\276\345\233\276\344\270\255\346\230\257\345\220\246\345\255\230\345\234\250\350\267\257\345\276\204.md" @@ -138,3 +138,4 @@ public: + diff --git "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" index 50287ce20b..1982d44943 100644 --- "a/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" +++ "b/problems/\344\270\272\344\272\206\347\273\235\346\235\200\347\274\226\350\276\221\350\267\235\347\246\273\357\274\214\345\215\241\345\260\224\345\201\232\344\272\206\344\270\211\346\255\245\351\223\272\345\236\253.md" @@ -200,4 +200,3 @@ class Solution { - diff --git "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" index 008b7915c3..a3fb7ab330 100644 --- "a/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" +++ "b/problems/\345\211\221\346\214\207Offer58-II.\345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262.md" @@ -142,7 +142,7 @@ class Solution { } ``` -### python: +### Python: (版本一)使用切片 ```python @@ -338,7 +338,7 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) { ``` -### PHP +### PHP: ```php function reverseLeftWords($s, $n) { @@ -418,4 +418,3 @@ impl Solution { - diff --git "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" index c86376d349..e28bfd0472 100644 --- "a/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\345\212\250\346\200\201\350\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" @@ -40,7 +40,7 @@ 好啦,我们再一起回顾一下,动态规划专题中我们都讲了哪些内容。 -## 动划基础 +## 动态规划基础 * [关于动态规划,你该了解这些!](https://programmercarl.com/动态规划理论基础.html) * [动态规划:斐波那契数](https://programmercarl.com/0509.斐波那契数.html) @@ -136,4 +136,3 @@ - diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" index 6127016111..7f6fd114b4 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201030\345\233\236\346\272\257\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -9,6 +9,8 @@ -------------------------- +# 本周小结!(回溯算法系列一) + ## 周一 本周我们正式开始了回溯算法系列,那么首先当然是概述。 diff --git "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" index 31ce955489..e7f8ef86c6 100644 --- "a/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" +++ "b/problems/\346\240\210\344\270\216\351\230\237\345\210\227\346\200\273\347\273\223.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 栈与队列总结篇 ## 栈与队列的理论基础 diff --git "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" index fdb4f58ba4..4291c80c72 100644 --- "a/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" +++ "b/problems/\346\240\271\346\215\256\350\272\253\351\253\230\351\207\215\345\273\272\351\230\237\345\210\227\357\274\210vector\345\216\237\347\220\206\350\256\262\350\247\243\357\274\211.md" @@ -196,7 +196,7 @@ impl Solution{ } ``` -### Go: +### Go Go中slice的`append`操作和C++中vector的扩容机制基本相同。 @@ -213,4 +213,3 @@ Go中slice的`append`操作和C++中vector的扩容机制基本相同。 - diff --git "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" index 59de69dfd7..21b7f93b1c 100644 --- "a/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" +++ "b/problems/\347\256\227\346\263\225\346\250\241\346\235\277.md" @@ -3,8 +3,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+# 算法模板 -## 二分查找法 +## 算法模板 + +### 二分查找法 ```CPP class Solution { @@ -29,7 +32,7 @@ public: ``` -## KMP +### KMP ```CPP void kmp(int* next, const string& s){ @@ -47,7 +50,7 @@ void kmp(int* next, const string& s){ } ``` -## 二叉树 +### 二叉树 二叉树的定义: @@ -60,7 +63,7 @@ struct TreeNode { }; ``` -### 深度优先遍历(递归) +#### 深度优先遍历(递归) 前序遍历(中左右) ```CPP @@ -90,7 +93,7 @@ void traversal(TreeNode* cur, vector& vec) { } ``` -### 深度优先遍历(迭代法) +#### 深度优先遍历(迭代法) 相关题解:[0094.二叉树的中序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0094.二叉树的中序遍历.md) @@ -170,7 +173,7 @@ vector postorderTraversal(TreeNode* root) { return result; } ``` -### 广度优先遍历(队列) +#### 广度优先遍历(队列) 相关题解:[0102.二叉树的层序遍历](https://programmercarl.com/0102.二叉树的层序遍历.html) @@ -208,7 +211,7 @@ vector> levelOrder(TreeNode* root) { * [0111.二叉树的最小深度(迭代法)](https://programmercarl.com/0111.二叉树的最小深度.html) * [0222.完全二叉树的节点个数(迭代法)](https://programmercarl.com/0222.完全二叉树的节点个数.html) -### 二叉树深度 +#### 二叉树深度 ```CPP int getDepth(TreeNode* node) { @@ -217,7 +220,7 @@ int getDepth(TreeNode* node) { } ``` -### 二叉树节点数量 +#### 二叉树节点数量 ```CPP int countNodes(TreeNode* root) { @@ -226,7 +229,7 @@ int countNodes(TreeNode* root) { } ``` -## 回溯算法 +### 回溯算法 ```CPP void backtracking(参数) { if (终止条件) { @@ -243,7 +246,7 @@ void backtracking(参数) { ``` -## 并查集 +### 并查集 ```CPP int n = 1005; // 根据题意而定 @@ -278,9 +281,9 @@ void backtracking(参数) { (持续补充ing) ## 其他语言版本 -JavaScript: +### JavaScript: -## 二分查找法 +#### 二分查找法 使用左闭右闭区间 @@ -322,7 +325,7 @@ var search = function (nums, target) { }; ``` -## KMP +#### KMP ```javascript var kmp = function (next, s) { @@ -340,9 +343,9 @@ var kmp = function (next, s) { } ``` -## 二叉树 +#### 二叉树 -### 深度优先遍历(递归) +##### 深度优先遍历(递归) 二叉树节点定义: @@ -387,7 +390,7 @@ var postorder = function (root, list) { } ``` -### 深度优先遍历(迭代) +##### 深度优先遍历(迭代) 前序遍历(中左右): @@ -447,7 +450,7 @@ var postorderTraversal = function (root) { }; ``` -### 广度优先遍历(队列) +##### 广度优先遍历(队列) ```javascript var levelOrder = function (root) { @@ -469,7 +472,7 @@ var levelOrder = function (root) { }; ``` -### 二叉树深度 +##### 二叉树深度 ```javascript var getDepth = function (node) { @@ -478,7 +481,7 @@ var getDepth = function (node) { } ``` -### 二叉树节点数量 +##### 二叉树节点数量 ```javascript var countNodes = function (root) { @@ -487,7 +490,7 @@ var countNodes = function (root) { } ``` -## 回溯算法 +#### 回溯算法 ```javascript function backtracking(参数) { @@ -505,7 +508,7 @@ function backtracking(参数) { ``` -## 并查集 +#### 并查集 ```javascript let n = 1005; // 根据题意而定 @@ -536,9 +539,9 @@ function backtracking(参数) { } ``` -TypeScript: +### TypeScript: -## 二分查找法 +#### 二分查找法 使用左闭右闭区间 @@ -580,7 +583,7 @@ var search = function (nums: number[], target: number): number { }; ``` -## KMP +#### KMP ```typescript var kmp = function (next: number[], s: number): void { @@ -598,9 +601,9 @@ var kmp = function (next: number[], s: number): void { } ``` -## 二叉树 +#### 二叉树 -### 深度优先遍历(递归) +##### 深度优先遍历(递归) 二叉树节点定义: @@ -650,7 +653,7 @@ var postorder = function (root: TreeNode | null, list: number[]): void { } ``` -### 深度优先遍历(迭代) +##### 深度优先遍历(迭代) 前序遍历(中左右): @@ -710,7 +713,7 @@ var postorderTraversal = function (root: TreeNode | null): number[] { }; ``` -### 广度优先遍历(队列) +##### 广度优先遍历(队列) ```typescript var levelOrder = function (root: TreeNode | null): number[] { @@ -732,7 +735,7 @@ var levelOrder = function (root: TreeNode | null): number[] { }; ``` -### 二叉树深度 +##### 二叉树深度 ```typescript var getDepth = function (node: TreNode | null): number { @@ -741,7 +744,7 @@ var getDepth = function (node: TreNode | null): number { } ``` -### 二叉树节点数量 +##### 二叉树节点数量 ```typescript var countNodes = function (root: TreeNode | null): number { @@ -750,7 +753,7 @@ var countNodes = function (root: TreeNode | null): number { } ``` -## 回溯算法 +#### 回溯算法 ```typescript function backtracking(参数) { @@ -768,7 +771,7 @@ function backtracking(参数) { ``` -## 并查集 +#### 并查集 ```typescript let n: number = 1005; // 根据题意而定 @@ -801,9 +804,9 @@ function backtracking(参数) { Java: +### Python: -Python: -## 二分查找法 +#### 二分查找法 ```python def binarysearch(nums, target): low = 0 @@ -823,7 +826,7 @@ def binarysearch(nums, target): return -1 ``` -## KMP +#### KMP ```python def kmp(self, a, s): diff --git "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" index 019947e536..f60e261be0 100644 --- "a/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" +++ "b/problems/\350\203\214\345\214\205\347\220\206\350\256\272\345\237\272\347\241\20001\350\203\214\345\214\205-2.md" @@ -3,8 +3,10 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 动态规划:01背包理论基础(滚动数组) + ## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!(滚动数组)](https://www.bilibili.com/video/BV1BU4y177kY/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 diff --git "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" index dacd4dee5d..3f2f5c9792 100644 --- "a/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" +++ "b/problems/\351\223\276\350\241\250\346\200\273\347\273\223\347\257\207.md" @@ -3,6 +3,7 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ # 链表总结篇