From 97cd84d47870a8ab2aad30838b9ebc1a9a450099 Mon Sep 17 00:00:00 2001 From: zcxzcxzcx <18810692826@163.com> Date: Sun, 2 Jan 2022 22:16:26 +0800 Subject: [PATCH 01/86] =?UTF-8?q?Create=200093.=E5=A4=8D=E5=8E=9FIP?= =?UTF-8?q?=E5=9C=B0=E5=9D=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化时间复杂度,更好地剪枝(java版本) --- ...\345\216\237IP\345\234\260\345\235\200.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) 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 3e7cd1ade2..67bb1bbe03 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" @@ -304,6 +304,48 @@ class Solution { return true; } } + +//方法二:比上面的方法时间复杂度低,更好地剪枝,优化时间复杂度 +class Solution { + List result = new ArrayList(); + StringBuilder stringBuilder = new StringBuilder(); + + public List restoreIpAddresses(String s) { + restoreIpAddressesHandler(s, 0, 0); + return result; + } + + // number表示stringbuilder中ip段的数量 + public void restoreIpAddressesHandler(String s, int start, int number) { + // 如果start等于s的长度并且ip段的数量是4,则加入结果集,并返回 + if (start == s.length() && number == 4) { + result.add(stringBuilder.toString()); + return; + } + // 如果start等于s的长度但是ip段的数量不为4,或者ip段的数量为4但是start小于s的长度,则直接返回 + if (start == s.length() || number == 4) { + return; + } + // 剪枝:ip段的长度最大是3,并且ip段处于[0,255] + for (int i = start; i < s.length() && i - start < 3 && Integer.parseInt(s.substring(start, i + 1)) >= 0 + && Integer.parseInt(s.substring(start, i + 1)) <= 255; i++) { + // 如果ip段的长度大于1,并且第一位为0的话,continue + if (i + 1 - start > 1 && s.charAt(start) - '0' == 0) { + continue; + } + stringBuilder.append(s.substring(start, i + 1)); + // 当stringBuilder里的网段数量小于3时,才会加点;如果等于3,说明已经有3段了,最后一段不需要再加点 + if (number < 3) { + stringBuilder.append("."); + } + number++; + restoreIpAddressesHandler(s, i + 1, number); + number--; + // 删除当前stringBuilder最后一个网段,注意考虑点的数量的问题 + stringBuilder.delete(start + number, i + number + 2); + } + } +} ``` ## python From 35bbec3e298c03cc69582f486baa961102e0f583 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Mon, 17 Jan 2022 12:36:22 +0800 Subject: [PATCH 02/86] Update --- ...\345\220\214\350\267\257\345\276\204II.md" | 29 +++--- ...11\346\220\234\347\264\242\346\240\221.md" | 10 +-- ...64\346\225\260\346\213\206\345\210\206.md" | 26 +++--- ...47\241\20001\350\203\214\345\214\205-1.md" | 88 +++++++++---------- 4 files changed, 75 insertions(+), 78 deletions(-) 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 490b6b5c95..1bcc11cddc 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" @@ -4,7 +4,7 @@

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

-## 63. 不同路径 II +# 63. 不同路径 II [力扣题目链接](https://leetcode-cn.com/problems/unique-paths-ii/) @@ -22,23 +22,22 @@ ![](https://img-blog.csdnimg.cn/20210111204939971.png) -输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] -输出:2 +* 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] +* 输出:2 解释: -3x3 网格的正中间有一个障碍物。 -从左上角到右下角一共有 2 条不同的路径: -1. 向右 -> 向右 -> 向下 -> 向下 -2. 向下 -> 向下 -> 向右 -> 向右 +* 3x3 网格的正中间有一个障碍物。 +* 从左上角到右下角一共有 2 条不同的路径: + 1. 向右 -> 向右 -> 向下 -> 向下 + 2. 向下 -> 向下 -> 向右 -> 向右 示例 2: ![](https://img-blog.csdnimg.cn/20210111205857918.png) -输入:obstacleGrid = [[0,1],[0,0]] -输出:1 +* 输入:obstacleGrid = [[0,1],[0,0]] +* 输出:1 提示: - * m == obstacleGrid.length * n == obstacleGrid[i].length * 1 <= m, n <= 100 @@ -171,7 +170,7 @@ public: ## 其他语言版本 -Java: +### Java ```java class Solution { @@ -199,7 +198,7 @@ class Solution { ``` -Python: +### Python ```python class Solution: @@ -262,7 +261,7 @@ class Solution: ``` -Go: +### Go ```go func uniquePathsWithObstacles(obstacleGrid [][]int) int { @@ -308,8 +307,8 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int { ``` -Javascript -``` Javascript +### Javascript +```Javascript var uniquePathsWithObstacles = function(obstacleGrid) { const m = obstacleGrid.length const n = obstacleGrid[0].length 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 d4b8d0245f..48826697cf 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" @@ -4,7 +4,7 @@

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

-## 96.不同的二叉搜索树 +# 96.不同的二叉搜索树 [力扣题目链接](https://leetcode-cn.com/problems/unique-binary-search-trees/) @@ -163,7 +163,7 @@ public: ## 其他语言版本 -Java: +### Java ```Java class Solution { public int numTrees(int n) { @@ -184,7 +184,7 @@ class Solution { } ``` -Python: +### Python ```python class Solution: def numTrees(self, n: int) -> int: @@ -196,7 +196,7 @@ class Solution: return dp[-1] ``` -Go: +### Go ```Go func numTrees(n int)int{ dp:=make([]int,n+1) @@ -210,7 +210,7 @@ func numTrees(n int)int{ } ``` -Javascript: +### Javascript ```Javascript const numTrees =(n) => { let dp = new Array(n+1).fill(0); 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 5d11f670f7..471a7ab765 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" @@ -4,23 +4,22 @@

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

-## 343. 整数拆分 +# 343. 整数拆分 [力扣题目链接](https://leetcode-cn.com/problems/integer-break/) 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。 示例 1: -输入: 2 -输出: 1 - -\解释: 2 = 1 + 1, 1 × 1 = 1。 +* 输入: 2 +* 输出: 1 +* 解释: 2 = 1 + 1, 1 × 1 = 1。 示例 2: -输入: 10 -输出: 36 -解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。 -说明: 你可以假设 n 不小于 2 且不大于 58。 +* 输入: 10 +* 输出: 36 +* 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。 +* 说明: 你可以假设 n 不小于 2 且不大于 58。 ## 思路 @@ -193,7 +192,7 @@ public: ## 其他语言版本 -Java: +### Java ```Java class Solution { public int integerBreak(int n) { @@ -212,7 +211,7 @@ class Solution { } ``` -Python: +### Python ```python class Solution: def integerBreak(self, n: int) -> int: @@ -226,7 +225,8 @@ class Solution: dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j])) return dp[n] ``` -Go: + +### Go ```golang func integerBreak(n int) int { /** @@ -256,7 +256,7 @@ func max(a,b int) int{ } ``` -Javascript: +### Javascript ```Javascript var integerBreak = function(n) { let dp = new Array(n + 1).fill(0) 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 4367aff9bb..6ff32017cf 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" @@ -3,11 +3,12 @@

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

+ # 动态规划:关于01背包问题,你该了解这些! 这周我们正式开始讲解背包问题! -背包问题的经典资料当然是:背包九讲。在公众号「代码随想录」后台回复:背包九讲,就可以获得背包九讲的PDF。 +背包问题的经典资料当然是:背包九讲。在公众号「代码随想录」后台回复:背包九讲,就可以获得背包九讲的pdf。 但说实话,背包九讲对于小白来说确实不太友好,看起来还是有点费劲的,而且都是伪代码理解起来也吃力。 @@ -32,7 +33,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, ## 01 背包 -有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 +有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。 ![动态规划-背包问题](https://img-blog.csdnimg.cn/20210117175428387.jpg) @@ -40,7 +41,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目, 这样其实是没有从底向上去思考,而是习惯性想到了背包,那么暴力的解法应该是怎么样的呢? -每一件物品其实只有两个状态,取或者不取,所以可以使用回溯法搜索出所有的情况,那么时间复杂度就是$O(2^n)$,这里的n表示物品数量。 +每一件物品其实只有两个状态,取或者不取,所以可以使用回溯法搜索出所有的情况,那么时间复杂度就是$o(2^n)$,这里的n表示物品数量。 **所以暴力的解法是指数级别的时间复杂度。进而才需要动态规划的解法来进行优化!** @@ -109,7 +110,7 @@ for (int j = 0 ; j < weight[0]; j++) { // 当然这一步,如果把dp数组 dp[0][j] = 0; } // 正序遍历 -for (int j = weight[0]; j <= bagWeight; j++) { +for (int j = weight[0]; j <= bagweight; j++) { dp[0][j] = value[0]; } ``` @@ -135,8 +136,8 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 ``` // 初始化 dp -vector> dp(weight.size(), vector(bagWeight + 1, 0)); -for (int j = weight[0]; j <= bagWeight; j++) { +vector> dp(weight.size(), vector(bagweight + 1, 0)); +for (int j = weight[0]; j <= bagweight; j++) { dp[0][j] = value[0]; } @@ -160,7 +161,7 @@ for (int j = weight[0]; j <= bagWeight; j++) { ``` // weight数组的大小 就是物品个数 for(int i = 1; i < weight.size(); i++) { // 遍历物品 - for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 if (j < weight[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); @@ -174,7 +175,7 @@ for(int i = 1; i < weight.size(); i++) { // 遍历物品 ``` // weight数组的大小 就是物品个数 -for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 +for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 for(int i = 1; i < weight.size(); i++) { // 遍历物品 if (j < weight[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); @@ -219,32 +220,32 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括 主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。 -## 完整C++测试代码 +## 完整c++测试代码 -```CPP +```cpp void test_2_wei_bag_problem1() { vector weight = {1, 3, 4}; vector value = {15, 20, 30}; - int bagWeight = 4; + int bagweight = 4; // 二维数组 - vector> dp(weight.size(), vector(bagWeight + 1, 0)); + vector> dp(weight.size(), vector(bagweight + 1, 0)); // 初始化 - for (int j = weight[0]; j <= bagWeight; j++) { + for (int j = weight[0]; j <= bagweight; j++) { dp[0][j] = value[0]; } // weight数组的大小 就是物品个数 for(int i = 1; i < weight.size(); i++) { // 遍历物品 - for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 + for(int j = 0; j <= bagweight; j++) { // 遍历背包容量 if (j < weight[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]); } } - cout << dp[weight.size() - 1][bagWeight] << endl; + cout << dp[weight.size() - 1][bagweight] << endl; } int main() { @@ -267,48 +268,45 @@ int main() { ## 其他语言版本 -Java: +### java ```java - public static void main(String[] args) { + public static void main(string[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; - int bagSize = 4; - testWeightBagProblem(weight, value, bagSize); + int bagsize = 4; + testweightbagproblem(weight, value, bagsize); } - public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ - int wLen = weight.length, value0 = 0; + public static void testweightbagproblem(int[] weight, int[] value, int bagsize){ + int wlen = weight.length, value0 = 0; //定义dp数组:dp[i][j]表示背包容量为j时,前i个物品能获得的最大价值 - int[][] dp = new int[wLen + 1][bagSize + 1]; + int[][] dp = new int[wlen + 1][bagsize + 1]; //初始化:背包容量为0时,能获得的价值都为0 - for (int i = 0; i <= wLen; i++){ + for (int i = 0; i <= wlen; i++){ dp[i][0] = value0; } //遍历顺序:先遍历物品,再遍历背包容量 - for (int i = 1; i <= wLen; i++){ - for (int j = 1; j <= bagSize; j++){ + for (int i = 1; i <= wlen; i++){ + for (int j = 1; j <= bagsize; j++){ if (j < weight[i - 1]){ dp[i][j] = dp[i - 1][j]; }else{ - dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]); + dp[i][j] = math.max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]); } } } //打印dp数组 - for (int i = 0; i <= wLen; i++){ - for (int j = 0; j <= bagSize; j++){ - System.out.print(dp[i][j] + " "); + for (int i = 0; i <= wlen; i++){ + for (int j = 0; j <= bagsize; j++){ + system.out.print(dp[i][j] + " "); } - System.out.print("\n"); + system.out.print("\n"); } } ``` - - - -Python: +### python ```python def test_2_wei_bag_problem1(bag_size, weight, value) -> int: rows, cols = len(weight), bag_size + 1 @@ -343,26 +341,26 @@ if __name__ == "__main__": ``` -Go: +### go ```go -func test_2_wei_bag_problem1(weight, value []int, bagWeight int) int { +func test_2_wei_bag_problem1(weight, value []int, bagweight int) int { // 定义dp数组 dp := make([][]int, len(weight)) for i, _ := range dp { - dp[i] = make([]int, bagWeight+1) + dp[i] = make([]int, bagweight+1) } // 初始化 - for j := bagWeight; j >= weight[0]; j-- { + for j := bagweight; j >= weight[0]; j-- { dp[0][j] = dp[0][j-weight[0]] + value[0] } // 递推公式 for i := 1; i < len(weight); i++ { //正序,也可以倒序 - for j := weight[i];j<= bagWeight ; j++ { + for j := weight[i];j<= bagweight ; j++ { dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i]]+value[i]) } } - return dp[len(weight)-1][bagWeight] + return dp[len(weight)-1][bagweight] } func max(a,b int) int { @@ -379,19 +377,19 @@ func main() { } ``` -javaScript: +### javascript ```js -function testWeightBagProblem (wight, value, size) { +function testweightbagproblem (wight, value, size) { const len = wight.length, - dp = Array.from({length: len + 1}).map( - () => Array(size + 1).fill(0) + dp = array.from({length: len + 1}).map( + () => array(size + 1).fill(0) ); for(let i = 1; i <= len; i++) { for(let j = 0; j <= size; j++) { if(wight[i - 1] <= j) { - dp[i][j] = Math.max( + dp[i][j] = math.max( dp[i - 1][j], value[i - 1] + dp[i - 1][j - wight[i - 1]] ) From 850939e40745fb6cf03c056caf0dbb88cae620ec Mon Sep 17 00:00:00 2001 From: leeeeeeewii <54872662+leeeeeeewii@users.noreply.github.com> Date: Wed, 19 Jan 2022 22:48:45 +0800 Subject: [PATCH 03/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20python=20go=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...220\206\350\256\272\345\237\272\347\241\200.md" | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 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 109aa1ed70..a4fefa2b83 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" @@ -195,10 +195,20 @@ class ListNode { ``` Python: - +```python +class ListNode: + def __init__(self, val, next=None): + self.val = val + self.next = next +``` Go: - +```go +type ListNode struct { + Val int + Next *ListNode +} +``` From e0c6492d6e141d8ff20a1bd2a2cc4d0337b88286 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 20 Jan 2022 14:56:16 +0800 Subject: [PATCH 04/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880028.=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0strStr.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.\345\256\236\347\216\260strStr.md" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index f0b56719ae..5f1fa137b7 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -929,6 +929,83 @@ var strStr = function (haystack, needle) { }; ``` +TypeScript版本: + +> 前缀表统一减一 + +```typescript +function strStr(haystack: string, needle: string): number { + function getNext(str: string): number[] { + let next: number[] = []; + let j: number = -1; + next[0] = j; + for (let i = 1, length = str.length; i < length; i++) { + while (j >= 0 && str[i] !== str[j + 1]) { + j = next[j]; + } + if (str[i] === str[j + 1]) { + j++; + } + next[i] = j; + } + return next; + } + if (needle.length === 0) return 0; + let next: number[] = getNext(needle); + let j: number = -1; + for (let i = 0, length = haystack.length; i < length; i++) { + while (j >= 0 && haystack[i] !== needle[j + 1]) { + j = next[j]; + } + if (haystack[i] === needle[j + 1]) { + if (j === needle.length - 2) { + return i - j - 1; + } + j++; + } + } + return -1; +}; +``` + +> 前缀表不减一 + +```typescript +// 不减一版本 +function strStr(haystack: string, needle: string): number { + function getNext(str: string): number[] { + let next: number[] = []; + let j: number = 0; + next[0] = j; + for (let i = 1, length = str.length; i < length; i++) { + while (j > 0 && str[i] !== str[j]) { + j = next[j - 1]; + } + if (str[i] === str[j]) { + j++; + } + next[i] = j; + } + return next; + } + if (needle.length === 0) return 0; + let next: number[] = getNext(needle); + let j: number = 0; + for (let i = 0, length = haystack.length; i < length; i++) { + while (j > 0 && haystack[i] !== needle[j]) { + j = next[j - 1]; + } + if (haystack[i] === needle[j]) { + if (j === needle.length - 1) { + return i - j; + } + j++; + } + } + return -1; +} +``` + Swift 版本 > 前缀表统一减一 From 59c95ff24d60ec7809aa80c632b3728291892077 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Thu, 20 Jan 2022 08:29:02 +0000 Subject: [PATCH 05/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200343.=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E6=8B=86=E5=88=86.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 5d11f670f7..9882c6ea7e 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" @@ -271,5 +271,40 @@ var integerBreak = function(n) { }; ``` +C: +```c +//初始化DP数组 +int *initDP(int num) { + int* dp = (int*)malloc(sizeof(int) * (num + 1)); + int i; + for(i = 0; i < num + 1; ++i) { + dp[i] = 0; + } + return dp; +} + +//取三数最大值 +int max(int num1, int num2, int num3) { + int tempMax = num1 > num2 ? num1 : num2; + return tempMax > num3 ? tempMax : num3; +} + +int integerBreak(int n){ + int *dp = initDP(n); + //初始化dp[2]为1 + dp[2] = 1; + + int i; + for(i = 3; i <= n; ++i) { + int j; + for(j = 1; j < i - 1; ++j) { + //取得上次循环:dp[i],原数相乘,或j*dp[]i-j] 三数中的最大值 + dp[i] = max(dp[i], j * (i - j), j * dp[i - j]); + } + } + return dp[n]; +} +``` + -----------------------
From 7aba93c8e1bbb3f623c1f67f9f2d7f794aaefd78 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 20 Jan 2022 17:15:09 +0800 Subject: [PATCH 06/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880459.=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) 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 9c74f4a7bb..ccfb485cbb 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" @@ -361,7 +361,65 @@ var repeatedSubstringPattern = function (s) { }; ``` +TypeScript: +> 前缀表统一减一 + +```typescript +function repeatedSubstringPattern(s: string): boolean { + function getNext(str: string): number[] { + let next: number[] = []; + let j: number = -1; + next[0] = j; + for (let i = 1, length = str.length; i < length; i++) { + while (j >= 0 && str[i] !== str[j + 1]) { + j = next[j]; + } + if (str[i] === str[j + 1]) { + j++; + } + next[i] = j; + } + return next; + } + + let next: number[] = getNext(s); + let sLength: number = s.length; + let nextLength: number = next.length; + let suffixLength: number = next[nextLength - 1] + 1; + if (suffixLength > 0 && sLength % (sLength - suffixLength) === 0) return true; + return false; +}; +``` + +> 前缀表不减一 + +```typescript +function repeatedSubstringPattern(s: string): boolean { + function getNext(str: string): number[] { + let next: number[] = []; + let j: number = 0; + next[0] = j; + for (let i = 1, length = str.length; i < length; i++) { + while (j > 0 && str[i] !== str[j]) { + j = next[j - 1]; + } + if (str[i] === str[j]) { + j++; + } + next[i] = j; + } + return next; + } + + let next: number[] = getNext(s); + let sLength: number = s.length; + let nextLength: number = next.length; + let suffixLength: number = next[nextLength - 1]; + if (suffixLength > 0 && sLength % (sLength - suffixLength) === 0) return true; + return false; +}; +``` -----------------------
From 02fd6b8f9a9dc49af1612762c80a439e0a1f5e04 Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Fri, 21 Jan 2022 10:45:10 +0800 Subject: [PATCH 07/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0257.=20=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=20Swift=20=E7=89=88=E6=9C=AC?= 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" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) 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 cb837d8781..9ccd81d8ae 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" @@ -581,6 +581,97 @@ var binaryTreePaths = function(root) { }; ``` +Swift: + +递归法: + +```swift +func binaryTreePaths(_ root: TreeNode?) -> [String] { + var result = Array() + var path = Array() + guard let root = root else { + return result + } + traversal(root, &path, &result) + return result + } + + func traversal(_ cur: TreeNode, _ path: inout Array, _ result: inout Array) { + path.append(cur.val) + // 递归终止条件:到达叶子节点 + if cur.left == nil && cur.right == nil { + var pathString = "" + // 处理 path 前面的元素 + for i in 0.. [String] { + // 保存树的遍历节点 + var treeStack = [TreeNode]() + // 保存遍历路径的节点 + var pathStack = [String]() + // 保存最终路径集合 + var result = [String]() + guard let root = root else { + return result + } + treeStack.append(root) + pathStack.append(String(root.val)) + while !treeStack.isEmpty { + let node = treeStack.removeLast() + let path = pathStack.removeLast() + + // 遇到叶子节点 + if node.left == nil && node.right == nil { + result.append(path) + } + + if let rightNode = node.right { + treeStack.append(rightNode) + let tmp = path + "->" + String(rightNode.val) + pathStack.append(tmp) + } + + if let leftNode = node.left { + treeStack.append(leftNode) + let tmp = path + "->" + String(leftNode.val) + pathStack.append(tmp) + } + } + + return result + } +``` + ----------------------- From d29fa29a87e8e5b2739f5033423cc6666d1ced42 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 21 Jan 2022 14:16:33 +0800 Subject: [PATCH 08/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880232.=E7=94=A8?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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 4edba2f201..33ce81147d 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" @@ -348,7 +348,44 @@ MyQueue.prototype.empty = function() { }; ``` +TypeScript: + +```typescript +class MyQueue { + private stackIn: number[] + private stackOut: number[] + constructor() { + this.stackIn = []; + this.stackOut = []; + } + + push(x: number): void { + this.stackIn.push(x); + } + + pop(): number { + if (this.stackOut.length === 0) { + while (this.stackIn.length > 0) { + this.stackOut.push(this.stackIn.pop()!); + } + } + return this.stackOut.pop()!; + } + + peek(): number { + let temp: number = this.pop(); + this.stackOut.push(temp); + return temp; + } + + empty(): boolean { + return this.stackIn.length === 0 && this.stackOut.length === 0; + } +} +``` + Swift: + ```swift class MyQueue { From 7037fb2877da6406d690e7bf3160da5c2b5dc673 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 21 Jan 2022 17:25:00 +0800 Subject: [PATCH 09/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880225.=E7=94=A8?= =?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) 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 fdb544a6c2..961fad3889 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" @@ -598,7 +598,80 @@ MyStack.prototype.empty = function() { ``` +TypeScript: + +版本一:使用两个队列模拟栈 + +```typescript +class MyStack { + private queue: number[]; + private tempQueue: number[]; + constructor() { + this.queue = []; + this.tempQueue = []; + } + + push(x: number): void { + this.queue.push(x); + } + + pop(): number { + for (let i = 0, length = this.queue.length - 1; i < length; i++) { + this.tempQueue.push(this.queue.shift()!); + } + let res: number = this.queue.pop()!; + let temp: number[] = this.queue; + this.queue = this.tempQueue; + this.tempQueue = temp; + return res; + } + + top(): number { + let res: number = this.pop(); + this.push(res); + return res; + } + + empty(): boolean { + return this.queue.length === 0; + } +} +``` + +版本二:使用一个队列模拟栈 + +```typescript +class MyStack { + private queue: number[]; + constructor() { + this.queue = []; + } + + push(x: number): void { + this.queue.push(x); + } + + pop(): number { + for (let i = 0, length = this.queue.length - 1; i < length; i++) { + this.queue.push(this.queue.shift()!); + } + return this.queue.shift()!; + } + + top(): number { + let res: number = this.pop(); + this.push(res); + return res; + } + + empty(): boolean { + return this.queue.length === 0; + } +} +``` + Swift + ```Swift // 定义一个队列数据结构 class Queue { From f54af7ff39bb3c2d268eb1daff3bd2f5c8ca8b72 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 21 Jan 2022 20:57:58 +0800 Subject: [PATCH 10/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) 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 9e4046d4d5..95d62e42a0 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" @@ -283,8 +283,60 @@ var isValid = function(s) { }; ``` +TypeScript: + +版本一:普通版 + +```typescript +function isValid(s: string): boolean { + let helperStack: string[] = []; + for (let i = 0, length = s.length; i < length; i++) { + let x: string = s[i]; + switch (x) { + case '(': + helperStack.push(')'); + break; + case '[': + helperStack.push(']'); + break; + case '{': + helperStack.push('}'); + break; + default: + if (helperStack.pop() !== x) return false; + break; + } + } + return helperStack.length === 0; +}; +``` + +版本二:优化版 + +```typescript +function isValid(s: string): boolean { + type BracketMap = { + [index: string]: string; + } + let helperStack: string[] = []; + let bracketMap: BracketMap = { + '(': ')', + '[': ']', + '{': '}' + } + for (let i of s) { + if (bracketMap.hasOwnProperty(i)) { + helperStack.push(bracketMap[i]); + } else if (i !== helperStack.pop()) { + return false; + } + } + return helperStack.length === 0; +}; +``` Swift + ```swift func isValid(_ s: String) -> Bool { var stack = [String.Element]() From 61b0ddef35b07f7b459d9bfac774691bcf6cec98 Mon Sep 17 00:00:00 2001 From: coolcty Date: Fri, 21 Jan 2022 17:52:43 +0100 Subject: [PATCH 11/86] =?UTF-8?q?Update=200031.=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\270\213\344\270\200\344\270\252\346\216\222\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 84bf3e607c..2219e24dba 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" @@ -81,7 +81,7 @@ public: for (int j = nums.size() - 1; j > i; j--) { if (nums[j] > nums[i]) { swap(nums[j], nums[i]); - sort(nums.begin() + i + 1, nums.end()); + reverse(nums.begin() + i + 1, nums.end()); return; } } From 2ec4867125e60381b9ded470a59ad89d0b8401d1 Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Sat, 22 Jan 2022 10:26:18 +0800 Subject: [PATCH 12/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20404.=20=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C=20Swift=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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 ddbd100e2c..09272052de 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" @@ -373,6 +373,54 @@ var sumOfLeftLeaves = function(root) { ``` +## Swift + +**递归法** +```swift +func sumOfLeftLeaves(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + + let leftValue = sumOfLeftLeaves(root.left) + let rightValue = sumOfLeftLeaves(root.right) + + var midValue: Int = 0 + if root.left != nil && root.left?.left == nil && root.left?.right == nil { + midValue = root.left!.val + } + + let sum = midValue + leftValue + rightValue + return sum +} +``` +**迭代法** +```swift +func sumOfLeftLeaves(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + + var stack = Array() + stack.append(root) + var sum = 0 + + while !stack.isEmpty { + let lastNode = stack.removeLast() + + if lastNode.left != nil && lastNode.left?.left == nil && lastNode.left?.right == nil { + sum += lastNode.left!.val + } + if let right = lastNode.right { + stack.append(right) + } + if let left = lastNode.left { + stack.append(left) + } + } + return sum +} +``` From 26c7fd202afc2aac492c2db589a3c4af5af3fcf3 Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Sat, 22 Jan 2022 11:38:28 +0800 Subject: [PATCH 13/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20513.=20=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=20Swift?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\247\222\347\232\204\345\200\274.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) 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 99f42d34cd..84ed393207 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" @@ -433,6 +433,74 @@ var findBottomLeftValue = function(root) { }; ``` +## Swift + +递归版本: + +```swift +var maxLen = -1 +var maxLeftValue = 0 +func findBottomLeftValue_2(_ root: TreeNode?) -> Int { + traversal(root, 0) + return maxLeftValue +} + +func traversal(_ root: TreeNode?, _ deep: Int) { + guard let root = root else { + return + } + + if root.left == nil && root.right == nil { + if deep > maxLen { + maxLen = deep + maxLeftValue = root.val + } + return + } + + if root.left != nil { + traversal(root.left, deep + 1) + } + + if root.right != nil { + traversal(root.right, deep + 1) + } + return +} +``` +层序遍历: + +```swift +func findBottomLeftValue(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + + var queue = [root] + var result = 0 + + while !queue.isEmpty { + let size = queue.count + for i in 0.. Date: Sat, 22 Jan 2022 17:45:40 +0800 Subject: [PATCH 14/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=881047.=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.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 d6eefd078a..9a0bb1c1bd 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" @@ -267,8 +267,32 @@ var removeDuplicates = function(s) { }; ``` +TypeScript: + +```typescript +function removeDuplicates(s: string): string { + const helperStack: string[] = []; + let i: number = 0; + while (i < s.length) { + let top: string = helperStack[helperStack.length - 1]; + if (top === s[i]) { + helperStack.pop(); + } else { + helperStack.push(s[i]); + } + i++; + } + let res: string = ''; + while (helperStack.length > 0) { + res = helperStack.pop() + res; + } + return res; +}; +``` + C: 方法一:使用栈 + ```c char * removeDuplicates(char * s){ //求出字符串长度 From 2b33b45170c22eb4b900bc85ff04ad5ce0a5c2e2 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Sat, 22 Jan 2022 10:39:07 +0000 Subject: [PATCH 15/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200096.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= =?UTF-8?q?.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?= 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" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 d4b8d0245f..d6f7aca7f2 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" @@ -227,7 +227,34 @@ const numTrees =(n) => { }; ``` +C: +```c +//开辟dp数组 +int *initDP(int n) { + int *dp = (int *)malloc(sizeof(int) * (n + 1)); + int i; + for(i = 0; i <= n; ++i) + dp[i] = 0; + return dp; +} + +int numTrees(int n){ + //开辟dp数组 + int *dp = initDP(n); + //将dp[0]设为1 + dp[0] = 1; + int i, j; + for(i = 1; i <= n; ++i) { + for(j = 1; j <= i; ++j) { + //递推公式:dp[i] = d[i] + 根为j时左子树种类个数 * 根为j时右子树种类个数 + dp[i] += dp[j - 1] * dp[i - j]; + } + } + + return dp[n]; +} +``` -----------------------
From 718e67acef29a7f21f7914d0b1e175b2b52a4063 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Sat, 22 Jan 2022 10:41:09 +0000 Subject: [PATCH 16/86] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200096.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= =?UTF-8?q?.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\272\214\345\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d6f7aca7f2..d334a29c86 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" @@ -247,7 +247,7 @@ int numTrees(int n){ int i, j; for(i = 1; i <= n; ++i) { for(j = 1; j <= i; ++j) { - //递推公式:dp[i] = d[i] + 根为j时左子树种类个数 * 根为j时右子树种类个数 + //递推公式:dp[i] = dp[i] + 根为j时左子树种类个数 * 根为j时右子树种类个数 dp[i] += dp[j - 1] * dp[i - j]; } } From 3fb923694db6cdd6a1b38f16cfcc294bfa415876 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 22 Jan 2022 22:20:24 +0800 Subject: [PATCH 17/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880150.=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?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) 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 f44703f18f..f4dad8231a 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" @@ -210,6 +210,71 @@ var evalRPN = function(tokens) { }; ``` +TypeScript: + +普通版: + +```typescript +function evalRPN(tokens: string[]): number { + let helperStack: number[] = []; + let temp: number; + let i: number = 0; + while (i < tokens.length) { + let t: string = tokens[i]; + switch (t) { + case '+': + temp = helperStack.pop()! + helperStack.pop()!; + helperStack.push(temp); + break; + case '-': + temp = helperStack.pop()!; + temp = helperStack.pop()! - temp; + helperStack.push(temp); + break; + case '*': + temp = helperStack.pop()! * helperStack.pop()!; + helperStack.push(temp); + break; + case '/': + temp = helperStack.pop()!; + temp = Math.trunc(helperStack.pop()! / temp); + helperStack.push(temp); + break; + default: + helperStack.push(Number(t)); + break; + } + i++; + } + return helperStack.pop()!; +}; +``` + +优化版: + +```typescript +function evalRPN(tokens: string[]): number { + const helperStack: number[] = []; + const operatorMap: Map number> = new Map([ + ['+', (a, b) => a + b], + ['-', (a, b) => a - b], + ['/', (a, b) => Math.trunc(a / b)], + ['*', (a, b) => a * b], + ]); + let a: number, b: number; + for (let t of tokens) { + if (operatorMap.has(t)) { + b = helperStack.pop()!; + a = helperStack.pop()!; + helperStack.push(operatorMap.get(t)!(a, b)); + } else { + helperStack.push(Number(t)); + } + } + return helperStack.pop()!; +}; +``` + python3 ```python From b3078ef51c321e386f5900e4f43c3b2c68dd0ca0 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Sun, 23 Jan 2022 22:59:59 +0800 Subject: [PATCH 18/86] =?UTF-8?q?Update=200435.=E6=97=A0=E9=87=8D=E5=8F=A0?= =?UTF-8?q?=E5=8C=BA=E9=97=B4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新Java版本代码 --- ...15\345\217\240\345\214\272\351\227\264.md" | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 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 118360bc2c..389443d1be 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" @@ -183,28 +183,22 @@ public: ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { - if (intervals.length < 2) return 0; - - Arrays.sort(intervals, new Comparator() { - @Override - public int compare(int[] o1, int[] o2) { - if (o1[1] != o2[1]) { - return Integer.compare(o1[1],o2[1]); - } else { - return Integer.compare(o1[0],o2[0]); - } - } + Arrays.sort(intervals, (a, b) -> { + if (a[0] == a[0]) return a[1] - b[1]; + return a[0] - b[0]; }); - int count = 1; - int edge = intervals[0][1]; - for (int i = 1; i < intervals.length; i++) { - if (edge <= intervals[i][0]){ - count ++; //non overlap + 1 + int count = 0; + int edge = Integer.MIN_VALUE; + for (int i = 0; i < intervals.length; i++) { + if (edge <= intervals[i][0]) { edge = intervals[i][1]; + } else { + count++; } } - return intervals.length - count; + + return count; } } ``` From 00728847e17558cda0d76ff87e544b5a0a5e2dfb Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Mon, 24 Jan 2022 11:36:06 +0800 Subject: [PATCH 19/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=E3=80=810113.=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=80=BB=E5=92=8C=20II=20Swift=20=E7=89=88=E6=9C=AC?= 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" | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) 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 910e57c86f..5ec8ffd051 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" @@ -766,7 +766,124 @@ let pathSum = function(root, targetSum) { }; ``` +## Swift +0112.路径总和 + +**递归** +```swift +func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { + guard let root = root else { + return false + } + + return traversal(root, targetSum - root.val) +} + +func traversal(_ cur: TreeNode?, _ count: Int) -> Bool { + if cur?.left == nil && cur?.right == nil && count == 0 { + return true + } + + if cur?.left == nil && cur?.right == nil { + return false + } + + if let leftNode = cur?.left { + if traversal(leftNode, count - leftNode.val) { + return true + } + } + + if let rightNode = cur?.right { + if traversal(rightNode, count - rightNode.val) { + return true + } + } + + return false +} +``` +**迭代** +```swift +func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { + guard let root = root else { + return false + } + + var stack = Array<(TreeNode, Int)>() + stack.append((root, root.val)) + + while !stack.isEmpty { + let node = stack.removeLast() + + if node.0.left == nil && node.0.right == nil && targetSum == node.1 { + return true + } + + if let rightNode = node.0.right { + stack.append((rightNode, node.1 + rightNode.val)) + } + + if let leftNode = node.0.left { + stack.append((leftNode, node.1 + leftNode.val)) + } + } + + return false +} +``` + +0113.路径总和 II + +**递归** + +```swift +var result = [[Int]]() +var path = [Int]() +func pathSum(_ root: TreeNode?, _ targetSum: Int) -> [[Int]] { + result.removeAll() + path.removeAll() + guard let root = root else { + return result + } + path.append(root.val) + traversal(root, count: targetSum - root.val) + return result + +} + +func traversal(_ cur: TreeNode?, count: Int) { + var count = count + // 遇到了叶子节点且找到了和为targetSum的路径 + if cur?.left == nil && cur?.right == nil && count == 0 { + result.append(path) + return + } + + // 遇到叶子节点而没有找到合适的边,直接返回 + if cur?.left == nil && cur?.right == nil{ + return + } + + if let leftNode = cur?.left { + path.append(leftNode.val) + count -= leftNode.val + traversal(leftNode, count: count)// 递归 + count += leftNode.val// 回溯 + path.removeLast()// 回溯 + } + + if let rightNode = cur?.right { + path.append(rightNode.val) + count -= rightNode.val + traversal(rightNode, count: count)// 递归 + count += rightNode.val// 回溯 + path.removeLast()// 回溯 + } + return +} +``` From ac9f8c612033d87221e1fc79ce093211a1f5570b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 24 Jan 2022 15:51:26 +0800 Subject: [PATCH 20/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) 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 d9788f6328..6e8039f403 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" @@ -418,7 +418,56 @@ var maxSlidingWindow = function (nums, k) { }; ``` +TypeScript: + +```typescript +function maxSlidingWindow(nums: number[], k: number): number[] { + /** 单调递减队列 */ + class MonoQueue { + private queue: number[]; + constructor() { + this.queue = []; + }; + /** 入队:value如果大于队尾元素,则将队尾元素删除,直至队尾元素大于value,或者队列为空 */ + public enqueue(value: number): void { + let back: number | undefined = this.queue[this.queue.length - 1]; + while (back !== undefined && back < value) { + this.queue.pop(); + back = this.queue[this.queue.length - 1]; + } + this.queue.push(value); + }; + /** 出队:只有当队头元素等于value,才出队 */ + public dequeue(value: number): void { + let top: number | undefined = this.top(); + if (top !== undefined && top === value) { + this.queue.shift(); + } + } + public top(): number | undefined { + return this.queue[0]; + } + } + const helperQueue: MonoQueue = new MonoQueue(); + let i: number = 0, + j: number = 0; + let resArr: number[] = []; + while (j < k) { + helperQueue.enqueue(nums[j++]); + } + resArr.push(helperQueue.top()!); + while (j < nums.length) { + helperQueue.enqueue(nums[j]); + helperQueue.dequeue(nums[i]); + resArr.push(helperQueue.top()!); + j++, i++; + } + return resArr; +}; +``` + Swift: + ```Swift /// 双向链表 class DoublyListNode { From a12ad31f46c082f35a88441d65ef322740d12520 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 24 Jan 2022 16:03:36 +0800 Subject: [PATCH 21/86] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E8=A7=84=E8=8C=83=E5=8C=96js=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 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 6e8039f403..adf3548cbb 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" @@ -395,26 +395,49 @@ func maxSlidingWindow(nums []int, k int) []int { Javascript: ```javascript +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ var maxSlidingWindow = function (nums, k) { - // 队列数组(存放的是元素下标,为了取值方便) - const q = []; - // 结果数组 - const ans = []; - for (let i = 0; i < nums.length; i++) { - // 若队列不为空,且当前元素大于等于队尾所存下标的元素,则弹出队尾 - while (q.length && nums[i] >= nums[q[q.length - 1]]) { - q.pop(); - } - // 入队当前元素下标 - q.push(i); - // 判断当前最大值(即队首元素)是否在窗口中,若不在便将其出队 - if (q[0] <= i - k) { - q.shift(); - } - // 当达到窗口大小时便开始向结果中添加数据 - if (i >= k - 1) ans.push(nums[q[0]]); - } - return ans; + class MonoQueue { + queue; + constructor() { + this.queue = []; + } + enqueue(value) { + let back = this.queue[this.queue.length - 1]; + while (back !== undefined && back < value) { + this.queue.pop(); + back = this.queue[this.queue.length - 1]; + } + this.queue.push(value); + } + dequeue(value) { + let front = this.front(); + if (front === value) { + this.queue.shift(); + } + } + front() { + return this.queue[0]; + } + } + let helperQueue = new MonoQueue(); + let i = 0, j = 0; + let resArr = []; + while (j < k) { + helperQueue.enqueue(nums[j++]); + } + resArr.push(helperQueue.front()); + while (j < nums.length) { + helperQueue.enqueue(nums[j]); + helperQueue.dequeue(nums[i]); + resArr.push(helperQueue.front()); + i++, j++; + } + return resArr; }; ``` From 9272063cf2627a6334cea807cd3de2e7166da1af Mon Sep 17 00:00:00 2001 From: YDLIN <1924723909@qq.com> Date: Mon, 24 Jan 2022 16:42:47 +0800 Subject: [PATCH 22/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=20=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20Swift=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\344\272\214\345\217\211\346\240\221.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 b4679c7d1f..44c74f8903 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" @@ -401,6 +401,33 @@ struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize){ } ``` +## Swift +```swift +func constructMaximumBinaryTree(_ nums: inout [Int]) -> TreeNode? { + return traversal(&nums, 0, nums.count) +} + +func traversal(_ nums: inout [Int], _ left: Int, _ right: Int) -> TreeNode? { + if left >= right { + return nil + } + + var maxValueIndex = left + for i in (left + 1).. nums[maxValueIndex] { + maxValueIndex = i + } + } + + let root = TreeNode(nums[maxValueIndex]) + + root.left = traversal(&nums, left, maxValueIndex) + root.right = traversal(&nums, maxValueIndex + 1, right) + return root +} +``` + + -----------------------
From fee948b2af53b7bfc583fbbe5c43cd98c04e462f Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Mon, 24 Jan 2022 08:57:02 +0000 Subject: [PATCH 23/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200005.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2.md=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E8=A7=A3=E6=B3=95?= 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" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) 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 f204a60784..588afc22f6 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" @@ -462,7 +462,56 @@ var longestPalindrome = function(s) { }; ``` +## C +动态规划: +```c +//初始化dp数组,全部初始为false +bool **initDP(int strLen) { + bool **dp = (bool **)malloc(sizeof(bool *) * strLen); + int i, j; + for(i = 0; i < strLen; ++i) { + dp[i] = (bool *)malloc(sizeof(bool) * strLen); + for(j = 0; j < strLen; ++j) + dp[i][j] = false; + } + return dp; +} +char * longestPalindrome(char * s){ + //求出字符串长度 + int strLen = strlen(s); + //初始化dp数组,元素初始化为false + bool **dp = initDP(strLen); + int maxLength = 0, left = 0, right = 0; + + //从下到上,从左到右遍历 + int i, j; + for(i = strLen - 1; i >= 0; --i) { + for(j = i; j < strLen; ++j) { + //若当前i与j所指字符一样 + if(s[i] == s[j]) { + //若i、j指向相邻字符或同一字符,则为回文字符串 + if(j - i <= 1) + dp[i][j] = true; + //若i+1与j-1所指字符串为回文字符串,则i、j所指字符串为回文字符串 + else if(dp[i + 1][j - 1]) + dp[i][j] = true; + } + //若新的字符串的长度大于之前的最大长度,进行更新 + if(dp[i][j] && j - i + 1 > maxLength) { + maxLength = j - i + 1; + left = i; + right = j; + } + } + } + //复制回文字符串,并返回 + char *ret = (char*)malloc(sizeof(char) * (maxLength + 1)); + memcpy(ret, s + left, maxLength); + ret[maxLength] = 0; + return ret; +} +``` -----------------------
From 60c3a27ce9537d7361e4927b21129bf77be507ba Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 24 Jan 2022 17:39:34 +0800 Subject: [PATCH 24/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880347.=E5=89=8D?= =?UTF-8?q?K=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...3\230\351\242\221\345\205\203\347\264\240.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 8bd774e94d..1d6a358bfc 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" @@ -358,6 +358,22 @@ PriorityQueue.prototype.compare = function(index1, index2) { } ``` +TypeScript: + +```typescript +function topKFrequent(nums: number[], k: number): number[] { + const countMap: Map = new Map(); + for (let num of nums) { + countMap.set(num, (countMap.get(num) || 0) + 1); + } + // tS没有最小堆的数据结构,所以直接对整个数组进行排序,取前k个元素 + return [...countMap.entries()] + .sort((a, b) => b[1] - a[1]) + .slice(0, k) + .map(i => i[0]); +}; +``` + ----------------------- From de37c44f4fbc6b368f1afe6a51e508c949ff9271 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 24 Jan 2022 19:41:17 +0800 Subject: [PATCH 25/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\206\350\256\272\345\237\272\347\241\200.md" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 cc89985092..009e8276f9 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" @@ -227,7 +227,23 @@ function TreeNode(val, left, right) { } ``` +TypeScript: + +```typescript +class TreeNode { + public val: number; + public left: TreeNode | null; + public right: TreeNode | null; + constructor(val?: number, left?: TreeNode, right?: TreeNode) { + this.val = val === undefined ? 0 : val; + this.left = left === undefined ? null : left; + this.right = right === undefined ? null : right; + } +} +``` + Swift: + ```Swift class TreeNode { var value: T From a488a3414a8c0e3408678f8393b116837e6240ea Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 24 Jan 2022 20:25:45 +0800 Subject: [PATCH 26/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) 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 4beed6503c..ba13fc74c5 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" @@ -358,7 +358,51 @@ var postorderTraversal = function(root) { }; ``` +TypeScript: + +```typescript +// 前序遍历 +function preorderTraversal(node: TreeNode | null): number[] { + function traverse(node: TreeNode | null, res: number[]): void { + if (node === null) return; + res.push(node.val); + traverse(node.left, res); + traverse(node.right, res); + } + const res: number[] = []; + traverse(node, res); + return res; +} + +// 中序遍历 +function inorderTraversal(node: TreeNode | null): number[] { + function traverse(node: TreeNode | null, res: number[]): void { + if (node === null) return; + traverse(node.left, res); + res.push(node.val); + traverse(node.right, res); + } + const res: number[] = []; + traverse(node, res); + return res; +} + +// 后序遍历 +function postorderTraversal(node: TreeNode | null): number[] { + function traverse(node: TreeNode | null, res: number[]): void { + if (node === null) return; + traverse(node.left, res); + traverse(node.right, res); + res.push(node.val); + } + const res: number[] = []; + traverse(node, res); + return res; +} +``` + C: + ```c //前序遍历: void preOrderTraversal(struct TreeNode* root, int* ret, int* returnSize) { From fec8e0882a771ea5866e0611c01874d6850654e6 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 24 Jan 2022 20:27:15 +0800 Subject: [PATCH 27/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84js=E7=89=88=E6=9C=AC?= 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" | 34 ------------------- 1 file changed, 34 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 ba13fc74c5..c481fd1181 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" @@ -270,40 +270,6 @@ func postorderTraversal(root *TreeNode) (res []int) { } ``` -javaScript: - -```js - -前序遍历: - -var preorderTraversal = function(root, res = []) { - if (!root) return res; - res.push(root.val); - preorderTraversal(root.left, res) - preorderTraversal(root.right, res) - return res; -}; - -中序遍历: - -var inorderTraversal = function(root, res = []) { - if (!root) return res; - inorderTraversal(root.left, res); - res.push(root.val); - inorderTraversal(root.right, res); - return res; -}; - -后序遍历: - -var postorderTraversal = function(root, res = []) { - if (!root) return res; - postorderTraversal(root.left, res); - postorderTraversal(root.right, res); - res.push(root.val); - return res; -}; -``` Javascript版本: 前序遍历: From 1efb77e96310d884eb2b8647232dd1586f4f0afb Mon Sep 17 00:00:00 2001 From: zucong Date: Tue, 25 Jan 2022 14:33:12 +0800 Subject: [PATCH 28/86] =?UTF-8?q?=E4=BC=98=E5=8C=96=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20Go=E7=89=88=E6=9C=AC=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= 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" | 101 +++++++----------- 1 file changed, 41 insertions(+), 60 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 910e57c86f..b3e121babd 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" @@ -531,82 +531,63 @@ class solution: ```go //递归法 /** - * definition for a binary tree node. - * type treenode struct { - * val int - * left *treenode - * right *treenode + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode * } */ -func haspathsum(root *treenode, targetsum int) bool { - var flage bool //找没找到的标志 - if root==nil{ - return flage - } - pathsum(root,0,targetsum,&flage) - return flage -} -func pathsum(root *treenode, sum int,targetsum int,flage *bool){ - sum+=root.val - if root.left==nil&&root.right==nil&&sum==targetsum{ - *flage=true - return +func hasPathSum(root *TreeNode, targetSum int) bool { + if root == nil { + return false } - if root.left!=nil&&!(*flage){//左节点不为空且还没找到 - pathsum(root.left,sum,targetsum,flage) - } - if root.right!=nil&&!(*flage){//右节点不为空且没找到 - pathsum(root.right,sum,targetsum,flage) + + targetSum -= root.Val // 将targetSum在遍历每层的时候都减去本层节点的值 + if root.Left == nil && root.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果 + return true } + return hasPathSum(root.Left, targetSum) || hasPathSum(root.Right, targetSum) // 否则递归找 } ``` -113 递归法 +113. 路径总和 II ```go /** - * definition for a binary tree node. - * type treenode struct { - * val int - * left *treenode - * right *treenode + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode * } */ -func pathsum(root *treenode, targetsum int) [][]int { - var result [][]int//最终结果 - if root==nil{ - return result - } - var sumnodes []int//经过路径的节点集合 - haspathsum(root,&sumnodes,targetsum,&result) +func pathSum(root *TreeNode, targetSum int) [][]int { + result := make([][]int, 0) + traverse(root, &result, new([]int), targetSum) return result } -func haspathsum(root *treenode,sumnodes *[]int,targetsum int,result *[][]int){ - *sumnodes=append(*sumnodes,root.val) - if root.left==nil&&root.right==nil{//叶子节点 - fmt.println(*sumnodes) - var sum int - var number int - for k,v:=range *sumnodes{//求该路径节点的和 - sum+=v - number=k - } - tempnodes:=make([]int,number+1)//新的nodes接受指针里的值,防止最终指针里的值发生变动,导致最后的结果都是最后一个sumnodes的值 - for k,v:=range *sumnodes{ - tempnodes[k]=v - } - if sum==targetsum{ - *result=append(*result,tempnodes) - } - } - if root.left!=nil{ - haspathsum(root.left,sumnodes,targetsum,result) - *sumnodes=(*sumnodes)[:len(*sumnodes)-1]//回溯 + +func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { + if node == nil { // 这个判空也可以挪到递归遍历左右子树时去判断 + return } - if root.right!=nil{ - haspathsum(root.right,sumnodes,targetsum,result) - *sumnodes=(*sumnodes)[:len(*sumnodes)-1]//回溯 + + targetSum -= node.Val // 将targetSum在遍历每层的时候都减去本层节点的值 + *currPath = append(*currPath, node.Val) // 把当前节点放到路径记录里 + + if node.Left == nil && node.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果 + // 不能直接将currPath放到result里面, 因为currPath是共享的, 每次遍历子树时都会被修改 + pathCopy := make([]int, len(*currPath)) + for i, element := range *currPath { + pathCopy[i] = element + } + *result = append(*result, pathCopy) // 将副本放到结果集里 } + + traverse(node.Left, result, currPath, targetSum) + traverse(node.Right, result, currPath, targetSum) + *currPath = (*currPath)[:len(*currPath)-1] // 当前节点遍历完成, 从路径记录里删除掉 } ``` From 3a46ffdad4cfcc2bc3695db93f4760b9ae70b4d2 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Tue, 25 Jan 2022 12:54:50 +0000 Subject: [PATCH 29/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200005.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E5=9B=9E=E6=96=87=E5=AD=90=E4=B8=B2.md=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E5=8F=8C=E6=8C=87=E9=92=88=E8=A7=A3=E6=B3=95?= 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" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) 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 588afc22f6..99458825e5 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" @@ -513,5 +513,41 @@ char * longestPalindrome(char * s){ } ``` +双指针: +```c +int left, maxLength; +void extend(char *str, int i, int j, int size) { + while(i >= 0 && j < size && str[i] == str[j]) { + //若当前子字符串长度大于最长的字符串长度,进行更新 + if(j - i + 1 > maxLength) { + maxLength = j - i + 1; + left = i; + } + //左指针左移,右指针右移。扩大搜索范围 + ++j, --i; + } +} + +char * longestPalindrome(char * s){ + left = right = maxLength = 0; + int size = strlen(s); + + int i; + for(i = 0; i < size; ++i) { + //长度为单数的子字符串 + extend(s, i, i, size); + //长度为双数的子字符串 + extend(s, i, i + 1, size); + } + + //复制子字符串 + char *subStr = (char *)malloc(sizeof(char) * (maxLength + 1)); + memcpy(subStr, s + left, maxLength); + subStr[maxLength] = 0; + + return subStr; +} +``` + -----------------------
From d40a15eccca9212f701c4602a514168ab52fecf6 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 26 Jan 2022 10:40:30 +0800 Subject: [PATCH 30/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) 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 4cb94cb5e3..ba38726bea 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" @@ -454,6 +454,61 @@ var postorderTraversal = function(root, res = []) { }; ``` +TypeScript: + +```typescript +// 前序遍历(迭代法) +function preorderTraversal(root: TreeNode | null): number[] { + if (root === null) return []; + let res: number[] = []; + let helperStack: TreeNode[] = []; + let curNode: TreeNode = root; + helperStack.push(curNode); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + res.push(curNode.val); + if (curNode.right !== null) helperStack.push(curNode.right); + if (curNode.left !== null) helperStack.push(curNode.left); + } + return res; +}; + +// 中序遍历(迭代法) +function inorderTraversal(root: TreeNode | null): number[] { + let helperStack: TreeNode[] = []; + let res: number[] = []; + if (root === null) return res; + let curNode: TreeNode | null = root; + while (curNode !== null || helperStack.length > 0) { + if (curNode !== null) { + helperStack.push(curNode); + curNode = curNode.left; + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + curNode = curNode.right; + } + } + return res; +}; + +// 后序遍历(迭代法) +function postorderTraversal(root: TreeNode | null): number[] { + let helperStack: TreeNode[] = []; + let res: number[] = []; + let curNode: TreeNode; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + res.push(curNode.val); + if (curNode.left !== null) helperStack.push(curNode.left); + if (curNode.right !== null) helperStack.push(curNode.right); + } + return res.reverse(); +}; +``` + Swift: > 迭代法前序遍历 From 9d09b11d56722748e00d3647cdcd228793baaa66 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 26 Jan 2022 09:26:03 +0000 Subject: [PATCH 31/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200143.=E9=87=8D?= =?UTF-8?q?=E6=8E=92=E9=93=BE=E8=A1=A8.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\346\216\222\351\223\276\350\241\250.md" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" index 4ea9cb97a9..0062262328 100644 --- "a/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" +++ "b/problems/0143.\351\207\215\346\216\222\351\223\276\350\241\250.md" @@ -439,7 +439,75 @@ var reorderList = function(head, s = [], tmp) { } ``` +### C +方法三:反转链表 +```c +//翻转链表 +struct ListNode *reverseList(struct ListNode *head) { + if(!head) + return NULL; + struct ListNode *preNode = NULL, *curNode = head; + while(curNode) { + //创建tempNode记录curNode->next(即将被更新) + struct ListNode* tempNode = curNode->next; + //将curNode->next指向preNode + curNode->next = preNode; + //更新preNode为curNode + preNode = curNode; + //curNode更新为原链表中下一个元素 + curNode = tempNode; + } + return preNode; +} + +void reorderList(struct ListNode* head){ + //slow用来截取到链表的中间节点(第一个链表的最后节点),每次循环跳一个节点。fast用来辅助,每次循环跳两个节点 + struct ListNode *fast = head, *slow = head; + while(fast && fast->next && fast->next->next) { + //fast每次跳两个节点 + fast = fast->next->next; + //slow每次跳一个节点 + slow = slow->next; + } + //将slow->next后的节点翻转 + struct ListNode *sndLst = reverseList(slow->next); + //将第一个链表与第二个链表断开 + slow->next = NULL; + //因为插入从curNode->next开始,curNode刚开始已经head。所以fstList要从head->next开始 + struct ListNode *fstLst = head->next; + struct ListNode *curNode = head; + + int count = 0; + //当第一个链表和第二个链表中都有节点时循环 + while(sndLst && fstLst) { + //count为奇数,插入fstLst中的节点 + if(count % 2) { + curNode->next = fstLst; + fstLst = fstLst->next; + } + //count为偶数,插入sndList的节点 + else { + curNode->next = sndLst; + sndLst = sndLst->next; + } + //设置下一个节点 + curNode = curNode->next; + //更新count + ++count; + } + //若两个链表fstList和sndLst中还有节点,将其放入链表 + if(fstLst) { + curNode->next = fstLst; + } + if(sndLst) { + curNode->next = sndLst; + } + + //返回链表 + return head; +} +``` ----------------------- From 7ed2a3aef7b10b48fe7f8127f185f4a6efc9952c Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 26 Jan 2022 19:18:23 +0800 Subject: [PATCH 32/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= 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" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) 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 2ff848179f..f6edf586ea 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" @@ -522,7 +522,75 @@ var postorderTraversal = function(root, res = []) { ``` +TypeScript: + +```typescript +// 前序遍历(迭代法) +function preorderTraversal(root: TreeNode | null): number[] { + let helperStack: (TreeNode | null)[] = []; + let res: number[] = []; + let curNode: TreeNode | null; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + } + } + return res; +}; + +// 中序遍历(迭代法) +function inorderTraversal(root: TreeNode | null): number[] { + let helperStack: (TreeNode | null)[] = []; + let res: number[] = []; + let curNode: TreeNode | null; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + } + } + return res; +}; +// 后序遍历(迭代法) +function postorderTraversal(root: TreeNode | null): number[] { + let helperStack: (TreeNode | null)[] = []; + let res: number[] = []; + let curNode: TreeNode | null; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + } + } + return res; +}; +``` -----------------------
From 878231551829fdd9deea2f2457eb09b794d7ed65 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 26 Jan 2022 21:20:36 +0800 Subject: [PATCH 33/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880102.=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?.md=20&=20107.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82?= =?UTF-8?q?=E6=AC=A1=E9=81=8D=E5=8E=86=20II=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\345\272\217\351\201\215\345\216\206.md" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) 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 1fb9b633af..b108c8f090 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" @@ -246,7 +246,35 @@ var levelOrder = function(root) { ``` +TypeScript: + +```typescript +function levelOrder(root: TreeNode | null): number[][] { + let helperQueue: TreeNode[] = []; + let res: number[][] = []; + let tempArr: number[] = []; + if (root !== null) helperQueue.push(root); + let curNode: TreeNode; + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + curNode = helperQueue.shift()!; + tempArr.push(curNode.val); + if (curNode.left !== null) { + helperQueue.push(curNode.left); + } + if (curNode.right !== null) { + helperQueue.push(curNode.right); + } + } + res.push(tempArr); + tempArr = []; + } + return res; +}; +``` + Swift: + ```swift func levelOrder(_ root: TreeNode?) -> [[Int]] { var res = [[Int]]() @@ -454,7 +482,31 @@ var levelOrderBottom = function(root) { }; ``` +TypeScript: + +```typescript +function levelOrderBottom(root: TreeNode | null): number[][] { + let helperQueue: TreeNode[] = []; + let resArr: number[][] = []; + let tempArr: number[] = []; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + tempArr.push(tempNode.val); + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + resArr.push(tempArr); + tempArr = []; + } + return resArr.reverse(); +}; +``` + Swift: + ```swift func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { var res = [[Int]]() From bb02fb957bfef34ffc7270e70b4cf41b829ed3a6 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 26 Jan 2022 23:13:07 +0000 Subject: [PATCH 34/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200052.N=E7=9A=87?= =?UTF-8?q?=E5=90=8EII.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0052.N\347\232\207\345\220\216II.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) 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 78531798c4..67e439ca81 100644 --- "a/problems/0052.N\347\232\207\345\220\216II.md" +++ "b/problems/0052.N\347\232\207\345\220\216II.md" @@ -143,5 +143,65 @@ var totalNQueens = function(n) { return count; }; ``` + +C +```c +//path[i]为在i行,path[i]列上存在皇后 +int *path; +int pathTop; +int answer; +//检查当前level行index列放置皇后是否合法 +int isValid(int index, int level) { + int i; + //updater为若斜角存在皇后,其所应在的列 + //用来检查左上45度是否存在皇后 + int lCornerUpdater = index - level; + //用来检查右上135度是否存在皇后 + int rCornerUpdater = index + level; + for(i = 0; i < pathTop; ++i) { + //path[i] == index检查index列是否存在皇后 + //检查斜角皇后:只要path[i] == updater,就说明当前位置不可放置皇后。 + //path[i] == lCornerUpdater检查左上角45度是否有皇后 + //path[i] == rCornerUpdater检查右上角135度是否有皇后 + if(path[i] == index || path[i] == lCornerUpdater || path[i] == rCornerUpdater) + return 0; + //更新updater指向下一行对应的位置 + ++lCornerUpdater; + --rCornerUpdater; + } + return 1; +} + +//回溯算法:level为当前皇后行数 +void backTracking(int n, int level) { + //若path中元素个数已经为n,则证明有一种解法。answer+1 + if(pathTop == n) { + ++answer; + return; + } + + int i; + for(i = 0; i < n; ++i) { + //若当前level行,i列是合法的放置位置。就将i放入path中 + if(isValid(i, level)) { + path[pathTop++] = i; + backTracking(n, level + 1); + //回溯 + --pathTop; + } + } +} + +int totalNQueens(int n){ + answer = 0; + pathTop = 0; + path = (int *)malloc(sizeof(int) * n); + + backTracking(n, 0); + + return answer; +} +``` + -----------------------
From 023ec6900a759e030712edf5362b65569935df75 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Thu, 27 Jan 2022 13:43:43 +0000 Subject: [PATCH 35/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200129.=E6=B1=82?= =?UTF-8?q?=E6=A0=B9=E5=88=B0=E5=8F=B6=E5=AD=90=E8=8A=82=E7=82=B9=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E4=B9=8B=E5=92=8C.md=20C=E8=AF=AD=E8=A8=80=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\345\255\227\344\271\213\345\222\214.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 d07d524462..980779c20c 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" @@ -289,7 +289,33 @@ var sumNumbers = function(root) { }; ``` +C: +```c +//sum记录总和 +int sum; +void traverse(struct TreeNode *node, int val) { + //更新val为根节点到当前节点的和 + val = val * 10 + node->val; + //若当前节点为叶子节点,记录val + if(!node->left && !node->right) { + sum+=val; + return; + } + //若有左/右节点,遍历左/右节点 + if(node->left) + traverse(node->left, val); + if(node->right) + traverse(node->right, val); +} + +int sumNumbers(struct TreeNode* root){ + sum = 0; + + traverse(root, 0); + return sum; +} +``` -----------------------
From 7fe4ea3fbc3d93665e2ed5663a1c41c6f21d5874 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Thu, 27 Jan 2022 19:32:30 +0000 Subject: [PATCH 36/86] =?UTF-8?q?Add=200209.=E9=95=BF=E5=BA=A6=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84.md=20C=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\346\225\260\347\273\204.md" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" index 17422ca06b..dc1d9f18d0 100644 --- "a/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" +++ "b/problems/0209.\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204.md" @@ -330,5 +330,55 @@ def min_sub_array_len(target, nums) end ``` +C: +暴力解法: +```c +int minSubArrayLen(int target, int* nums, int numsSize){ + //初始化最小长度为INT_MAX + int minLength = INT_MAX; + int sum; + + int left, right; + for(left = 0; left < numsSize; ++left) { + //每次遍历都清零sum,计算当前位置后和>=target的子数组的长度 + sum = 0; + //从left开始,sum中添加元素 + for(right = left; right < numsSize; ++right) { + sum += nums[right]; + //若加入当前元素后,和大于target,则更新minLength + if(sum >= target) { + int subLength = right - left + 1; + minLength = minLength < subLength ? minLength : subLength; + } + } + } + //若minLength不为INT_MAX,则返回minLnegth + return minLength == INT_MAX ? 0 : minLength; +} +``` + +滑动窗口: +```c +int minSubArrayLen(int target, int* nums, int numsSize){ + //初始化最小长度为INT_MAX + int minLength = INT_MAX; + int sum = 0; + + int left = 0, right = 0; + //右边界向右扩展 + for(; right < numsSize; ++right) { + sum += nums[right]; + //当sum的值大于等于target时,保存长度,并且收缩左边界 + while(sum >= target) { + int subLength = right - left + 1; + minLength = minLength < subLength ? minLength : subLength; + sum -= nums[left++]; + } + } + //若minLength不为INT_MAX,则返回minLnegth + return minLength == INT_MAX ? 0 : minLength; +} +``` + -----------------------
From e177bca526cd33ff4dce70b188f2214598941b0e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 27 Jan 2022 22:35:30 +0800 Subject: [PATCH 37/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88199.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=8F=B3=E8=A7=86=E5=9B=BE=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 b108c8f090..18e19227b7 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" @@ -709,7 +709,28 @@ var rightSideView = function(root) { }; ``` +TypeScript: + +```typescript +function rightSideView(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (i === length - 1) resArr.push(tempNode.val); + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resArr; +}; +``` + Swift: + ```swift func rightSideView(_ root: TreeNode?) -> [Int] { var res = [Int]() From daf710bde6eb82f2c5b2a87254ca9860d79af80e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 11:32:57 +0800 Subject: [PATCH 38/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88637.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=B9=B3=E5=9D=87=E5=80=BC?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 18e19227b7..9ff4f2e964 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" @@ -941,7 +941,32 @@ var averageOfLevels = function(root) { }; ``` +TypeScript: + +```typescript +function averageOfLevels(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let total: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + let length = helperQueue.length; + for (let i = 0; i < length; i++) { + tempNode = helperQueue.shift()!; + total += tempNode.val; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + resArr.push(total / length); + total = 0; + } + return resArr; +}; +``` + Swift: + ```swift func averageOfLevels(_ root: TreeNode?) -> [Double] { var res = [Double]() From 2862d47368512a9cbc7e7184775d99d8e53f72df Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 11:44:49 +0800 Subject: [PATCH 39/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88429.N=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 9ff4f2e964..5dd448b770 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" @@ -1190,7 +1190,30 @@ var levelOrder = function(root) { }; ``` +TypeScript: + +```typescript +function levelOrder(root: Node | null): number[][] { + let helperQueue: Node[] = []; + let resArr: number[][] = []; + let tempArr: number[] = []; + if (root !== null) helperQueue.push(root); + let curNode: Node; + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + curNode = helperQueue.shift()!; + tempArr.push(curNode.val); + helperQueue.push(...curNode.children); + } + resArr.push(tempArr); + tempArr = []; + } + return resArr; +}; +``` + Swift: + ```swift func levelOrder(_ root: Node?) -> [[Int]] { var res = [[Int]]() From 9329ecff5e95e5bc1e8cb17fbba1135765fd3286 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 12:02:15 +0800 Subject: [PATCH 40/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88515.=E5=9C=A8?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=80=BC=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= 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" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 5dd448b770..6f90cb75ed 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" @@ -1393,7 +1393,34 @@ var largestValues = function(root) { }; ``` +TypeScript: + +```typescript +function largestValues(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let tempNode: TreeNode; + let max: number = 0; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (i === 0) { + max = tempNode.val; + } else { + max = max > tempNode.val ? max : tempNode.val; + } + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + resArr.push(max); + } + return resArr; +}; +``` + Swift: + ```swift func largestValues(_ root: TreeNode?) -> [Int] { var res = [Int]() From 992cd6d7f3bc2b0065bf8f3f3ff064a13c66dde3 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 16:10:56 +0800 Subject: [PATCH 41/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88116.=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?= =?UTF-8?q?=E9=92=88=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= 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" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 6f90cb75ed..12f9646918 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" @@ -1611,6 +1611,31 @@ var connect = function(root) { }; ``` +TypeScript: + +```typescript +function connect(root: Node | null): Node | null { + let helperQueue: Node[] = []; + let preNode: Node, curNode: Node; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + if (i === 0) { + preNode = helperQueue.shift()!; + } else { + curNode = helperQueue.shift()!; + preNode.next = curNode; + preNode = curNode; + } + if (preNode.left) helperQueue.push(preNode.left); + if (preNode.right) helperQueue.push(preNode.right); + } + preNode.next = null; + } + return root; +}; +``` + go: ```GO From d1abb0cbb4397a950479788497e3ec21adbcea22 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 16:21:25 +0800 Subject: [PATCH 42/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88117.=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?= =?UTF-8?q?=E9=92=88II=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= 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" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 12f9646918..a7716d9258 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" @@ -1862,6 +1862,31 @@ var connect = function(root) { return root; }; ``` +TypeScript: + +```typescript +function connect(root: Node | null): Node | null { + let helperQueue: Node[] = []; + let preNode: Node, curNode: Node; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + if (i === 0) { + preNode = helperQueue.shift()!; + } else { + curNode = helperQueue.shift()!; + preNode.next = curNode; + preNode = curNode; + } + if (preNode.left) helperQueue.push(preNode.left); + if (preNode.right) helperQueue.push(preNode.right); + } + preNode.next = null; + } + return root; +}; +``` + go: ```GO From bdb0954e32976832b9bd99dd2dcb2744d55e665b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 17:11:11 +0800 Subject: [PATCH 43/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88104.=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?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 a7716d9258..9ac23fdf3f 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" @@ -2131,7 +2131,28 @@ var maxDepth = function(root) { }; ``` +TypeScript: + +```typescript +function maxDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resDepth: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resDepth++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resDepth; +}; +``` + Swift: + ```swift func maxDepth(_ root: TreeNode?) -> Int { guard let root = root else { From 626db3fc67ba0a478f39331c4788bca15314a720 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 20:33:04 +0800 Subject: [PATCH 44/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88111.=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?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 9ac23fdf3f..1a92d42f91 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" @@ -2349,7 +2349,29 @@ var minDepth = function(root) { }; ``` +TypeScript: + +```typescript +function minDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resMin: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resMin++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left === null && tempNode.right === null) return resMin; + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resMin; +}; +``` + Swift: + ```swift func minDepth(_ root: TreeNode?) -> Int { guard let root = root else { From be3f2cfc8a6e0abc3fbbefb3d7d2967b686e96e2 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 29 Jan 2022 22:12:49 +0800 Subject: [PATCH 45/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) 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 e6dbb70934..8108e7ada6 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" @@ -563,7 +563,135 @@ var invertTree = function(root) { }; ``` +### TypeScript: + +递归法: + +```typescript +// 递归法(前序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + if (root === null) return root; + let tempNode: TreeNode | null = root.left; + root.left = root.right; + root.right = tempNode; + invertTree(root.left); + invertTree(root.right); + return root; +}; + +// 递归法(后序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + if (root === null) return root; + invertTree(root.left); + invertTree(root.right); + let tempNode: TreeNode | null = root.left; + root.left = root.right; + root.right = tempNode; + return root; +}; + +// 递归法(中序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + if (root === null) return root; + invertTree(root.left); + let tempNode: TreeNode | null = root.left; + root.left = root.right; + root.right = tempNode; + // 因为左右节点已经进行交换,此时的root.left 是原先的root.right + invertTree(root.left); + return root; +}; +``` + +迭代法: + +```typescript +// 迭代法(栈模拟前序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + let helperStack: TreeNode[] = []; + let curNode: TreeNode, + tempNode: TreeNode | null; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + // 入栈操作最好在交换节点之前进行,便于理解 + if (curNode.right) helperStack.push(curNode.right); + if (curNode.left) helperStack.push(curNode.left); + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + } + return root; +}; + +// 迭代法(栈模拟中序遍历-统一写法形式) +function invertTree(root: TreeNode | null): TreeNode | null { + let helperStack: (TreeNode | null)[] = []; + let curNode: TreeNode | null, + tempNode: TreeNode | null; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop(); + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + } + } + return root; +}; + +// 迭代法(栈模拟后序遍历-统一写法形式) +function invertTree(root: TreeNode | null): TreeNode | null { + let helperStack: (TreeNode | null)[] = []; + let curNode: TreeNode | null, + tempNode: TreeNode | null; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop(); + if (curNode !== null) { + helperStack.push(curNode); + helperStack.push(null); + if (curNode.right !== null) helperStack.push(curNode.right); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + } + } + return root; +}; + +// 迭代法(队列模拟层序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + const helperQueue: TreeNode[] = []; + let curNode: TreeNode, + tempNode: TreeNode | null; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + curNode = helperQueue.shift()!; + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + if (curNode.left !== null) helperQueue.push(curNode.left); + if (curNode.right !== null) helperQueue.push(curNode.right); + } + } + return root; +}; +``` + ### C: + 递归法 ```c struct TreeNode* invertTree(struct TreeNode* root){ From cee8536fa671b635f74edb1f7d296a5f2efc1bde Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 30 Jan 2022 13:17:47 +0800 Subject: [PATCH 46/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880101.=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) 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 69bc41d3c3..e4e232c884 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" @@ -574,6 +574,75 @@ var isSymmetric = function(root) { }; ``` +## TypeScript: + +> 递归法 + +```typescript +function isSymmetric(root: TreeNode | null): boolean { + function recur(node1: TreeNode | null, node2: TreeNode | null): boolean { + if (node1 === null && node2 === null) return true; + if (node1 === null || node2 === null) return false; + if (node1.val !== node2.val) return false + let isSym1: boolean = recur(node1.left, node2.right); + let isSym2: boolean = recur(node1.right, node2.left); + return isSym1 && isSym2 + } + if (root === null) return true; + return recur(root.left, root.right); +}; +``` + +> 迭代法 + +```typescript +// 迭代法(队列) +function isSymmetric(root: TreeNode | null): boolean { + let helperQueue: (TreeNode | null)[] = []; + let tempNode1: TreeNode | null, + tempNode2: TreeNode | null; + if (root !== null) { + helperQueue.push(root.left); + helperQueue.push(root.right); + } + while (helperQueue.length > 0) { + tempNode1 = helperQueue.shift()!; + tempNode2 = helperQueue.shift()!; + if (tempNode1 === null && tempNode2 === null) continue; + if (tempNode1 === null || tempNode2 === null) return false; + if (tempNode1.val !== tempNode2.val) return false; + helperQueue.push(tempNode1.left); + helperQueue.push(tempNode2.right); + helperQueue.push(tempNode1.right); + helperQueue.push(tempNode2.left); + } + return true; +} + +// 迭代法(栈) +function isSymmetric(root: TreeNode | null): boolean { + let helperStack: (TreeNode | null)[] = []; + let tempNode1: TreeNode | null, + tempNode2: TreeNode | null; + if (root !== null) { + helperStack.push(root.left); + helperStack.push(root.right); + } + while (helperStack.length > 0) { + tempNode1 = helperStack.pop()!; + tempNode2 = helperStack.pop()!; + if (tempNode1 === null && tempNode2 === null) continue; + if (tempNode1 === null || tempNode2 === null) return false; + if (tempNode1.val !== tempNode2.val) return false; + helperStack.push(tempNode1.left); + helperStack.push(tempNode2.right); + helperStack.push(tempNode1.right); + helperStack.push(tempNode2.left); + } + return true; +}; +``` + ## Swift: > 递归 From 067f71cdf03858645ba70c5151a1d26f16d91928 Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Mon, 31 Jan 2022 11:54:59 +0800 Subject: [PATCH 47/86] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E8=83=8C=E5=8C=85.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加注释,去掉一个不必要的if语句 --- ...5\256\214\345\205\250\350\203\214\345\214\205.md" | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 f79310b8be..cea69c724c 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" @@ -183,11 +183,9 @@ private static void testCompletePack(){ int[] value = {15, 20, 30}; int bagWeight = 4; int[] dp = new int[bagWeight + 1]; - for (int i = 0; i < weight.length; i++){ - for (int j = 1; j <= bagWeight; j++){ - if (j - weight[i] >= 0){ - dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); - } + for (int i = 0; i < weight.length; i++){ // 遍历物品 + for (int j = weight[i]; j <= bagWeight; j++){ // 遍历背包容量 + dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); } } for (int maxValue : dp){ @@ -201,8 +199,8 @@ private static void testCompletePackAnotherWay(){ int[] value = {15, 20, 30}; int bagWeight = 4; int[] dp = new int[bagWeight + 1]; - for (int i = 1; i <= bagWeight; i++){ - for (int j = 0; j < weight.length; j++){ + for (int i = 1; i <= bagWeight; i++){ // 遍历背包容量 + for (int j = 0; j < weight.length; j++){ // 遍历物品 if (i - weight[j] >= 0){ dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]); } From 46571a3b9c619ac39c93becab1c1325fe211b545 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 31 Jan 2022 14:51:28 +0800 Subject: [PATCH 48/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880104.=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?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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 7038598b1f..6875ec74e6 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" @@ -598,7 +598,55 @@ var maxDepth = function(root) { }; ``` +## TypeScript: + +> 二叉树的最大深度: + +```typescript +// 后续遍历(自下而上) +function maxDepth(root: TreeNode | null): number { + if (root === null) return 0; + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; +}; + +// 前序遍历(自上而下) +function maxDepth(root: TreeNode | null): number { + function recur(node: TreeNode | null, count: number) { + if (node === null) { + resMax = resMax > count ? resMax : count; + return; + } + recur(node.left, count + 1); + recur(node.right, count + 1); + } + let resMax: number = 0; + let count: number = 0; + recur(root, count); + return resMax; +}; + +// 层序遍历(迭代法) +function maxDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resDepth: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resDepth++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resDepth; +}; +``` + + + ## C + 二叉树最大深度递归 ```c int maxDepth(struct TreeNode* root){ From 463f142f05d5759c8dfaa5f17dbebfa9ddf580f5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 31 Jan 2022 16:07:01 +0800 Subject: [PATCH 49/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88559.n=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) 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 6875ec74e6..3eecdc927b 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" @@ -643,7 +643,33 @@ function maxDepth(root: TreeNode | null): number { }; ``` +> N叉树的最大深度 +```typescript +// 后续遍历(自下而上) +function maxDepth(root: TreeNode | null): number { + if (root === null) return 0; + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; +}; + +// 前序遍历(自上而下) +function maxDepth(root: TreeNode | null): number { + function recur(node: TreeNode | null, count: number) { + if (node === null) { + resMax = resMax > count ? resMax : count; + return; + } + recur(node.left, count + 1); + recur(node.right, count + 1); + } + let resMax: number = 0; + let count: number = 0; + recur(root, count); + return resMax; +}; + + +``` ## C From e4f674c61e71a7b33ec97e22acc6b7a44e075b6e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 1 Feb 2022 20:04:51 +0800 Subject: [PATCH 50/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880111.=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?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) 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 a439322a9c..224caa5e37 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" @@ -404,6 +404,44 @@ var minDepth = function(root) { }; ``` +## TypeScript + +> 递归法 + +```typescript +function minDepth(root: TreeNode | null): number { + if (root === null) return 0; + if (root.left !== null && root.right === null) { + return 1 + minDepth(root.left); + } + if (root.left === null && root.right !== null) { + return 1 + minDepth(root.right); + } + return 1 + Math.min(minDepth(root.left), minDepth(root.right)); +} +``` + +> 迭代法 + +```typescript +function minDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resMin: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resMin++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left === null && tempNode.right === null) return resMin; + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resMin; +}; +``` + ## Swift > 递归 From 8ac7cdce22a6f350ec57627e474bd482f5d041b3 Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:01:36 +0800 Subject: [PATCH 51/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.md=EF=BC=89=EF=BC=9A=E8=A1=A5?= =?UTF-8?q?=E5=85=85Java=E7=89=88=E6=9C=AC2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\267\203\346\270\270\346\210\217II.md" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 a39c064ae6..7a3f048c60 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" @@ -142,6 +142,7 @@ public: ### Java ```Java +// 版本一 class Solution { public int jump(int[] nums) { if (nums == null || nums.length == 0 || nums.length == 1) { @@ -172,7 +173,30 @@ class Solution { } ``` +```java +// 版本二 +class Solution { + public int jump(int[] nums) { + int result = 0; + // 当前覆盖的最远距离下标 + int end = 0; + // 下一步覆盖的最远距离下标 + int temp = 0; + for (int i = 0; i <= end && end < nums.length - 1; ++i) { + temp = Math.max(temp, i + nums[i]); + // 可达位置的改变次数就是跳跃次数 + if (i == end) { + end = temp; + result++; + } + } + return result; + } +} +``` + ### Python + ```python class Solution: def jump(self, nums: List[int]) -> int: From 40c06155f9e65e3525100ac6aff6f3ce6aeb8dac Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:04:07 +0800 Subject: [PATCH 52/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Java=E5=9B=9E=E5=9B=9E=E6=BA=AF+=E8=AE=B0=E5=BF=86?= =?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 --- ...25\350\257\215\346\213\206\345\210\206.md" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 e24ffc1a89..1653a81a14 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" @@ -248,6 +248,36 @@ class Solution { return valid[s.length()]; } } + +// 回溯法+记忆化 +class Solution { + public boolean wordBreak(String s, List wordDict) { + Set wordDictSet = new HashSet(wordDict); + int[] memory = new int[s.length()]; + return backTrack(s, wordDictSet, 0, memory); + } + + public boolean backTrack(String s, Set wordDictSet, int startIndex, int[] memory) { + // 结束条件 + if (startIndex >= s.length()) { + return true; + } + if (memory[startIndex] != 0) { + // 此处认为:memory[i] = 1 表示可以拼出i 及以后的字符子串, memory[i] = -1 表示不能 + return memory[startIndex] == 1 ? true : false; + } + for (int i = startIndex; i < s.length(); ++i) { + // 处理 递归 回溯 循环不变量:[startIndex, i + 1) + String word = s.substring(startIndex, i + 1); + if (wordDictSet.contains(word) && backTrack(s, wordDictSet, i + 1, memory)) { + memory[startIndex] = 1; + return true; + } + } + memory[startIndex] = -1; + return false; + } +} ``` Python: From 5a2ff02b94d417a7a51d44998e212913afeba48b Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:08:56 +0800 Subject: [PATCH 53/86] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=EF=BC=89=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E9=80=BB=E8=BE=91=E5=B0=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0376.\346\221\206\345\212\250\345\272\217\345\210\227.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2bc231822f..d75311eb2a 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" @@ -174,7 +174,7 @@ public: ```Java class Solution { public int wiggleMaxLength(int[] nums) { - if (nums == null || nums.length <= 1) { + if (nums.length <= 1) { return nums.length; } //当前差值 From 2f1c56e6dd34a5a8ae89a8401219a0ce2dd623f3 Mon Sep 17 00:00:00 2001 From: Xiaofei-fei Date: Wed, 2 Feb 2022 18:47:01 +0800 Subject: [PATCH 54/86] =?UTF-8?q?494=E9=A2=98python=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 8196 bytes ...0494.\347\233\256\346\240\207\345\222\214.md" | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7794435bd6714aed6095604325ab6ffe84221748 GIT binary patch literal 8196 zcmeHMU2GIp6u#fIv@>*|1GG|b$8K6szzU`1r~D<`KM+bOc3ZkFVA<^sbYMDDcV^o{ zC8_Z*g7~0DeNjN;ix1VP7!wtvi3asattOIaj7FnRKFACD;<m0CQ8r?%*`* zup!Jqn1L_@VFtnsgc#mK>qC@oE@Ocl6VhKgsPK;f zB>54*zv!9H0lrT(kg-6H3F*61pW^g@z!kv}1Hzs3QEpB$7RWIng*$_AX9z|{a6*AU zI{8I?bB36ZVHsv1%)s;vaQet>kY$*|QrDl~QTewYB!ubsmP_3Yqqra9{olWg+mlT+n{GUw!6>BxFmbE zVwVEl+2`1MGX~dp^*L5{z;=7J1${}YFKZ<|qu#RO<_Uw^irv19V_AD0*W}%FCp=@s z+c#eT{C4F9d>fSP!^y{nJ6w>vh?lONbu4&8|RHg%m zmHORkhfBfBT2dIUR;$&HV*~|7b45$7x?bVg$!AiU;v1PT@4p;3+(h7x5C#;XGc$n|KQs@D4u2 zC47d@@il(J&-ewu;tyQKU-(*}`EPl8aOHV4$};qPPqUrg~a?_**LUMB<#)$$zerqZ#K$YPAhoU&S> zu&z*-$r>?ej;P+Hu9USFMJo{1s>o{MrL0MQRb3mYRkWn6<%w!lWTPT$5S7j9R?b5$ zUsQE9rYIWoE0M-XGv|^f6Uxsu`LjI>%9Uv=l#lj1Ssi`Gb+E&<$vniKWM>KOAFzw; zOLm$4$bMs22=4`ibPeTLfog0(4BN2-yU>DG>_Zp2(L-oAaM(xuF?h&gm{5NL591L$ zhR5*)p2jnH7SG`$w)*YO6S|2@2qi-i7<@G-u?1it6^4^W#!bE~N5N?f6* zX=ombXKc%{kB~0smvguYWuAEFGJgJVp8fa#n=rEQmBS2#8ThXmKz>uase!C#ThsVi zJ4*K literal 0 HcmV?d00001 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 0faef4a577..f190b734f3 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" @@ -272,7 +272,8 @@ Python: class Solution: def findTargetSumWays(self, nums: List[int], target: int) -> int: sumValue = sum(nums) - if target > sumValue or (sumValue + target) % 2 == 1: return 0 + #注意边界条件为 target>sumValue or target<-sumValue or (sumValue + target) % 2 == 1 + if abs(target) > sumValue or (sumValue + target) % 2 == 1: return 0 bagSize = (sumValue + target) // 2 dp = [0] * (bagSize + 1) dp[0] = 1 From 38c8b6849f32b47d59fc9ed5d1b394dda745b277 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 3 Feb 2022 20:03:43 +0800 Subject: [PATCH 55/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880222.=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.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02\347\202\271\344\270\252\346\225\260.md" | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 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 8d38bace93..ffbc32ffb2 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" @@ -447,7 +447,63 @@ var countNodes = function(root) { }; ``` +## TypeScrpt: + +> 递归法 + +```typescript +function countNodes(root: TreeNode | null): number { + if (root === null) return 0; + return 1 + countNodes(root.left) + countNodes(root.right); +}; +``` + +> 迭代法 + +```typescript +function countNodes(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resCount: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + resCount++; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resCount; +}; +``` + +> 利用完全二叉树性质 + +```typescript +function countNodes(root: TreeNode | null): number { + if (root === null) return 0; + let left: number = 0, + right: number = 0; + let curNode: TreeNode | null= root; + while (curNode !== null) { + left++; + curNode = curNode.left; + } + curNode = root; + while (curNode !== null) { + right++; + curNode = curNode.right; + } + if (left === right) { + return 2 ** left - 1; + } + return 1 + countNodes(root.left) + countNodes(root.right); +}; +``` + ## C: + 递归法 ```c int countNodes(struct TreeNode* root) { @@ -538,7 +594,7 @@ func _countNodes(_ root: TreeNode?) -> Int { return 1 + leftCount + rightCount } ``` - + > 层序遍历 ```Swift func countNodes(_ root: TreeNode?) -> Int { @@ -564,7 +620,7 @@ func countNodes(_ root: TreeNode?) -> Int { return res } ``` - + > 利用完全二叉树性质 ```Swift func countNodes(_ root: TreeNode?) -> Int { From ea5a011d783073dc0f98bd5d0a0130c97a02153c Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 5 Feb 2022 13:04:38 +0800 Subject: [PATCH 56/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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 9d43407a31..8a701bb307 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" @@ -627,7 +627,26 @@ var isBalanced = function(root) { }; ``` +## TypeScript + +```typescript +// 递归法 +function isBalanced(root: TreeNode | null): boolean { + function getDepth(root: TreeNode | null): number { + if (root === null) return 0; + let leftDepth: number = getDepth(root.left); + if (leftDepth === -1) return -1; + let rightDepth: number = getDepth(root.right); + if (rightDepth === -1) return -1; + if (Math.abs(leftDepth - rightDepth) > 1) return -1; + return 1 + Math.max(leftDepth, rightDepth); + } + return getDepth(root) !== -1; +}; +``` + ## C + 递归法: ```c int getDepth(struct TreeNode* node) { From f9e5bce7d7b8b53d266ba082036677b30d092696 Mon Sep 17 00:00:00 2001 From: jobinben <1021137079@qq.com> Date: Sun, 6 Feb 2022 18:36:14 +0800 Subject: [PATCH 57/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E9=A2=9802.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md=20JavaSc?= =?UTF-8?q?ript=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95=E7=9A=84=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02.07.\351\223\276\350\241\250\347\233\270\344\272\244.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 dd91f0696e..2e7226deeb 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" @@ -224,12 +224,14 @@ var getIntersectionNode = function(headA, headB) { lenA = getListLen(headA), lenB = getListLen(headB); if(lenA < lenB) { + // 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时 + // 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA] [curA, curB] = [curB, curA]; [lenA, lenB] = [lenB, lenA]; } let i = lenA - lenB; while(i-- > 0) { - curA = curA.next + curA = curA.next; } while(curA && curA !== curB) { curA = curA.next; From 343ddb37f9e84284ff8df34f89bed18522244e43 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 20:13:42 +0800 Subject: [PATCH 58/86] =?UTF-8?q?1047.=E5=88=A0=E9=99=A4=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89=E7=9B=B8=E9=82=BB?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E9=A1=B9=EF=BC=9A=E4=BC=98=E5=8C=96Swift?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...351\202\273\351\207\215\345\244\215\351\241\271.md" | 10 ++++------ 1 file changed, 4 insertions(+), 6 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 d6eefd078a..9309aed889 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" @@ -322,14 +322,12 @@ char * removeDuplicates(char * s){ Swift: ```swift func removeDuplicates(_ s: String) -> String { - let array = Array(s) var stack = [Character]() - for c in array { - let last: Character? = stack.last - if stack.isEmpty || last != c { - stack.append(c) - } else { + for c in s { + if stack.last == c { stack.removeLast() + } else { + stack.append(c) } } return String(stack) From 2aa31ce54bcb186376a1341d8ef91282a8db4c6f Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 21:42:07 +0800 Subject: [PATCH 59/86] =?UTF-8?q?0239.=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=80=BC=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92?= =?UTF-8?q?=E7=89=88=EF=BC=8C=E8=A1=A5=E5=85=85Swift=E7=89=88=E6=9C=AC?= 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" | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) 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 d9788f6328..d7a94b5ae3 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" @@ -45,7 +45,7 @@ 这个队列应该长这个样子: -``` +```cpp class MyQueue { public: void pop(int value) { @@ -525,5 +525,39 @@ class Solution { } ``` +Swift解法二: + +```swift +func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { + var result = [Int]() + var window = [Int]() + var right = 0, left = right - k + 1 + + while right < nums.count { + let value = nums[right] + + // 因为窗口移动丢弃的左边数 + if left > 0, left - 1 == window.first { + window.removeFirst() + } + + // 保证末尾的是最大的 + while !window.isEmpty, value > nums[window.last!] { + window.removeLast() + } + window.append(right) + + if left >= 0 { // 窗口形成 + result.append(nums[window.first!]) + } + + right += 1 + left += 1 + } + + return result +} +``` + -----------------------
From 40dc0d5e65fc36833c9d3fc77bba45606e83f739 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 21:48:45 +0800 Subject: [PATCH 60/86] =?UTF-8?q?=E6=A0=88=E4=B8=8E=E9=98=9F=E5=88=97?= =?UTF-8?q?=E6=80=BB=E7=BB=93=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\237\345\210\227\346\200\273\347\273\223.md" | 17 ----------------- 1 file changed, 17 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 8ec96a2972..15093ca703 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" @@ -158,22 +158,5 @@ cd a/b/c/../../ 好了,栈与队列我们就总结到这里了,接下来Carl就要带大家开启新的篇章了,大家加油! - - - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - - -----------------------
From c88aee12e53a301ede5ed3fa81023d98c1ce009b Mon Sep 17 00:00:00 2001 From: bqlin Date: Tue, 21 Dec 2021 11:29:56 +0800 Subject: [PATCH 61/86] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\221\347\220\206\350\256\272\345\237\272\347\241\200.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 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 cc89985092..c5dd118b19 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" @@ -154,7 +154,7 @@ C++代码如下: -``` +```cpp struct TreeNode { int val; TreeNode *left; @@ -163,7 +163,7 @@ struct TreeNode { }; ``` -大家会发现二叉树的定义 和链表是差不多的,相对于链表 ,二叉树的节点里多了一个指针, 有两个指针,指向左右孩子. +大家会发现二叉树的定义 和链表是差不多的,相对于链表 ,二叉树的节点里多了一个指针, 有两个指针,指向左右孩子。 这里要提醒大家要注意二叉树节点定义的书写方式。 @@ -177,7 +177,7 @@ struct TreeNode { 本篇我们介绍了二叉树的种类、存储方式、遍历方式以及定义,比较全面的介绍了二叉树各个方面的重点,帮助大家扫一遍基础。 -**说道二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。** +**说到二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。** ## 其他语言版本 From be6c45c636556b1c0d3838f265fe7830ca4f00ff Mon Sep 17 00:00:00 2001 From: jobinben <1021137079@qq.com> Date: Sun, 6 Feb 2022 19:06:51 +0800 Subject: [PATCH 62/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91.md=20JavaScript=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC=E7=9A=84=E8=BF=AD=E4=BB=A3=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221.md" | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) 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 9d43407a31..8a11c0134f 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" @@ -604,7 +604,8 @@ func abs(a int)int{ } ``` -## JavaScript +## JavaScript +递归法: ```javascript var isBalanced = function(root) { //还是用递归三部曲 + 后序遍历 左右中 当前左子树右子树高度相差大于1就返回-1 @@ -627,6 +628,48 @@ var isBalanced = function(root) { }; ``` +迭代法: +```javascript +// 获取当前节点的高度 +var getHeight = function (curNode) { + let queue = []; + if (curNode !== null) queue.push(curNode); // 压入当前元素 + let depth = 0, res = 0; + while (queue.length) { + let node = queue[queue.length - 1]; // 取出栈顶 + if (node !== null) { + queue.pop(); + queue.push(node); // 中 + queue.push(null); + depth++; + node.right && queue.push(node.right); // 右 + node.left && queue.push(node.left); // 左 + } else { + queue.pop(); + node = queue[queue.length - 1]; + queue.pop(); + depth--; + } + res = res > depth ? res : depth; + } + return res; +} +var isBalanced = function (root) { + if (root === null) return true; + let queue = [root]; + while (queue.length) { + let node = queue[queue.length - 1]; // 取出栈顶 + queue.pop(); + if (Math.abs(getHeight(node.left) - getHeight(node.right)) > 1) { + return false; + } + node.right && queue.push(node.right); + node.left && queue.push(node.left); + } + return true; +}; +``` + ## C 递归法: ```c From f1a3fbc78851c01bb2b97cf004c85b3d15f41970 Mon Sep 17 00:00:00 2001 From: Anmizi <1845513904@qq.com> Date: Sun, 6 Feb 2022 23:33:11 +0800 Subject: [PATCH 63/86] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改原有JS版本代码问题 --- ...71\263\350\241\241\344\272\214\345\217\211\346\240\221.md" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 9d43407a31..313aeff237 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" @@ -614,8 +614,10 @@ var isBalanced = function(root) { if(node === null) return 0; // 3. 确定单层递归逻辑 let leftDepth = getDepth(node.left); //左子树高度 - let rightDepth = getDepth(node.right); //右子树高度 + // 当判定左子树不为平衡二叉树时,即可直接返回-1 if(leftDepth === -1) return -1; + let rightDepth = getDepth(node.right); //右子树高度 + // 当判定右子树不为平衡二叉树时,即可直接返回-1 if(rightDepth === -1) return -1; if(Math.abs(leftDepth - rightDepth) > 1) { return -1; From 8ce87963e5fe7e74657109d71c80ce3ce3b07da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Mon, 7 Feb 2022 13:29:05 +0800 Subject: [PATCH 64/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=EF=BC=9A=E4=BB=A5=E4=B8=BA=E4=BD=BF=E7=94=A8=E4=BA=86?= =?UTF-8?q?=E9=80=92=E5=BD=92=EF=BC=8C=E5=85=B6=E5=AE=9E=E8=BF=98=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E7=9D=80=E5=9B=9E=E6=BA=AF=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\347\235\200\345\233\236\346\272\257.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index 20b87f875c..603854dc95 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -515,6 +515,61 @@ var binaryTreePaths = function(root) { }; ``` +Swift: +> 100.相同的树 +```swift +// 递归 +func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { + return _isSameTree3(p, q) +} +func _isSameTree3(_ p: TreeNode?, _ q: TreeNode?) -> Bool { + if p == nil && q == nil { + return true + } else if p == nil && q != nil { + return false + } else if p != nil && q == nil { + return false + } else if p!.val != q!.val { + return false + } + let leftSide = _isSameTree3(p!.left, q!.left) + let rightSide = _isSameTree3(p!.right, q!.right) + return leftSide && rightSide +} +``` + +> 257.二叉树的不同路径 +```swift +// 递归/回溯 +func binaryTreePaths(_ root: TreeNode?) -> [String] { + var res = [String]() + guard let root = root else { + return res + } + var paths = [Int]() + _binaryTreePaths3(root, res: &res, paths: &paths) + return res +} +func _binaryTreePaths3(_ root: TreeNode, res: inout [String], paths: inout [Int]) { + paths.append(root.val) + if root.left == nil && root.right == nil { + var str = "" + for i in 0 ..< (paths.count - 1) { + str.append("\(paths[i])->") + } + str.append("\(paths.last!)") + res.append(str) + } + if let left = root.left { + _binaryTreePaths3(left, res: &res, paths: &paths) + paths.removeLast() + } + if let right = root.right { + _binaryTreePaths3(right, res: &res, paths: &paths) + paths.removeLast() + } +} +``` -----------------------
From 3aa54bfb8a8604a8cfb05a4fa8c606ebe6b09db3 Mon Sep 17 00:00:00 2001 From: Wayne <3522373084@qq.com> Date: Mon, 7 Feb 2022 21:39:51 +0800 Subject: [PATCH 65/86] =?UTF-8?q?343=E6=95=B4=E6=95=B0=E6=8B=86=E5=88=86,?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AC=AC=E4=BA=8C=E5=B1=82=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=20j=20=E7=9A=84=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\346\225\260\346\213\206\345\210\206.md" | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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 5d11f670f7..f616a606af 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" @@ -197,14 +197,17 @@ Java: ```Java class Solution { public int integerBreak(int n) { - //dp[i]为正整数i拆分结果的最大乘积 - int[] dp = new int[n+1]; - dp[2] = 1; - for (int i = 3; i <= n; ++i) { - for (int j = 1; j < i - 1; ++j) { - //j*(i-j)代表把i拆分为j和i-j两个数相乘 - //j*dp[i-j]代表把i拆分成j和继续把(i-j)这个数拆分,取(i-j)拆分结果中的最大乘积与j相乘 - dp[i] = Math.max(dp[i], Math.max(j * (i - j), j * dp[i - j])); + //dp[i] 为正整数 i 拆分后的结果的最大乘积 + int[]dp=new int[n+1]; + dp[2]=1; + for(int i=3;i<=n;i++){ + for(int j=1;j<=i-j;j++){ + // 这里的 j 其实最大值为 i-j,再大只不过是重复而已, + //并且,在本题中,我们分析 dp[0], dp[1]都是无意义的, + //j 最大到 i-j,就不会用到 dp[0]与dp[1] + dp[i]=Math.max(dp[i],Math.max(j*(i-j),j*dp[i-j])); + // j * (i - j) 是单纯的把整数 i 拆分为两个数 也就是 i,i-j ,再相乘 + //而j * dp[i - j]是将 i 拆分成两个以及两个以上的个数,再相乘。 } } return dp[n]; From 1b7c86e20c2b36ed0732deb66a0c124a8e8fcdff Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 7 Feb 2022 23:01:47 +0800 Subject: [PATCH 66/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880257.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= 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" | 67 +++++++++++++++++-- 1 file changed, 61 insertions(+), 6 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 4078320f23..1362897c89 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" @@ -433,9 +433,9 @@ class Solution: if cur.right: self.traversal(cur.right, path + '->', result) ``` - + 迭代法: - + ```python3 from collections import deque @@ -463,13 +463,13 @@ class Solution: return result ``` - + --- Go: - + 递归法: - + ```go func binaryTreePaths(root *TreeNode) []string { res := make([]string, 0) @@ -492,7 +492,7 @@ func binaryTreePaths(root *TreeNode) []string { return res } ``` - + 迭代法: ```go @@ -581,7 +581,62 @@ var binaryTreePaths = function(root) { }; ``` +TypeScript: + +> 递归法 + +```typescript +function binaryTreePaths(root: TreeNode | null): string[] { + function recur(node: TreeNode, route: string, resArr: string[]): void { + route += String(node.val); + if (node.left === null && node.right === null) { + resArr.push(route); + return; + } + if (node.left !== null) recur(node.left, route + '->', resArr); + if (node.right !== null) recur(node.right, route + '->', resArr); + } + const resArr: string[] = []; + if (root === null) return resArr; + recur(root, '', resArr); + return resArr; +}; +``` + +> 迭代法 + +```typescript +// 迭代法2 +function binaryTreePaths(root: TreeNode | null): string[] { + let helperStack: TreeNode[] = []; + let tempNode: TreeNode; + let routeArr: string[] = []; + let resArr: string[] = []; + if (root !== null) { + helperStack.push(root); + routeArr.push(String(root.val)); + }; + while (helperStack.length > 0) { + tempNode = helperStack.pop()!; + let route: string = routeArr.pop()!; // tempNode 对应的路径 + if (tempNode.left === null && tempNode.right === null) { + resArr.push(route); + } + if (tempNode.right !== null) { + helperStack.push(tempNode.right); + routeArr.push(route + '->' + tempNode.right.val); // tempNode.right 对应的路径 + } + if (tempNode.left !== null) { + helperStack.push(tempNode.left); + routeArr.push(route + '->' + tempNode.left.val); // tempNode.left 对应的路径 + } + } + return resArr; +}; +``` + Swift: + > 递归/回溯 ```swift func binaryTreePaths(_ root: TreeNode?) -> [String] { From 47818c947888ecff3dae6f36daded1cfaf92db3b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 8 Feb 2022 10:23:59 +0800 Subject: [PATCH 67/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80?= =?UTF-8?q?=E5=9B=9E=E6=BA=AF.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84=E6=A0=91typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\347\235\200\345\233\236\346\272\257.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index 20b87f875c..03815ed3e3 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -515,6 +515,29 @@ var binaryTreePaths = function(root) { }; ``` +TypeScript: + +> 相同的树 + +```typescript +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (p === null && q === null) return true; + if (p === null || q === null) return false; + if (p.val !== q.val) return false; + let bool1: boolean, bool2: boolean; + bool1 = isSameTree(p.left, q.left); + bool2 = isSameTree(p.right, q.right); + return bool1 && bool2; +}; +``` + +> 二叉树的不同路径 + +```typescript +``` + + + -----------------------
From 49a1a42bbed494b10b4d6659ba787830f62bd842 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 8 Feb 2022 10:45:15 +0800 Subject: [PATCH 68/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80?= =?UTF-8?q?=E5=9B=9E=E6=BA=AF.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E7=9A=84typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\347\235\200\345\233\236\346\272\257.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" index 03815ed3e3..41d5663e04 100644 --- "a/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" +++ "b/problems/\344\272\214\345\217\211\346\240\221\344\270\255\351\200\222\345\275\222\345\270\246\347\235\200\345\233\236\346\272\257.md" @@ -534,6 +534,27 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { > 二叉树的不同路径 ```typescript +function binaryTreePaths(root: TreeNode | null): string[] { + function recur(node: TreeNode, nodeSeqArr: number[], resArr: string[]): void { + nodeSeqArr.push(node.val); + if (node.left === null && node.right === null) { + resArr.push(nodeSeqArr.join('->')); + } + if (node.left !== null) { + recur(node.left, nodeSeqArr, resArr); + nodeSeqArr.pop(); + } + if (node.right !== null) { + recur(node.right, nodeSeqArr, resArr); + nodeSeqArr.pop(); + } + } + let nodeSeqArr: number[] = []; + let resArr: string[] = []; + if (root === null) return resArr; + recur(root, nodeSeqArr, resArr); + return resArr; +}; ``` From f604aea34c6632e20ad4ced37cb24c005420e574 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 8 Feb 2022 23:00:31 +0800 Subject: [PATCH 69/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= 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" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) 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 09272052de..2b4df15123 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" @@ -372,6 +372,50 @@ var sumOfLeftLeaves = function(root) { }; ``` +## TypeScript + +> 递归法 + +```typescript +function sumOfLeftLeaves(root: TreeNode | null): number { + if (root === null) return 0; + let midVal: number = 0; + if ( + root.left !== null && + root.left.left === null && + root.left.right === null + ) { + midVal = root.left.val; + } + let leftVal: number = sumOfLeftLeaves(root.left); + let rightVal: number = sumOfLeftLeaves(root.right); + return midVal + leftVal + rightVal; +}; +``` + +> 迭代法 + +```typescript +function sumOfLeftLeaves(root: TreeNode | null): number { + let helperStack: TreeNode[] = []; + let tempNode: TreeNode; + let sum: number = 0; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + tempNode = helperStack.pop()!; + if ( + tempNode.left !== null && + tempNode.left.left === null && + tempNode.left.right === null + ) { + sum += tempNode.left.val; + } + if (tempNode.right !== null) helperStack.push(tempNode.right); + if (tempNode.left !== null) helperStack.push(tempNode.left); + } + return sum; +}; +``` ## Swift From 81c1060ad7b21cab48d3f27aea802135544a73ff Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Wed, 9 Feb 2022 14:57:04 +0800 Subject: [PATCH 70/86] Update --- README.md | 9 +++++---- ...1\346\226\207\346\241\243\345\267\245\345\205\267.md" | 1 - ...5\237\272\347\241\20001\350\203\214\345\214\205-2.md" | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c51497767b..c1761f7b02 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,11 @@ > 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **PDF版本** : [「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 3. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 -> 4. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 5. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 -> 6. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! +> 3. **最强八股文:**:[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) +> 4. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 +> 5. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 +> 6. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 +> 7. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!

diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" index b76fb03680..e4193c426a 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" @@ -135,7 +135,6 @@ Markdown支持部分html,例如这样 - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) 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 a57bae1064..dabdfb2d09 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" @@ -210,7 +210,7 @@ int main() { ## 其他语言版本 -Java: +### Java ```java public static void main(String[] args) { @@ -240,7 +240,7 @@ Java: -Python: +### Python ```python def test_1_wei_bag_problem(): weight = [1, 3, 4] @@ -260,7 +260,7 @@ def test_1_wei_bag_problem(): test_1_wei_bag_problem() ``` -Go: +### Go ```go func test_1_wei_bag_problem(weight, value []int, bagWeight int) int { // 定义 and 初始化 @@ -292,7 +292,7 @@ func main() { } ``` -javaScript: +### javaScript ```js From 4ed65b50d174a3d9f89d0352ee30da95fcecece9 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 9 Feb 2022 22:45:31 +0800 Subject: [PATCH 71/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880513.=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\247\222\347\232\204\345\200\274.md" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) 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 84ed393207..12c62c7068 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" @@ -433,6 +433,51 @@ var findBottomLeftValue = function(root) { }; ``` +## TypeScript + +> 递归法: + +```typescript +function findBottomLeftValue(root: TreeNode | null): number { + function recur(root: TreeNode, depth: number): void { + if (root.left === null && root.right === null) { + if (depth > maxDepth) { + maxDepth = depth; + resVal = root.val; + } + return; + } + if (root.left !== null) recur(root.left, depth + 1); + if (root.right !== null) recur(root.right, depth + 1); + } + let maxDepth: number = 0; + let resVal: number = 0; + if (root === null) return resVal; + recur(root, 1); + return resVal; +}; +``` + +> 迭代法: + +```typescript +function findBottomLeftValue(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + if (root !== null) helperQueue.push(root); + let resVal: number = 0; + let tempNode: TreeNode; + while (helperQueue.length > 0) { + resVal = helperQueue[0].val; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resVal; +}; +``` + ## Swift 递归版本: From 7d8e9e8fae9d580013b717ff882bd9a942fe0e4f Mon Sep 17 00:00:00 2001 From: Guang-Hou <87743934+Guang-Hou@users.noreply.github.com> Date: Wed, 9 Feb 2022 16:37:44 -0500 Subject: [PATCH 72/86] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 递归法- 无返回值的一种更简单的实现。 --- ...22\345\205\245\346\223\215\344\275\234.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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 468f26750d..5e9fbdfe7e 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" @@ -310,6 +310,26 @@ class Solution: return root ``` +**递归法** - 无返回值 - another easier way +```python +class Solution: + def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]: + newNode = TreeNode(val) + if not root: return newNode + + if not root.left and val < root.val: + root.left = newNode + if not root.right and val > root.val: + root.right = newNode + + if val < root.val: + self.insertIntoBST(root.left, val) + if val > root.val: + self.insertIntoBST(root.right, val) + + return root +``` + **迭代法** 与无返回值的递归函数的思路大体一致 ```python From fc1a7e3e079958e6919bbc4fbe069a8d92e66322 Mon Sep 17 00:00:00 2001 From: zhaoninge Date: Thu, 10 Feb 2022 17:01:31 +0800 Subject: [PATCH 73/86] =?UTF-8?q?Update=200027.=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加27. 移除元素 C++版相向双指针方法,该方法基于元素顺序可以改变的题目描述,使用双指针法改变了元素相对位置,确保了移动最少元素 --- ...73\351\231\244\345\205\203\347\264\240.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) 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 d69f2bcfe8..0c6f512965 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" @@ -106,6 +106,37 @@ public: 旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html) +```CPP +/** +* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素 +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ +*/ +class Solution { +public: + int removeElement(vector& nums, int val) { + int leftIndex = 0; + int rightIndex = nums.size() - 1; + while (leftIndex <= rightIndex) { + // 找左边等于val的元素 + while (leftIndex <= rightIndex && nums[leftIndex] != val){ + ++leftIndex; + } + // 找右边不等于val的元素 + while (leftIndex <= rightIndex && nums[rightIndex] == val) { + -- rightIndex; + } + // 将右边不等于val的元素覆盖左边等于val的元素 + if (leftIndex < rightIndex) { + nums[leftIndex++] = nums[rightIndex--]; + } + } + return leftIndex; // leftIndex一定指向了最终数组末尾的下一个元素 + } +}; +``` + + ## 相关题目推荐 * 26.删除排序数组中的重复项 From 71dd3eab9e9a005eec5669bfc25ba3cd4d27fb2f Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 11 Feb 2022 11:26:14 +0800 Subject: [PATCH 74/86] =?UTF-8?q?0059=20=20=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5II=20=20=E9=A2=98=E7=9B=AE=E9=94=99=E8=AF=AF=E7=BA=A0?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0059.\350\236\272\346\227\213\347\237\251\351\230\265II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3f7a59ca2f..ff65435835 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" @@ -10,7 +10,7 @@ [力扣题目链接](https://leetcode-cn.com/problems/spiral-matrix-ii/) -给定一个正整数 n,生成一个包含 1 到 $n^2$ 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 +给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 示例: From 305ef69d5878ecaf1b9655e7899d663290f53af5 Mon Sep 17 00:00:00 2001 From: weiting-cn <2254912@qq.com> Date: Fri, 11 Feb 2022 16:09:17 +0800 Subject: [PATCH 75/86] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A0015.=E4=B8=89?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=EF=BC=8C0018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=EF=BC=88=E5=8E=BB=E9=87=8D=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0015.\344\270\211\346\225\260\344\271\213\345\222\214.md" | 4 ++++ .../0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" | 4 ++++ 2 files changed, 8 insertions(+) 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 e191eabc4d..9b59e66dc3 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" @@ -138,8 +138,12 @@ public: */ if (nums[i] + nums[left] + nums[right] > 0) { right--; + // 当前元素不合适了,可以去重 + while (left < right && nums[right] == nums[right + 1]) right--; } else if (nums[i] + nums[left] + nums[right] < 0) { left++; + // 不合适,去重 + while (left < right && nums[left] == nums[left - 1]) left++; } else { result.push_back(vector{nums[i], nums[left], nums[right]}); // 去重逻辑应该放在找到一个三元组之后 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 bad258c181..c6c55d5069 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" @@ -91,9 +91,13 @@ public: // nums[k] + nums[i] + nums[left] + nums[right] > target 会溢出 if (nums[k] + nums[i] > target - (nums[left] + nums[right])) { right--; + // 当前元素不合适了,可以去重 + while (left < right && nums[right] == nums[right + 1]) right--; // nums[k] + nums[i] + nums[left] + nums[right] < target 会溢出 } else if (nums[k] + nums[i] < target - (nums[left] + nums[right])) { left++; + // 不合适,去重 + while (left < right && nums[left] == nums[left - 1]) left++; } else { result.push_back(vector{nums[k], nums[i], nums[left], nums[right]}); // 去重逻辑应该放在找到一个四元组之后 From fbf52ee2bf0cad8c4231311ef2d87fc383a49a3c Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 11 Feb 2022 16:57:22 +0800 Subject: [PATCH 76/86] =?UTF-8?q?=E7=AE=97=E6=B3=95=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20typescript=20=E7=89=88=E6=9C=AC=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...27\346\263\225\346\250\241\346\235\277.md" | 265 +++++++++++++++++- 1 file changed, 264 insertions(+), 1 deletion(-) 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 a41365115f..789d8f8031 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" @@ -394,7 +394,7 @@ var postorder = function (root, list) { ```javascript var preorderTraversal = function (root) { let res = []; - if (root === null) return rs; + if (root === null) return res; let stack = [root], cur = null; while (stack.length) { @@ -536,6 +536,269 @@ function backtracking(参数) { } ``` +TypeScript: + +## 二分查找法 + +使用左闭右闭区间 + +```typescript +var search = function (nums: number[], target: number): number { + let left: number = 0, right: number = nums.length - 1; + // 使用左闭右闭区间 + while (left <= right) { + let mid: number = left + Math.floor((right - left)/2); + if (nums[mid] > target) { + right = mid - 1; // 去左面闭区间寻找 + } else if (nums[mid] < target) { + left = mid + 1; // 去右面闭区间寻找 + } else { + return mid; + } + } + return -1; +}; +``` + +使用左闭右开区间 + +```typescript +var search = function (nums: number[], target: number): number { + let left: number = 0, right: number = nums.length; + // 使用左闭右开区间 [left, right) + while (left < right) { + let mid: number = left + Math.floor((right - left)/2); + if (nums[mid] > target) { + right = mid; // 去左面闭区间寻找 + } else if (nums[mid] < target) { + left = mid + 1; // 去右面闭区间寻找 + } else { + return mid; + } + } + return -1; +}; +``` + +## KMP + +```typescript +var kmp = function (next: number[], s: number): void { + next[0] = -1; + let j: number = -1; + for(let i: number = 1; i < s.length; i++){ + while (j >= 0 && s[i] !== s[j + 1]) { + j = next[j]; + } + if (s[i] === s[j + 1]) { + j++; + } + next[i] = j; + } +} +``` + +## 二叉树 + +### 深度优先遍历(递归) + +二叉树节点定义: + +```typescript +class TreeNode { + val: number + left: TreeNode | null + right: TreeNode | null + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = (val===undefined ? 0 : val) + this.left = (left===undefined ? null : left) + this.right = (right===undefined ? null : right) + } +} +``` + +前序遍历(中左右): + +```typescript +var preorder = function (root: TreeNode | null, list: number[]): void { + if (root === null) return; + list.push(root.val); // 中 + preorder(root.left, list); // 左 + preorder(root.right, list); // 右 +} +``` + +中序遍历(左中右): + +```typescript +var inorder = function (root: TreeNode | null, list: number[]): void { + if (root === null) return; + inorder(root.left, list); // 左 + list.push(root.val); // 中 + inorder(root.right, list); // 右 +} +``` + +后序遍历(左右中): + +```typescript +var postorder = function (root: TreeNode | null, list: number[]): void { + if (root === null) return; + postorder(root.left, list); // 左 + postorder(root.right, list); // 右 + list.push(root.val); // 中 +} +``` + +### 深度优先遍历(迭代) + +前序遍历(中左右): + +```typescript +var preorderTraversal = function (root: TreeNode | null): number[] { + let res: number[] = []; + if (root === null) return res; + let stack: TreeNode[] = [root], + cur: TreeNode | null = null; + while (stack.length) { + cur = stack.pop(); + res.push(cur.val); + cur.right && stack.push(cur.right); + cur.left && stack.push(cur.left); + } + return res; +}; +``` + +中序遍历(左中右): + +```typescript +var inorderTraversal = function (root: TreeNode | null): number[] { + let res: number[] = []; + if (root === null) return res; + let stack: TreeNode[] = []; + let cur: TreeNode | null = root; + while (stack.length !== 0 || cur !== null) { + if (cur !== null) { + stack.push(cur); + cur = cur.left; + } else { + cur = stack.pop(); + res.push(cur.val); + cur = cur.right; + } + } + return res; +}; +``` + +后序遍历(左右中): + +```typescript +var postorderTraversal = function (root: TreeNode | null): number[] { + let res: number[] = []; + if (root === null) return res; + let stack: TreeNode[] = [root]; + let cur: TreeNode | null = null; + while (stack.length) { + cur = stack.pop(); + res.push(cur.val); + cur.left && stack.push(cur.left); + cur.right && stack.push(cur.right); + } + return res.reverse() +}; +``` + +### 广度优先遍历(队列) + +```typescript +var levelOrder = function (root: TreeNode | null): number[] { + let res: number[] = []; + if (root === null) return res; + let queue: TreeNode[] = [root]; + while (queue.length) { + let n: number = queue.length; + let temp: number[] = []; + for (let i: number = 0; i < n; i++) { + let node: TreeNode = queue.shift(); + temp.push(node.val); + node.left && queue.push(node.left); + node.right && queue.push(node.right); + } + res.push(temp); + } + return res; +}; +``` + +### 二叉树深度 + +```typescript +var getDepth = function (node: TreNode | null): number { + if (node === null) return 0; + return 1 + Math.max(getDepth(node.left), getDepth(node.right)); +} +``` + +### 二叉树节点数量 + +```typescript +var countNodes = function (root: TreeNode | null): number { + if (root === null) return 0; + return 1 + countNodes(root.left) + countNodes(root.right); +} +``` + +## 回溯算法 + +```typescript +function backtracking(参数) { + if (终止条件) { + 存放结果; + return; + } + + for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) { + 处理节点; + backtracking(路径,选择列表); // 递归 + 回溯,撤销处理结果 + } +} + +``` + +## 并查集 + +```typescript + let n: number = 1005; // 根据题意而定 + let father: number[] = new Array(n).fill(0); + + // 并查集初始化 + function init () { + for (int i: number = 0; i < n; ++i) { + father[i] = i; + } + } + // 并查集里寻根的过程 + function find (u: number): number { + return u === father[u] ? u : father[u] = find(father[u]); + } + // 将v->u 这条边加入并查集 + function join(u: number, v: number) { + u = find(u); + v = find(v); + if (u === v) return ; + father[v] = u; + } + // 判断 u 和 v是否找到同一个根 + function same(u: number, v: number): boolean { + u = find(u); + v = find(v); + return u === v; + } +``` + Java: From 65a341bf365eb3b2648c357a7a311a59ec6e9e18 Mon Sep 17 00:00:00 2001 From: Larry Liu Date: Fri, 11 Feb 2022 20:24:45 +0800 Subject: [PATCH 77/86] =?UTF-8?q?Update=20=E5=B9=BF=E5=B7=9E=E4=BA=92?= =?UTF-8?q?=E8=81=94=E7=BD=91=E5=85=AC=E5=8F=B8=E6=80=BB=E7=BB=93.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 字节跳动有广州研发岗位 --- ...7\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\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/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index ae41c89972..ac70f40cb1 100644 --- "a/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\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/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -14,6 +14,7 @@ ## 一线互联网 * 微信(总部) 有点难进! +* 字节跳动(广州) ## 二线 * 网易(总部)主要是游戏 From 961ae6eadf6359f2ef6b787ad612475e02e6842e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 11 Feb 2022 21:52:36 +0800 Subject: [PATCH 78/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) 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 5ec8ffd051..de6caef02c 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" @@ -766,11 +766,100 @@ let pathSum = function(root, targetSum) { }; ``` +## TypeScript + +> 0112.路径总和 + +**递归法:** + +```typescript +function hasPathSum(root: TreeNode | null, targetSum: number): boolean { + function recur(node: TreeNode, sum: number): boolean { + console.log(sum); + if ( + node.left === null && + node.right === null && + sum === 0 + ) return true; + if (node.left !== null) { + sum -= node.left.val; + if (recur(node.left, sum) === true) return true; + sum += node.left.val; + } + if (node.right !== null) { + sum -= node.right.val; + if (recur(node.right, sum) === true) return true; + sum += node.right.val; + } + return false; + } + if (root === null) return false; + return recur(root, targetSum - root.val); +}; +``` + +**递归法(精简版):** + +```typescript +function hasPathSum(root: TreeNode | null, targetSum: number): boolean { + if (root === null) return false; + targetSum -= root.val; + if ( + root.left === null && + root.right === null && + targetSum === 0 + ) return true; + return hasPathSum(root.left, targetSum) || + hasPathSum(root.right, targetSum); +}; +``` + +**迭代法:** + +```typescript +function hasPathSum(root: TreeNode | null, targetSum: number): boolean { + type Pair = { + node: TreeNode, // 当前节点 + sum: number // 根节点到当前节点的路径数值总和 + } + + const helperStack: Pair[] = []; + if (root !== null) helperStack.push({ node: root, sum: root.val }); + let tempPair: Pair; + while (helperStack.length > 0) { + tempPair = helperStack.pop()!; + if ( + tempPair.node.left === null && + tempPair.node.right === null && + tempPair.sum === targetSum + ) return true; + if (tempPair.node.right !== null) { + helperStack.push({ + node: tempPair.node.right, + sum: tempPair.sum + tempPair.node.right.val + }); + } + if (tempPair.node.left !== null) { + helperStack.push({ + node: tempPair.node.left, + sum: tempPair.sum + tempPair.node.left.val + }); + } + } + return false; +}; +``` + +> 0112.路径总和 ii + + + ## Swift 0112.路径总和 **递归** + ```swift func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { guard let root = root else { From fde074cb2ab400b44707c178139d7757541bbf39 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 11 Feb 2022 23:28:30 +0800 Subject: [PATCH 79/86] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20ii=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...57\345\276\204\346\200\273\345\222\214.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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 de6caef02c..2e10d98d2c 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" @@ -852,7 +852,39 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { > 0112.路径总和 ii +**递归法:** +```typescript +function pathSum(root: TreeNode | null, targetSum: number): number[][] { + function recur(node: TreeNode, sumGap: number, routeArr: number[]): void { + if ( + node.left === null && + node.right === null && + sumGap === 0 + ) resArr.push([...routeArr]); + if (node.left !== null) { + sumGap -= node.left.val; + routeArr.push(node.left.val); + recur(node.left, sumGap, routeArr); + sumGap += node.left.val; + routeArr.pop(); + } + if (node.right !== null) { + sumGap -= node.right.val; + routeArr.push(node.right.val); + recur(node.right, sumGap, routeArr); + sumGap += node.right.val; + routeArr.pop(); + } + } + const resArr: number[][] = []; + if (root === null) return resArr; + const routeArr: number[] = []; + routeArr.push(root.val); + recur(root, targetSum - root.val, routeArr); + return resArr; +}; +``` ## Swift From 266702c291505ab3847d520ab7474789d0e97fab Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Fri, 18 Feb 2022 22:09:38 +0800 Subject: [PATCH 80/86] Update --- ...236\204\345\273\272\344\272\214\345\217\211\346\240\221.md" | 3 --- ...203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" | 3 --- ...253\237\346\230\257\345\244\232\345\244\247\357\274\237.md" | 3 --- ...275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...\210\346\230\257ACM\346\250\241\345\274\217\357\274\237.md" | 3 --- ...203\275\345\234\250\350\277\231\351\207\214\357\274\201.md" | 3 --- ...207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" | 3 --- ...255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" | 3 --- ...274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" | 3 --- ...275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" | 3 --- ...206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" | 3 --- ...250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" | 3 --- ...244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" | 3 --- ...227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" | 3 --- 18 files changed, 54 deletions(-) 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 d3b2656eba..fc7a1823bf 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" @@ -251,7 +251,4 @@ int main() { ``` ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)

diff --git "a/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" "b/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" index c579773969..6678860d65 100644 --- "a/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" +++ "b/problems/\345\211\215\345\272\217/BAT\347\272\247\345\210\253\346\212\200\346\234\257\351\235\242\350\257\225\346\265\201\347\250\213\345\222\214\346\263\250\346\204\217\344\272\213\351\241\271\351\203\275\345\234\250\350\277\231\351\207\214\344\272\206.md" @@ -218,7 +218,4 @@ leetcode是专门针对算法练习的题库,leetcode现在也推出了中文 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/On\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/\345\211\215\345\272\217/On\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 9a56937c95..5257ceb94d 100644 --- "a/problems/\345\211\215\345\272\217/On\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/\345\211\215\345\272\217/On\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" @@ -280,7 +280,4 @@ public class TimeComplexity { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\344\270\212\346\265\267\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/\344\270\212\346\265\267\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index 08c1589511..ffcbe77b4c 100644 --- "a/problems/\345\211\215\345\272\217/\344\270\212\346\265\267\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/\344\270\212\346\265\267\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -130,7 +130,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
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 3c5fb4e47f..54c5b6ecc7 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" @@ -119,7 +119,4 @@ int main() { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\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\211\215\345\272\217/\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 cfcbed1a6e..478b82e4f3 100644 --- "a/problems/\345\211\215\345\272\217/\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\211\215\345\272\217/\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" @@ -170,7 +170,4 @@ $O(2 × n^2 + 10 × n + 1000) < O(3 × n^2)$,所以说最后省略掉常数项 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" index 95ffe597be..d49b42a2a1 100644 --- "a/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\345\205\263\344\272\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\357\274\214\345\217\257\350\203\275\346\234\211\345\207\240\344\270\252\347\226\221\351\227\256\357\274\237.md" @@ -73,7 +73,4 @@ for (int i = 0; i < n; i++) { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" index 3fccfb2278..0364fc8b4a 100644 --- "a/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\345\210\267\344\272\206\350\277\231\344\271\210\345\244\232\351\242\230\357\274\214\344\275\240\344\272\206\350\247\243\350\207\252\345\267\261\344\273\243\347\240\201\347\232\204\345\206\205\345\255\230\346\266\210\350\200\227\344\271\210\357\274\237.md" @@ -150,7 +150,4 @@ char型的数据和int型的数据挨在一起,该int数据从地址1开始, ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" index c4899a2068..dca6eec37b 100644 --- "a/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" +++ "b/problems/\345\211\215\345\272\217/\345\212\233\346\211\243\344\270\212\347\232\204\344\273\243\347\240\201\346\203\263\345\234\250\346\234\254\345\234\260\347\274\226\350\257\221\350\277\220\350\241\214\357\274\237.md" @@ -67,7 +67,4 @@ int main() { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\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/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index 0e22dad640..02a877b7c6 100644 --- "a/problems/\345\211\215\345\272\217/\345\214\227\344\272\254\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/\345\214\227\344\272\254\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -116,7 +116,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\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/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index ae41c89972..1cf0da36b3 100644 --- "a/problems/\345\211\215\345\272\217/\345\271\277\345\267\236\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/\345\271\277\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -79,7 +79,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\346\210\220\351\203\275\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\210\220\351\203\275\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index d44800cd2d..7964f23c64 100644 --- "a/problems/\345\211\215\345\272\217/\346\210\220\351\203\275\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\210\220\351\203\275\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -77,7 +77,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\346\235\255\345\267\236\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\235\255\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" index 326a176b73..029ee38089 100644 --- "a/problems/\345\211\215\345\272\217/\346\235\255\345\267\236\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\235\255\345\267\236\344\272\222\350\201\224\347\275\221\345\205\254\345\217\270\346\200\273\347\273\223.md" @@ -87,7 +87,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
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 9e08931549..61bd52e8da 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" @@ -82,7 +82,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" index e4193c426a..5504ae7ac5 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\345\206\231\346\226\207\346\241\243\345\267\245\345\205\267.md" @@ -136,7 +136,4 @@ Markdown支持部分html,例如这样 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" index f47516dc51..522fc5f752 100644 --- "a/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" +++ "b/problems/\345\211\215\345\272\217/\347\250\213\345\272\217\345\221\230\347\256\200\345\216\206.md" @@ -133,7 +133,4 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" index 4dd340a6a2..914cccfdd6 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\344\270\216\347\251\272\351\227\264\345\244\215\346\235\202\345\272\246\345\210\206\346\236\220.md" @@ -269,7 +269,4 @@ int binary_search( int arr[], int l, int r, int x) { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" index 8780122f3f..849a025d7f 100644 --- "a/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" +++ "b/problems/\345\211\215\345\272\217/\351\200\232\350\277\207\344\270\200\351\201\223\351\235\242\350\257\225\351\242\230\347\233\256\357\274\214\350\256\262\344\270\200\350\256\262\351\200\222\345\275\222\347\256\227\346\263\225\347\232\204\346\227\266\351\227\264\345\244\215\346\235\202\345\272\246\357\274\201.md" @@ -152,7 +152,4 @@ int function3(int x, int n) { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
From 3652d00e542fc251d479a66eb676c8e1ac910f45 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Thu, 24 Feb 2022 17:49:15 +0800 Subject: [PATCH 81/86] =?UTF-8?q?0104=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=20=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B9=A6=E5=86=99=E9=94=99=E8=AF=AF=E7=BA=A0?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 函数错误,逻辑错误 --- ...346\234\200\345\244\247\346\267\261\345\272\246.md" | 10 ++++++---- 1 file changed, 6 insertions(+), 4 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 3eecdc927b..2229a85434 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" @@ -523,8 +523,8 @@ func maxdepth(root *treenode) int { ```javascript var maxdepth = function(root) { - if (!root) return root - return 1 + math.max(maxdepth(root.left), maxdepth(root.right)) + if (root === null) return 0; + return 1 + Math.max(maxdepth(root.left), maxdepth(root.right)) }; ``` @@ -541,7 +541,7 @@ var maxdepth = function(root) { //3. 确定单层逻辑 let leftdepth=getdepth(node.left); let rightdepth=getdepth(node.right); - let depth=1+math.max(leftdepth,rightdepth); + let depth=1+Math.max(leftdepth,rightdepth); return depth; } return getdepth(root); @@ -591,7 +591,9 @@ var maxDepth = function(root) { count++ while(size--) { let node = queue.shift() - node && (queue = [...queue, ...node.children]) + for (let item of node.children) { + item && queue.push(item); + } } } return count From 14f91a53702d8df5e1a587e757f739b5df55bb8f Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Fri, 25 Feb 2022 10:37:17 +0800 Subject: [PATCH 82/86] =?UTF-8?q?0404=20=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20=20=E5=B7=A6=E5=8F=B6=E5=AD=90=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\267\246\345\217\266\345\255\220\344\271\213\345\222\214.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 09272052de..691c0f3799 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" @@ -19,7 +19,7 @@ **首先要注意是判断左叶子,不是二叉树左侧节点,所以不要上来想着层序遍历。** -因为题目中其实没有说清楚左叶子究竟是什么节点,那么我来给出左叶子的明确定义:**如果左节点不为空,且左节点没有左右孩子,那么这个节点就是左叶子** +因为题目中其实没有说清楚左叶子究竟是什么节点,那么我来给出左叶子的明确定义:**如果左节点不为空,且左节点没有左右孩子,那么这个节点的左节点就是左叶子** 大家思考一下如下图中二叉树,左叶子之和究竟是多少? From 37e7d73ec200bd9adefbdaaef24d6f4c2e38f5f7 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Mon, 28 Feb 2022 20:52:01 +0800 Subject: [PATCH 83/86] Update --- ...46\225\210\347\232\204\346\213\254\345\217\267.md" | 4 ++-- ...47\247\273\351\231\244\345\205\203\347\264\240.md" | 2 +- ...46\217\222\345\205\245\344\275\215\347\275\256.md" | 2 +- ...47\273\204\345\220\210\346\200\273\345\222\214.md" | 4 ++-- ...\273\204\345\220\210\346\200\273\345\222\214II.md" | 4 ++-- .../0042.\346\216\245\351\233\250\346\260\264.md" | 2 +- .../0046.\345\205\250\346\216\222\345\210\227.md" | 2 +- ...\236\272\346\227\213\347\237\251\351\230\265II.md" | 4 ++-- ...50\203\214\345\214\205\347\211\210\346\234\254.md" | 4 ++-- ...47\273\204\345\220\210\344\274\230\345\214\226.md" | 2 +- "problems/0078.\345\255\220\351\233\206.md" | 2 +- ...45\244\247\347\232\204\347\237\251\345\275\242.md" | 4 ++-- ...\244\215\345\216\237IP\345\234\260\345\235\200.md" | 2 +- ...45\261\202\345\272\217\351\201\215\345\216\206.md" | 4 ++-- ...45\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- ...50\241\241\344\272\214\345\217\211\346\240\221.md" | 2 +- ...46\225\260\345\255\227\344\271\213\345\222\214.md" | 2 +- ...45\211\262\345\233\236\346\226\207\344\270\262.md" | 4 ++-- ...\234\200\344\275\263\346\227\266\346\234\272IV.md" | 2 +- ...46\211\200\346\234\211\350\267\257\345\276\204.md" | 2 +- ...45\205\250\345\271\263\346\226\271\346\225\260.md" | 2 +- ...51\233\266\351\222\261\345\205\221\346\215\242.md" | 2 +- ...211\223\345\256\266\345\212\253\350\210\215III.md" | 6 +++--- ...46\221\206\345\212\250\345\272\217\345\210\227.md" | 2 +- .../0383.\350\265\216\351\207\221\344\277\241.md" | 2 +- ...45\217\266\345\255\220\344\271\213\345\222\214.md" | 4 ++-- ...45\210\206\345\217\221\351\245\274\345\271\262.md" | 2 +- ...45\261\277\347\232\204\345\221\250\351\225\277.md" | 2 +- .../0474.\344\270\200\345\222\214\351\233\266.md" | 2 +- ...45\242\236\345\255\220\345\272\217\345\210\227.md" | 4 ++-- .../0494.\347\233\256\346\240\207\345\222\214.md" | 11 ++++++++--- ...6\233\264\345\244\247\345\205\203\347\264\240I.md" | 4 ++-- ...44\270\255\347\232\204\344\274\227\346\225\260.md" | 4 ++-- ...\233\264\345\244\247\345\205\203\347\264\240II.md" | 2 +- ...\233\266\351\222\261\345\205\221\346\215\242II.md" | 2 +- ...44\270\272\347\264\257\345\212\240\346\240\221.md" | 2 +- ...45\217\211\346\220\234\347\264\242\346\240\221.md" | 2 +- ...45\214\231\345\222\214\346\210\277\351\227\264.md" | 1 - ...51\202\273\351\207\215\345\244\215\351\241\271.md" | 4 ++-- ...51\200\222\345\275\222\351\201\215\345\216\206.md" | 2 +- ...45\221\250\346\234\253\346\200\273\347\273\223.md" | 4 ++-- ...45\256\214\345\205\250\350\203\214\345\214\205.md" | 2 +- 42 files changed, 64 insertions(+), 60 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 95d62e42a0..01fc7b93e8 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" @@ -159,7 +159,7 @@ class Solution { ``` Python: -```python3 +```python # 方法一,仅使用栈,更省空间 class Solution: def isValid(self, s: str) -> bool: @@ -180,7 +180,7 @@ class Solution: return True if not stack else False ``` -```python3 +```python # 方法二,使用字典 class Solution: def isValid(self, s: str) -> bool: 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 d69f2bcfe8..dd9155c693 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" @@ -142,7 +142,7 @@ class Solution { Python: -```python3 +```python class Solution: """双指针法 时间复杂度:O(n) diff --git "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" index f5f041aa7f..9a770703e3 100644 --- "a/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" +++ "b/problems/0035.\346\220\234\347\264\242\346\217\222\345\205\245\344\275\215\347\275\256.md" @@ -246,7 +246,7 @@ func searchInsert(nums []int, target int) int { ``` ### Python -```python3 +```python class Solution: def searchInsert(self, nums: List[int], target: int) -> int: left, right = 0, len(nums) - 1 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 0f8fe4f6b8..7a2084dd19 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" @@ -264,7 +264,7 @@ class Solution { ## Python **回溯** -```python3 +```python class Solution: def __init__(self): self.path = [] @@ -296,7 +296,7 @@ class Solution: self.path.pop() # 回溯 ``` **剪枝回溯** -```python3 +```python class Solution: def __init__(self): self.path = [] 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 5438418807..49acb8d63f 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" @@ -334,7 +334,7 @@ class Solution { ## Python **回溯+巧妙去重(省去使用used** -```python3 +```python class Solution: def __init__(self): self.paths = [] @@ -374,7 +374,7 @@ class Solution: sum_ -= candidates[i] # 回溯,为了下一轮for loop ``` **回溯+去重(使用used)** -```python3 +```python class Solution: def __init__(self): self.paths = [] 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 2774560794..55f80a5407 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" @@ -491,7 +491,7 @@ class Solution: return res ``` 动态规划 -```python3 +```python class Solution: def trap(self, height: List[int]) -> int: leftheight, rightheight = [0]*len(height), [0]*len(height) 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 3d31db6212..c5369ddd1b 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" @@ -243,7 +243,7 @@ class Solution: usage_list[i] = False ``` **回溯+丢掉usage_list** -```python3 +```python class Solution: def __init__(self): self.path = [] 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 3f7a59ca2f..670d6fc277 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" @@ -188,9 +188,9 @@ class Solution { } ``` -python: +python3: -```python3 +```python class Solution: def generateMatrix(self, n: int) -> List[List[int]]: 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 b5fbb96a19..2286de2d1f 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" @@ -143,10 +143,10 @@ class Solution { } ``` -Python: +Python3: -```python3 +```python class Solution: def climbStairs(self, n: int) -> int: dp = [0]*(n + 1) 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 e995fd1847..81b4304cee 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" @@ -174,7 +174,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res=[] #存放符合条件结果的集合 diff --git "a/problems/0078.\345\255\220\351\233\206.md" "b/problems/0078.\345\255\220\351\233\206.md" index 1abf8c9553..cdb5f54891 100644 --- "a/problems/0078.\345\255\220\351\233\206.md" +++ "b/problems/0078.\345\255\220\351\233\206.md" @@ -203,7 +203,7 @@ class Solution { ``` ## Python -```python3 +```python class Solution: def __init__(self): self.path: List[int] = [] 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 3cb51f1d82..439a3bc5b7 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" @@ -277,9 +277,9 @@ class Solution { } ``` -Python: +Python3: -```python3 +```python # 双指针;暴力解法(leetcode超时) class Solution: 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 3e7cd1ade2..1fa72cc97b 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" @@ -339,7 +339,7 @@ class Solution(object): ``` python3: -```python3 +```python class Solution: def __init__(self): self.result = [] 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 1a92d42f91..7448584854 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" @@ -83,10 +83,10 @@ public: }; ``` -python代码: +python3代码: -```python3 +```python class Solution: """二叉树层序遍历迭代解法""" 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 bd48ea0c96..9e008e867e 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" @@ -305,7 +305,7 @@ class Solution { ## Python **递归** -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): 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 90cb7336d9..d9dcf289b3 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" @@ -497,7 +497,7 @@ class Solution { ## Python 递归法: -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): 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 980779c20c..b271ca7de5 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" @@ -217,7 +217,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def sumNumbers(self, root: TreeNode) -> int: res = 0 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 439ad8ea13..f50f1c1d11 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" @@ -289,7 +289,7 @@ class Solution { ## Python **回溯+正反序判断回文串** -```python3 +```python class Solution: def __init__(self): self.paths = [] @@ -326,7 +326,7 @@ class Solution: continue ``` **回溯+函数判断回文串** -```python3 +```python class Solution: def __init__(self): self.paths = [] 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 7db75f060c..61c558a1a1 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" @@ -271,7 +271,7 @@ class Solution: return dp[-1][2*k] ``` 版本二 -```python3 +```python class Solution: def maxProfit(self, k: int, prices: List[int]) -> int: if len(prices) == 0: return 0 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 4078320f23..a0c718f45f 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" @@ -436,7 +436,7 @@ class Solution: 迭代法: -```python3 +```python from collections import deque 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 7bc0c2f721..9bad2085e2 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" @@ -207,7 +207,7 @@ class Solution { Python: -```python3 +```python class Solution: def numSquares(self, n: int) -> int: '''版本一,先遍历背包, 再遍历物品''' 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 8f3438af47..3a8d0662ab 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" @@ -207,7 +207,7 @@ class Solution { Python: -```python3 +```python class Solution: def coinChange(self, coins: List[int], amount: int) -> int: '''版本一''' 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 06831cb6ab..ecd31d1b01 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" @@ -288,7 +288,7 @@ Python: > 暴力递归 -```python3 +```python # Definition for a binary tree node. # class TreeNode: @@ -315,7 +315,7 @@ class Solution: > 记忆化递归 -```python3 +```python # Definition for a binary tree node. # class TreeNode: @@ -345,7 +345,7 @@ class Solution: ``` > 动态规划 -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): 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 d75311eb2a..5076c9adaa 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" @@ -228,7 +228,7 @@ class Solution { ### Python -```python3 +```python class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: preC,curC,res = 0,0,1 #题目里nums长度大于等于1,当长度为1时,其实到不了for循环里去,所以不用考虑nums长度 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 31e19b1005..0070734779 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" @@ -209,7 +209,7 @@ class Solution(object): Python写法四: -```python3 +```python class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: c1 = collections.Counter(ransomNote) 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 09272052de..8c6eaddb30 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" @@ -229,7 +229,7 @@ class Solution { ## Python **递归后序遍历** -```python3 +```python class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: if not root: @@ -246,7 +246,7 @@ class Solution: ``` **迭代** -```python3 +```python class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int: """ 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 1711f63836..210b492df6 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" @@ -146,7 +146,7 @@ class Solution { ``` ### Python -```python3 +```python class Solution: # 思路1:优先考虑胃饼干 def findContentChildren(self, g: List[int], s: List[int]) -> int: 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 e19e06ee5e..9911dfe5a3 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" @@ -124,7 +124,7 @@ Python: ### 解法1: 扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 -```python3 +```python class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: 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 67e366f436..964df4a829 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" @@ -193,7 +193,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0 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 6d88119e7c..70b08d50a5 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" @@ -233,7 +233,7 @@ class Solution { python3 **回溯** -```python3 +```python class Solution: def __init__(self): self.paths = [] @@ -270,7 +270,7 @@ class Solution: self.path.pop() ``` **回溯+哈希表去重** -```python3 +```python class Solution: def __init__(self): self.paths = [] 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 f190b734f3..47d0784ee4 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" @@ -160,11 +160,16 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[j]种方法 那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。 -举一个例子,nums[i] = 2: dp[3],填满背包容量为3的话,有dp[3]种方法。 -那么只需要搞到一个2(nums[i]),有dp[3]方法可以凑齐容量为3的背包,相应的就有多少种方法可以凑齐容量为5的背包。 +例如:dp[j],j 为5, -那么需要把 这些方法累加起来就可以了,dp[j] += dp[j - nums[i]] +* 已经有一个1(nums[i]) 的话,有 dp[4]种方法 凑成 dp[5]。 +* 已经有一个2(nums[i]) 的话,有 dp[3]种方法 凑成 dp[5]。 +* 已经有一个3(nums[i]) 的话,有 dp[2]中方法 凑成 dp[5] +* 已经有一个4(nums[i]) 的话,有 dp[1]中方法 凑成 dp[5] +* 已经有一个5 (nums[i])的话,有 dp[0]中方法 凑成 dp[5] + +那么凑整dp[5]有多少方法呢,也就是把 所有的 dp[j - nums[i]] 累加起来。 所以求组合类问题的公式,都是类似这种: 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 f039c19854..f9dfa3081b 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" @@ -222,8 +222,8 @@ class Solution { } } ``` -Python: -```python3 +Python3: +```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [-1]*len(nums1) 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 277f46f52e..a2984eccb3 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" @@ -470,7 +470,7 @@ class Solution { > 递归法 -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): @@ -515,7 +515,7 @@ class Solution: > 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性 -```python3 +```python class Solution: def findMode(self, root: TreeNode) -> List[int]: stack = [] 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 c9532a2291..36e183e1b1 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" @@ -124,7 +124,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: dp = [-1] * len(nums) 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 be60ac1333..e72c5f8515 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" @@ -207,7 +207,7 @@ class Solution { Python: -```python3 +```python class Solution: def change(self, amount: int, coins: List[int]) -> int: dp = [0]*(amount + 1) 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 1d11d4ee0e..1b07b8034f 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" @@ -196,7 +196,7 @@ class Solution { ## Python **递归** -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): 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 d17c0b1c1c..15f6a04093 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" @@ -264,7 +264,7 @@ class Solution { ## Python **递归** -```python3 +```python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): diff --git "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" index 8397690e5c..1cd130653b 100644 --- "a/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" +++ "b/problems/0841.\351\222\245\345\214\231\345\222\214\346\210\277\351\227\264.md" @@ -152,7 +152,6 @@ class Solution { -Python: python3 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 9a0bb1c1bd..b94e557de8 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" @@ -194,7 +194,7 @@ class Solution { ``` Python: -```python3 +```python # 方法一,使用栈,推荐! class Solution: def removeDuplicates(self, s: str) -> str: @@ -207,7 +207,7 @@ class Solution: return "".join(res) # 字符串拼接 ``` -```python3 +```python # 方法二,使用双指针模拟栈,如果不让用栈可以作为备选方法。 class Solution: def removeDuplicates(self, s: str) -> str: 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 c481fd1181..2fef68daf0 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" @@ -168,7 +168,7 @@ class Solution { ``` Python: -```python3 +```python # 前序遍历-递归-LC144_二叉树的前序遍历 class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: diff --git "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" index a0b8c2dd3c..18bbf37f97 100644 --- "a/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" +++ "b/problems/\345\221\250\346\200\273\347\273\223/20201003\344\272\214\345\217\211\346\240\221\345\221\250\346\234\253\346\200\273\347\273\223.md" @@ -34,8 +34,8 @@ public: // 此时就是:左右节点都不为空,且数值相同的情况 // 此时才做递归,做下一层的判断 - bool outside = compare(left->left, right->right); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序) - bool inside = compare(left->right, right->left); // 左子树:右、 右子树:右 + bool outside = compare(left->left, right->left); // 左子树:左、 右子树:左 (相对于求对称二叉树,只需改一下这里的顺序) + bool inside = compare(left->right, right->right); // 左子树:右、 右子树:右 bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理) return isSame; 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 cea69c724c..7abaf16000 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" @@ -216,7 +216,7 @@ private static void testCompletePackAnotherWay(){ Python: -```python3 +```python # 先遍历物品,再遍历背包 def test_complete_pack1(): weight = [1, 3, 4] From 1ed3e9e8f4dff921e983e636f85f963a38839e05 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 1 Mar 2022 17:12:19 +0800 Subject: [PATCH 84/86] Update --- ...\217\211\346\240\221\346\200\273\347\273\223\347\257\207.md" | 2 +- ...\247\204\345\210\222\346\200\273\347\273\223\347\257\207.md" | 2 +- "problems/\345\233\236\346\272\257\346\200\273\347\273\223.md" | 2 +- ...\256\227\346\263\225\346\200\273\347\273\223\347\257\207.md" | 2 +- 4 files changed, 4 insertions(+), 4 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 d1332e09a8..73faffa6c5 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" @@ -152,7 +152,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030125421.png) -这个图是 [代码随想录知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 +这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 **最后,二叉树系列就这么完美结束了,估计这应该是最长的系列了,感谢大家33天的坚持与陪伴,接下来我们又要开始新的系列了「回溯算法」!** 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 699d443556..cc973b2314 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" @@ -118,7 +118,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211121223754.png) -这个图是 [代码随想录知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 +这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 这已经是全网对动规最深刻的讲解系列了。 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 af1712439f..671e0a4eb1 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" @@ -432,7 +432,7 @@ N皇后问题分析: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211030124742.png) -这个图是 [代码随想录知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) 成员:[莫非毛](https://wx.zsxq.com/dweb2/index/footprint/828844212542),所画,总结的非常好,分享给大家。 +这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[莫非毛](https://wx.zsxq.com/dweb2/index/footprint/828844212542),所画,总结的非常好,分享给大家。 **回溯算法系列正式结束,新的系列终将开始,录友们准备开启新的征程!** 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 6da43ea31d..1db9b4dc10 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" @@ -129,7 +129,7 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是 ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211110121605.png) -这个图是 [代码随想录知识星球](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842)所画,总结的非常好,分享给大家。 +这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842)所画,总结的非常好,分享给大家。 很多没有接触过贪心的同学都会感觉贪心有啥可学的,但只要跟着「代码随想录」坚持下来之后,就会发现,贪心是一种很重要的算法思维而且并不简单,贪心往往妙的出其不意,触不及防! From ecde43b2fc292edbfaa1fec6b16d6941e879507f Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Sat, 5 Mar 2022 17:12:34 +0800 Subject: [PATCH 85/86] update --- ...0\244\346\226\255\345\255\220\345\272\217\345\210\227.md" | 2 +- ...3\214\345\214\205\346\200\273\347\273\223\347\257\207.md" | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) 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 d64e1fd080..671576f7dc 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" @@ -77,7 +77,7 @@ if (s[i - 1] != t[j - 1]),此时相当于t要删除元素,t如果把当前 如果要是定义的dp[i][j]是以下标i为结尾的字符串s和以下标j为结尾的字符串t,初始化就比较麻烦了。 -这里dp[i][0]和dp[0][j]是没有含义的,仅仅是为了给递推公式做前期铺垫,所以初始化为0。 +dp[i][0] 表示以下标i-1为结尾的字符串,与空字符串的相同子序列长度,所以为0. dp[0][j]同理。 **其实这里只初始化dp[i][0]就够了,但一起初始化也方便,所以就一起操作了**,代码如下: 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 f80bcf29ae..a7852de370 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" @@ -82,6 +82,7 @@ ## 总结 + **这篇背包问题总结篇是对背包问题的高度概括,讲最关键的两部:递推公式和遍历顺序,结合力扣上的题目全都抽象出来了**。 **而且每一个点,我都给出了对应的力扣题目**。 @@ -90,7 +91,11 @@ 如果把我本篇总结出来的内容都掌握的话,可以说对背包问题理解的就很深刻了,用来对付面试中的背包问题绰绰有余! +背包问题总结: + +![](https://code-thinking-1253855093.file.myqcloud.com/pics/背包问题1.jpeg) +这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[海螺人](https://wx.zsxq.com/dweb2/index/footprint/844412858822412),所画结的非常好,分享给大家。 From f6360c701f5e1d251ad69e15c94c05273ff4235c Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Sun, 6 Mar 2022 11:44:39 +0800 Subject: [PATCH 86/86] Update --- README.md | 5 ++--- ...3\272\344\272\214\345\217\211\346\240\221.md" | 16 +--------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c1761f7b02..0bf7fcdf43 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,7 @@ ## 前序 -* [「代码随想录」后序安排](https://mp.weixin.qq.com/s/4eeGJREy6E-v6D7cR_5A4g) -* [「代码随想录」学习社区](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) +* [「代码随想录」学习社区](https://programmercarl.com/other/kstar.html) * 编程语言 @@ -124,7 +123,7 @@ * 算法性能分析 * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) - * [$O(n)$的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md) + * [O(n)的算法居然超时了,此时的n究竟是多大?](./problems/前序/On的算法居然超时了,此时的n究竟是多大?.md) * [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md) * [本周小结!(算法性能分析系列一)](./problems/周总结/20201210复杂度分析周末总结.md) * [关于空间复杂度,可能有几个疑问?](./problems/前序/关于空间复杂度,可能有几个疑问?.md) 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 fc7a1823bf..28c4b6f7ae 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" @@ -45,21 +45,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210914223147.png) -那么此时大家是不是应该知道了,数组如何转化成 二叉树了。**如果父节点的数组下标是i,那么它的左孩子下标就是i * 2 + 1,右孩子下标就是 i * 2 + 2**。计算过程为: - -如果父节点在第$k$层,第$m,m \in [0,2^k]$个节点,则其左孩子所在的位置必然为$k+1$层,第$2*(m-1)+1$个节点。 - -- 计算父节点在数组中的索引: - $$ - index_{father}=(\sum_{i=0}^{i=k-1}2^i)+m-1=2^k-1+m-1 - $$ - -- 计算左子节点在数组的索引: - $$ - index_{left}=(\sum_{i=0}^{i=k}2^i)+2*m-1-1=2^{k+1}+2m-3 - $$ - -- 故左孩子的下表为$index_{left}=index_{father}\times2+1$,同理可得到右子孩子的索引关系。也可以直接在左子孩子的基础上`+1`。 +那么此时大家是不是应该知道了,数组如何转化成 二叉树了。**如果父节点的数组下标是i,那么它的左孩子下标就是i * 2 + 1,右孩子下标就是 i * 2 + 2**。 那么这里又有同学疑惑了,这些我都懂了,但我还是不知道 应该 怎么构造。