From fe2362a97e0dd56ce76c8550c3d9007d53f779d3 Mon Sep 17 00:00:00 2001 From: baici1 <249337001@qq.com> Date: Wed, 3 Nov 2021 10:37:08 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E5=A2=9E=E5=8A=A042=E6=8E=A5=E9=9B=A8?= =?UTF-8?q?=E6=B0=B4=20go=E7=89=88=E6=9C=AC=E7=9A=84=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=A7=84=E5=88=92=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2.\346\216\245\351\233\250\346\260\264.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/problems/0042.\346\216\245\351\233\250\346\260\264.md" "b/problems/0042.\346\216\245\351\233\250\346\260\264.md" index 9b26bc6b01..ca2c99333b 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" @@ -604,7 +604,48 @@ func trap(height []int) int { } ``` +动态规划解法: + +```go +func trap(height []int) int { + sum:=0 + n:=len(height) + lh:=make([]int,n) + rh:=make([]int,n) + lh[0]=height[0] + rh[n-1]=height[n-1] + for i:=1;i=0;i--{ + rh[i]=max(rh[i+1],height[i]) + } + for i:=1;i0{ + sum+=h + } + } + return sum +} +func max(a,b int)int{ + if a>b{ + return a + } + return b +} +func min(a,b int)int{ + if a Date: Thu, 4 Nov 2021 16:42:56 -0400 Subject: [PATCH 02/21] =?UTF-8?q?0028.=20=E5=AE=9E=E7=8E=B0strstr.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改错别字 --- "problems/0028.\345\256\236\347\216\260strStr.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/problems/0028.\345\256\236\347\216\260strStr.md" "b/problems/0028.\345\256\236\347\216\260strStr.md" index 1c200a7108..f8eb0aceb0 100644 --- "a/problems/0028.\345\256\236\347\216\260strStr.md" +++ "b/problems/0028.\345\256\236\347\216\260strStr.md" @@ -120,7 +120,7 @@ next数组就是一个前缀表(prefix table)。 此时就要问了**前缀表是如何记录的呢?** -首先要知道前缀表的任务是当前位置匹配失败,找到之前已经匹配上的位置,在重新匹配,此也意味着在某个字符失配时,前缀表会告诉你下一步匹配中,模式串应该跳到哪个位置。 +首先要知道前缀表的任务是当前位置匹配失败,找到之前已经匹配上的位置,再重新匹配,此也意味着在某个字符失配时,前缀表会告诉你下一步匹配中,模式串应该跳到哪个位置。 那么什么是前缀表:**记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。** @@ -148,7 +148,7 @@ next数组就是一个前缀表(prefix table)。 # 为什么一定要用前缀表 -这就是前缀表那为啥就能告诉我们 上次匹配的位置,并跳过去呢? +这就是前缀表,那为啥就能告诉我们 上次匹配的位置,并跳过去呢? 回顾一下,刚刚匹配的过程在下标5的地方遇到不匹配,模式串是指向f,如图: KMP精讲1 From 36a4a909928c08d40d997a0f493372fb2d039c6b Mon Sep 17 00:00:00 2001 From: Wen Date: Sat, 6 Nov 2021 20:14:43 +0800 Subject: [PATCH 03/21] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200122.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAII=EF=BC=88=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?= =?UTF-8?q?=EF=BC=89.md=20Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 5dfe3f0e3b..5a39c8b1b5 100644 --- "a/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272II\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -199,6 +199,33 @@ class Solution: ``` Go: +```go +// 买卖股票的最佳时机Ⅱ 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int) int { + dp := make([][]int, len(prices)) + status := make([]int, len(prices) * 2) + for i := range dp { + dp[i] = status[:2] + status = status[2:] + } + dp[0][0] = -prices[0] + + for i := 1; i < len(prices); i++ { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]) + } + + return dp[len(prices) - 1][1] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` Javascript: ```javascript From 5483a714e7a3386467c50f49e7e543e04f51b3e5 Mon Sep 17 00:00:00 2001 From: Wen Date: Sat, 6 Nov 2021 20:17:16 +0800 Subject: [PATCH 04/21] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200123.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAIII.md=20Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...344\275\263\346\227\266\346\234\272III.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" index 6a849c802f..81f8e82d62 100644 --- "a/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" +++ "b/problems/0123.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272III.md" @@ -317,7 +317,38 @@ const maxProfit = prices => { }; ``` +Go: +> 版本一: +```go +// 买卖股票的最佳时机III 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int) int { + dp := make([][]int, len(prices)) + status := make([]int, len(prices) * 4) + for i := range dp { + dp[i] = status[:4] + status = status[4:] + } + dp[0][0], dp[0][2] = -prices[0], -prices[0] + + for i := 1; i < len(prices); i++ { + dp[i][0] = max(dp[i - 1][0], -prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]) + dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] - prices[i]) + dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i]) + } + + return dp[len(prices) - 1][3] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` ----------------------- From bc77968c31186313bfd94627e09a49153aace468 Mon Sep 17 00:00:00 2001 From: Wen Date: Sat, 6 Nov 2021 20:21:23 +0800 Subject: [PATCH 05/21] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200188.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BAIV.md=20Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\344\275\263\346\227\266\346\234\272IV.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 bcb8a1ab72..9e3ca11211 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" @@ -275,6 +275,41 @@ class Solution: return dp[2*k] ``` Go: +版本一: +```go +// 买卖股票的最佳时机IV 动态规划 +// 时间复杂度O(kn) 空间复杂度O(kn) +func maxProfit(k int, prices []int) int { + if k == 0 || len(prices) == 0 { + return 0 + } + + dp := make([][]int, len(prices)) + status := make([]int, (2 * k + 1) * len(prices)) + for i := range dp { + dp[i] = status[:2 * k + 1] + status = status[2 * k + 1:] + } + for j := 1; j < 2 * k; j += 2 { + dp[0][j] = -prices[0] + } + + for i := 1; i < len(prices); i++ { + for j := 0; j < 2 * k; j += 2 { + dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]) + dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]) + } + } + return dp[len(prices) - 1][2 * k] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` Javascript: From e1e3b38eef494c4c7840264ea66a06802de4c1f9 Mon Sep 17 00:00:00 2001 From: Wen Date: Sat, 6 Nov 2021 20:29:00 +0800 Subject: [PATCH 06/21] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200309.=E6=9C=80?= =?UTF-8?q?=E4=BD=B3=E4=B9=B0=E5=8D=96=E8=82=A1=E7=A5=A8=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=90=AB=E5=86=B7=E5=86=BB=E6=9C=9F.md=20Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\345\206\267\345\206\273\346\234\237.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" index 59178c6454..f5665a5eae 100644 --- "a/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" +++ "b/problems/0309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237.md" @@ -208,6 +208,40 @@ class Solution: ``` Go: +```go +// 最佳买卖股票时机含冷冻期 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int) int { + n := len(prices) + if n < 2 { + return 0 + } + + dp := make([][]int, n) + status := make([]int, n * 4) + for i := range dp { + dp[i] = status[:4] + status = status[4:] + } + dp[0][0] = -prices[0] + + for i := 1; i < n; i++ { + dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1] - prices[i], dp[i - 1][3] - prices[i])) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]) + dp[i][2] = dp[i - 1][0] + prices[i] + dp[i][3] = dp[i - 1][2] + } + + return max(dp[n - 1][1], max(dp[n - 1][2], dp[n - 1][3])) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` Javascript: From 5c7addcf0ea688a8c1da3ef875f4c2d379a87da5 Mon Sep 17 00:00:00 2001 From: Yu Date: Sat, 6 Nov 2021 17:28:50 -0700 Subject: [PATCH 07/21] Add python3 solution for the quesiton 0463. Thanks! -Kai --- ...77\347\232\204\345\221\250\351\225\277.md" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 3b0278a64d..4aa2246057 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" @@ -123,6 +123,41 @@ class Solution { ``` Python: +### 解法1: +扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。 + +```python3 +class Solution: + def islandPerimeter(self, grid: List[List[int]]) -> int: + + m = len(grid) + n = len(grid[0]) + + # 创建res二维素组记录答案 + res = [[0] * n for j in range(m)] + + for i in range(m): + for j in range(len(grid[i])): + # 如果当前位置为水域,不做修改或reset res[i][j] = 0 + if grid[i][j] == 0: + res[i][j] = 0 + # 如果当前位置为陆地,往四个方向判断,update res[i][j] + elif grid[i][j] == 1: + if i == 0 or (i > 0 and grid[i-1][j] == 0): + res[i][j] += 1 + if j == 0 or (j >0 and grid[i][j-1] == 0): + res[i][j] += 1 + if i == m-1 or (i < m-1 and grid[i+1][j] == 0): + res[i][j] += 1 + if j == n-1 or (j < n-1 and grid[i][j+1] == 0): + res[i][j] += 1 + + # 最后求和res矩阵,这里其实不一定需要矩阵记录,可以设置一个variable res 记录边长,舍矩阵无非是更加形象而已 + ans = sum([sum(row) for row in res]) + + return ans + +``` Go: From f8fbdd4ed1d66bf03578728650310bddc8a5fc7a Mon Sep 17 00:00:00 2001 From: Wen Date: Sun, 7 Nov 2021 12:53:27 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200714.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9=EF=BC=88=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=A7=84=E5=88=92=EF=BC=89.md=20Go=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\357\274\211.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" index 50db8868cd..42afd777aa 100644 --- "a/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" +++ "b/problems/0714.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272\345\220\253\346\211\213\347\273\255\350\264\271\357\274\210\345\212\250\346\200\201\350\247\204\345\210\222\357\274\211.md" @@ -152,6 +152,37 @@ class Solution: ``` Go: +```go +// 买卖股票的最佳时机含手续费 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int, fee int) int { + if len(prices) == 1 { + return 0 + } + + dp := make([][]int, len(prices)) + status := make([]int, len(prices) * 2) + for i := range dp { + dp[i] = status[:2] + status = status[2:] + } + dp[0][0] = -prices[0] + + for i := 1; i < len(dp); i++ { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee) + } + + return dp[len(dp) - 1][1] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` Javascript: ```javascript From 766e9729243cd16357555ff4d1e8ece8166337f7 Mon Sep 17 00:00:00 2001 From: bourne-3 <595962708@qq.com> Date: Sun, 7 Nov 2021 18:59:14 +0800 Subject: [PATCH 09/21] =?UTF-8?q?=E6=9C=80=E9=95=BF=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=97=E4=B8=B2Java=E5=AE=9E=E7=8E=B0?= 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" | 28 +++++++++++++++++++ 1 file changed, 28 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 c78b827c80..bd57206025 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" @@ -265,6 +265,32 @@ public: ## Java ```java +// 双指针 中心扩散法 +class Solution { + public String longestPalindrome(String s) { + String s1 = ""; + String s2 = ""; + String res = ""; + for (int i = 0; i < s.length(); i++) { + // 分两种情况:即一个元素作为中心点,两个元素作为中心点 + s1 = extend(s, i, i); // 情况1 + res = s1.length() > res.length() ? s1 : res; + s2 = extend(s, i, i + 1); // 情况2 + res = s2.length() > res.length() ? s2 : res; + } + return res; // 返回最长的 + } + public String extend(String s, int start, int end){ + String tmp = ""; + while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)){ + tmp = s.substring(start, end + 1); // Java中substring是左闭右开的,所以要+1 + // 向两边扩散 + start--; + end++; + } + return tmp; + } +} ``` ## Python @@ -292,11 +318,13 @@ class Solution: ## Go ```go + ``` ## JavaScript ```js + ``` ----------------------- From bb1dd8d8c33674cacf08ecd77a50ebf072210b8e Mon Sep 17 00:00:00 2001 From: Hugh <34978586+nanqic@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:09:06 +0800 Subject: [PATCH 10/21] =?UTF-8?q?Update=201365.=E6=9C=89=E5=A4=9A=E5=B0=91?= =?UTF-8?q?=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E5=AD=97=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Go语言版本 --- ...27\347\232\204\346\225\260\345\255\227.md" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" index 9f28220922..09d41e7088 100644 --- "a/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" +++ "b/problems/1365.\346\234\211\345\244\232\345\260\221\345\260\217\344\272\216\345\275\223\345\211\215\346\225\260\345\255\227\347\232\204\346\225\260\345\255\227.md" @@ -152,7 +152,35 @@ class Solution: res[i] = hash[num] return res ``` + Go: +```go +func smallerNumbersThanCurrent(nums []int) []int { + // map,key[数组中出现的数] value[比这个数小的个数] + m := make(map[int]int) + // 拷贝一份原始数组 + rawNums := make([]int,len(nums)) + copy(rawNums,nums) + // 将数组排序 + sort.Ints(nums) + // 循环遍历排序后的数组,值为map的key,索引为value + for i,v := range nums { + _,contains := m[v] + if !contains { + m[v] = i + } + + } + // 返回值结果 + result := make([]int,len(nums)) + // 根据原始数组的位置,存放对应的比它小的数 + for i,v := range rawNums { + result[i] = m[v] + } + + return result +} +``` JavaScript: ```javascript From 214b60cc6945fea8d1b22f83f6f67b6ff82cf4c8 Mon Sep 17 00:00:00 2001 From: Hugh <34978586+nanqic@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:09:45 +0800 Subject: [PATCH 11/21] =?UTF-8?q?Create=201365.=E6=9C=89=E5=A4=9A=E5=B0=91?= =?UTF-8?q?=E5=B0=8F=E4=BA=8E=E5=BD=93=E5=89=8D=E6=95=B0=E5=AD=97=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Go语言版本 From 808bdbb8604de47f9e75412d0ebb85f92e33fedf Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 8 Nov 2021 11:50:39 +0000 Subject: [PATCH 12/21] =?UTF-8?q?=E6=9B=B4=E6=94=B9=200226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...277\273\350\275\254\344\272\214\345\217\211\346\240\221.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 36083dcd62..f9185959e8 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" @@ -565,7 +565,7 @@ var invertTree = function(root) { }; ``` -C: +### C: 递归法 ```c struct TreeNode* invertTree(struct TreeNode* root){ @@ -582,6 +582,7 @@ struct TreeNode* invertTree(struct TreeNode* root){ return root; } ``` + 迭代法:深度优先遍历 ```c struct TreeNode* invertTree(struct TreeNode* root){ From 61044a1f94583e817dbf799e8d5c9b59da800e8b Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 8 Nov 2021 12:20:52 +0000 Subject: [PATCH 13/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0.md=20C=E8=AF=AD=E8=A8=80=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=E3=80=81=E9=80=92=E5=BD=92=E6=B3=95?= 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" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) 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 dc09985d47..0826110f81 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" @@ -449,7 +449,51 @@ var countNodes = function(root) { }; ``` +## C: +递归法 +```c +int countNodes(struct TreeNode* root) { + //若传入结点不存在,返回0 + if(!root) + return 0; + //算出左右子树的结点总数 + int leftCount = countNodes(root->left); + int rightCount = countNodes(root->right); + //返回左右子树结点总数+1 + return leftCount + rightCount + 1; +} + +int countNodes(struct TreeNode* root){ + return getNodes(root); +} +``` +迭代法 +```c +int countNodes(struct TreeNode* root){ + //记录结点总数 + int totalNum = 0; + //开辟栈空间 + struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 100); + int stackTop = 0; + //如果root结点不为NULL,则将其入栈。若为NULL,则不会进入遍历,返回0 + if(root) + stack[stackTop++] = root; + //若栈中有结点存在,则进行遍历 + while(stackTop) { + //取出栈顶元素 + struct TreeNode* tempNode = stack[--stackTop]; + //结点总数+1 + totalNum++; + //若栈顶结点有左右孩子,将它们入栈 + if(tempNode->left) + stack[stackTop++] = tempNode->left; + if(tempNode->right) + stack[stackTop++] = tempNode->right; + } + return totalNum; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From f3b140591b67b524e322a0dc31e3043488ea422b Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 8 Nov 2021 14:07:24 +0000 Subject: [PATCH 14/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E7=BB=93=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0.md?= 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" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 0826110f81..14e1537a84 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" @@ -495,6 +495,35 @@ int countNodes(struct TreeNode* root){ } ``` +满二叉树 +```c +int countNodes(struct TreeNode* root){ + if(!root) + return 0; + int leftHeight = 0; + int rightHeight = 0; + struct TreeNode* rightNode = root->right; + struct TreeNode* leftNode = root->left; + //求出左子树深度 + while(leftNode) { + leftNode = leftNode->left; + leftHeight++; + } + + //求出右子树深度 + while(rightNode) { + rightNode = rightNode->right; + rightHeight++; + } + //若左右子树深度相同,为满二叉树。结点个数为2^height-1 + if(rightHeight == leftHeight) { + return (2 << leftHeight) - 1; + } + //否则返回左右子树的结点个数+1 + return countNodes(root->right) + countNodes(root->left) + 1; +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 2b0788cdb7d46b39dbb3b54a5797322cc6dc6e51 Mon Sep 17 00:00:00 2001 From: mengyuan Date: Tue, 9 Nov 2021 10:39:04 +0800 Subject: [PATCH 15/21] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B90239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BCjs?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=8C=E5=88=A4=E6=96=AD=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=80=BC=E6=98=AF=E5=90=A6=E5=9C=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E4=B8=AD=E7=94=A8if=E5=8D=B3=E5=8F=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" | 2 +- 1 file changed, 1 insertion(+), 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 be46bd05e9..21dbaf4dc8 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" @@ -410,7 +410,7 @@ var maxSlidingWindow = function (nums, k) { // 入队当前元素下标 q.push(i); // 判断当前最大值(即队首元素)是否在窗口中,若不在便将其出队 - while (q[0] <= i - k) { + if (q[0] <= i - k) { q.shift(); } // 当达到窗口大小时便开始向结果中添加数据 From 912e27442fa5d71c4848d345dc56e1042a666032 Mon Sep 17 00:00:00 2001 From: Carol Date: Tue, 9 Nov 2021 12:03:32 +0800 Subject: [PATCH 16/21] =?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 --- ...00\344\270\252\346\216\222\345\210\227.md" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 9999486ec9..a6aa8a916d 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" @@ -165,6 +165,29 @@ class Solution: low += 1 high -= 1 ``` +>上一版本简化版 +'''python +class Solution(object): + def nextPermutation(self, nums: List[int]) -> None: + n = len(nums) + i = n-2 + while i >= 0 and nums[i] >= nums[i+1]: + i -= 1 + + if i > -1: // i==-1,不存在下一个更大的排列 + j = n-1 + while j >= 0 and nums[j] <= nums[i]: + j -= 1 + nums[i], nums[j] = nums[j], nums[i] + + start, end = i+1, n-1 + while start < end: + nums[start], nums[end] = nums[end], nums[start] + start += 1 + end -= 1 + + return nums +''' ## Go From 6d01869bbc4cdbd0034ffb1314556d9147caa132 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Thu, 11 Nov 2021 22:09:25 +0000 Subject: [PATCH 17/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=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" | 29 +++++++++++++++++++ 1 file changed, 29 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 a4ae868af5..5f01286146 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" @@ -356,6 +356,35 @@ var constructMaximumBinaryTree = function (nums) { }; ``` +## C +```c +struct TreeNode* traversal(int* nums, int left, int right) { + //若左边界大于右边界,返回NULL + if(left >= right) + return NULL; + + //找出数组中最大数坐标 + int maxIndex = left; + int i; + for(i = left + 1; i < right; i++) { + if(nums[i] > nums[maxIndex]) + maxIndex = i; + } + + //开辟结点 + struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + //将结点的值设为最大数组数组元素 + node->val = nums[maxIndex]; + //递归定义左孩子结点和右孩子结点 + node->left = traversal(nums, left, maxIndex); + node->right = traversal(nums, maxIndex + 1, right); + return node; +} + +struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize){ + return traversal(nums, 0, numsSize); +} +``` ----------------------- From c249f3a0f1aff16db83a07322e0c2ef481f466fb Mon Sep 17 00:00:00 2001 From: ArthurP Date: Thu, 11 Nov 2021 22:32:25 +0000 Subject: [PATCH 18/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?.md=20C=E8=AF=AD=E8=A8=80=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" | 55 +++++++++++++++++++ 1 file changed, 55 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 e20f147fe7..1c94994f79 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" @@ -582,6 +582,61 @@ var maxDepth = function(root) { }; ``` +## C +二叉树最大深度递归 +```c +int maxDepth(struct TreeNode* root){ + //若传入结点为NULL,返回0 + if(!root) + return 0; + + //求出左子树深度 + int left = maxDepth(root->left); + //求出右子树深度 + int right = maxDepth(root->right); + //求出左子树深度和右子树深度的较大值 + int max = left > right ? left : right; + //返回较大值+1(1为当前层数) + return max + 1; +} +``` +二叉树最大深度迭代 +```c +int maxDepth(struct TreeNode* root){ + //若传入根节点为NULL,返回0 + if(!root) + return 0; + + int depth = 0; + //开辟队列空间 + struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 6000); + int queueFront = 0; + int queueEnd = 0; + + //将根结点入队 + queue[queueEnd++] = root; + + int queueSize; + //求出当前队列中元素个数 + while(queueSize = queueEnd - queueFront) { + int i; + //若当前队列中结点有左右子树,则将它们的左右子树入队 + for(i = 0; i < queueSize; i++) { + struct TreeNode* tempNode = queue[queueFront + i]; + if(tempNode->left) + queue[queueEnd++] = tempNode->left; + if(tempNode->right) + queue[queueEnd++] = tempNode->right; + } + //更新队头下标 + queueFront += queueSize; + //深度+1 + depth++; + } + return depth; +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 6ea7ce9cd89bba28d5512f6df6dd790f78be5314 Mon Sep 17 00:00:00 2001 From: qq240814476 <240814476@qq.com> Date: Sat, 13 Nov 2021 17:43:05 +0800 Subject: [PATCH 19/21] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=2047=20?= =?UTF-8?q?=E5=85=A8=E6=8E=92=E5=88=97=20=E7=9A=84=E6=8B=BC=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "problems/0047.\345\205\250\346\216\222\345\210\227II.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" index b910583388..c169d91314 100644 --- "a/problems/0047.\345\205\250\346\216\222\345\210\227II.md" +++ "b/problems/0047.\345\205\250\346\216\222\345\210\227II.md" @@ -38,7 +38,7 @@ 那么排列问题其实也是一样的套路。 -**还要强调的是去重一定要对元素经行排序,这样我们才方便通过相邻的节点来判断是否重复使用了**。 +**还要强调的是去重一定要对元素进行排序,这样我们才方便通过相邻的节点来判断是否重复使用了**。 我以示例中的 [1,1,2]为例 (为了方便举例,已经排序)抽象为一棵树,去重过程如图: From c46acca1e4ceaddbda6b4975afe0c4d061ab11fc Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 14 Nov 2021 10:05:29 +0000 Subject: [PATCH 20/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md=20C?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=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" | 51 +++++++++++++++++++ 1 file changed, 51 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 f70f39f3d4..ef93ced815 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" @@ -269,6 +269,57 @@ var removeDuplicates = function(s) { }; ``` +C: +方法一:使用栈 +```c +char * removeDuplicates(char * s){ + //求出字符串长度 + int strLength = strlen(s); + //开辟栈空间。栈空间长度应为字符串长度+1(为了存放字符串结束标志'\0') + char* stack = (char*)malloc(sizeof(char) * strLength + 1); + int stackTop = 0; + + int index = 0; + //遍历整个字符串 + while(index < strLength) { + //取出当前index对应字母,之后index+1 + char letter = s[index++]; + //若栈中有元素,且栈顶字母等于当前字母(两字母相邻)。将栈顶元素弹出 + if(stackTop > 0 && letter == stack[stackTop - 1]) + stackTop--; + //否则将字母入栈 + else + stack[stackTop++] = letter; + } + //存放字符串结束标志'\0' + stack[stackTop] = '\0'; + //返回栈本身作为字符串 + return stack; +} +``` +方法二:双指针法 +```c +char * removeDuplicates(char * s){ + //创建快慢指针 + int fast = 0; + int slow = 0; + //求出字符串长度 + int strLength = strlen(s); + //遍历字符串 + while(fast < strLength) { + //将当前slow指向字符改为fast指向字符。fast指针+1 + char letter = s[slow] = s[fast++]; + //若慢指针大于0,且慢指针指向元素等于字符串中前一位元素,删除慢指针指向当前元素 + if(slow > 0 && letter == s[slow - 1]) + slow--; + else + slow++; + } + //在字符串结束加入字符串结束标志'\0' + s[slow] = 0; + return s; +} +``` ----------------------- From 0a31eaa9d3790f86d9be2c7ad9dde601a2d2b67f Mon Sep 17 00:00:00 2001 From: ccqstark <54167233+ccqstark@users.noreply.github.com> Date: Tue, 16 Nov 2021 11:52:38 +0800 Subject: [PATCH 21/21] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= 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/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 98fb9dd0fc..995db555c2 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" @@ -138,7 +138,7 @@ if (root->val < low) { root->left = trimBST(root->left, low, high); ``` -此时节点3的右孩子就变成了节点2,将节点0从二叉树中移除了。 +此时节点3的左孩子就变成了节点2,将节点0从二叉树中移除了。 最后整体代码如下: