From deaf08a9ad225a113fd7c0efd40f161d93ca460c Mon Sep 17 00:00:00 2001 From: zj <294839611@qq.com> Date: Tue, 21 Mar 2023 16:15:58 +0800 Subject: [PATCH 001/122] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= =?UTF-8?q?=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/背包理论基础01背包-1.md | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index ff0b6aba..1b04b057 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -338,6 +338,64 @@ public class BagProblem { ``` +```java +import java.util.Arrays; + +public class BagProblem { + public static void main(String[] args) { + int[] weight = {1,3,4}; + int[] value = {15,20,30}; + int bagSize = 4; + testWeightBagProblem(weight,value,bagSize); + } + + /** + * 初始化 dp 数组做了简化(给物品增加冗余维)。这样初始化dp数组,默认全为0即可。 + * dp[i][j] 表示从下标为[0 - i-1]的物品里任意取,放进容量为j的背包,价值总和最大是多少。 + * 其实是模仿背包重量从 0 开始,背包容量 j 为 0 的话,即dp[i][0],无论是选取哪些物品,背包价值总和一定为 0。 + * 可选物品也可以从无开始,也就是没有物品可选,即dp[0][j],这样无论背包容量为多少,背包价值总和一定为 0。 + * @param weight 物品的重量 + * @param value 物品的价值 + * @param bagSize 背包的容量 + */ + public static void testWeightBagProblem(int[] weight, int[] value, int bagSize){ + + // 创建dp数组 + int goods = weight.length; // 获取物品的数量 + int[][] dp = new int[goods + 1][bagSize + 1]; // 给物品增加冗余维,i = 0 表示没有物品可选 + + // 初始化dp数组,默认全为0即可 + // 填充dp数组 + for (int i = 1; i <= goods; i++) { + for (int j = 1; j <= bagSize; j++) { + if (j < weight[i - 1]) { // i - 1 对应物品 i + /** + * 当前背包的容量都没有当前物品i大的时候,是不放物品i的 + * 那么前i-1个物品能放下的最大价值就是当前情况的最大价值 + */ + dp[i][j] = dp[i - 1][j]; + } else { + /** + * 当前背包的容量可以放下物品i + * 那么此时分两种情况: + * 1、不放物品i + * 2、放物品i + * 比较这两种情况下,哪种背包中物品的最大价值最大 + */ + dp[i][j] = Math.max(dp[i - 1][j] , dp[i - 1][j - weight[i - 1]] + value[i - 1]); // i - 1 对应物品 i + } + } + } + + // 打印dp数组 + for(int[] arr : dp){ + System.out.println(Arrays.toString(arr)); + } + } +} + +``` + ### python ```python From 2d06a685ea39c988ad48b1a6025c8476cc06a640 Mon Sep 17 00:00:00 2001 From: NeedyChild <100189185+NeedyChild@users.noreply.github.com> Date: Tue, 9 May 2023 15:22:33 -0700 Subject: [PATCH 002/122] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200494.=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E5=92=8C.md=20Java=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index cc2bc8df..f8843d08 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -292,6 +292,85 @@ class Solution { } ``` +易于理解的二维数组版本: +```java +class Solution { + public int findTargetSumWays(int[] nums, int target) { + + // 01背包应用之“有多少种不同的填满背包最大容量的方法“ + // 易于理解的二维数组解法及详细注释 + + int sum = 0; + for(int i = 0; i < nums.length; i++) { + sum += nums[i]; + } + + // 注意nums[i] >= 0的题目条件,意味着sum也是所有nums[i]的绝对值之和 + // 这里保证了sum + target一定是大于等于零的,也就是left大于等于零(毕竟我们定义left大于right) + if(sum < Math.abs(target)){ + return 0; + } + + // 利用二元一次方程组将left用target和sum表示出来(替换掉right组合),详见代码随想录对此题的分析 + // 如果所求的left数组和为小数,则作为整数数组的nums里的任何元素自然是没有办法凑出这个小数的 + if((sum + target) % 2 != 0) { + return 0; + } + + int left = (sum + target) / 2; + + // dp[i][j]:遍历到数组第i个数时, left为j时的能装满背包的方法总数 + int[][] dp = new int[nums.length][left + 1]; + + // 初始化最上行(dp[0][j]),当nums[0] == j时(注意nums[0]和j都一定是大于等于零的,因此不需要判断等于-j时的情况),有唯一一种取法可取到j,dp[0][j]此时等于1 + // 其他情况dp[0][j] = 0 + // java整数数组默认初始值为0 + for(int j = 0; j <= left; j++) { + if(nums[0] == j) { + dp[0][j] = 1; + } + } + + // 初始化最左列(dp[i][0]) + // 当从nums数组的索引0到i的部分有n个0时(n > 0),每个0可以取+/-,因此有2的n次方中可以取到j = 0的方案 + // n = 0说明当前遍历到的数组部分没有0全为正数,因此只有一种方案可以取到j = 0(就是所有数都不取) + int numZeros = 0; + for(int i = 0; i < nums.length; i++) { + if(nums[i] == 0) { + numZeros++; + } + dp[i][0] = (int) Math.pow(2, numZeros); + + } + + // 递推公式分析: + // 当nums[i] > j时,这时候nums[i]一定不能取,所以是dp[i - 1][j]种方案数 + // nums[i] <= j时,num[i]可取可不取,因此方案数是dp[i - 1][j] + dp[i - 1][j - nums[i]] + // 由递推公式可知,先遍历i或j都可 + for(int i = 1; i < nums.length; i++) { + for(int j = 1; j <= left; j++) { + if(nums[i] > j) { + dp[i][j] = dp[i - 1][j]; + } else { + dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i]]; + } + } + } + + // 打印dp数组 + // for(int i = 0; i < nums.length; i++) { + // for(int j = 0; j <= left; j++) { + // System.out.print(dp[i][j] + " "); + // } + // System.out.println(""); + // } + + return dp[nums.length - 1][left]; + + } +} +``` + ### Python ```python class Solution: From 346e927f6c8a98594b444233dede3bca8f6ad5eb Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:51:21 -0400 Subject: [PATCH 003/122] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95=20for=20leetcode=205.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java解法 for leetcode 5. 感覺卡哥可以提一下,其實在下一偏有提到,但是真的有關聯的這一篇沒有提到:這一題稍微改一下就能通過兩題,這一點我沒有寫在這個fork上。 --- problems/0647.回文子串.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index e172de54..084b9f74 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -304,7 +304,38 @@ class Solution { } } ``` +LeetCode 5. Longest Palindromic Substring(LeetCode 647. 同一題的思路改一下、加一點,就能通過LeetCode 5) +```java +class Solution { + public String longestPalindrome(String s) { + //題目要求要return 最長的回文連續子串,故需要記錄當前最長的連續回文子串長度、最終起點、最終終點。 + int finalStart = 0; + int finalEnd = 0; + int finalLen = 0; + char[] chars = s.toCharArray(); + int len = chars.length; + + boolean[][] dp = new boolean[len][len]; + for (int i = len - 1; i >= 0; i--) { + for (int j = i; j < len; j++) { + if (chars[i] == chars[j] && (j - i <= 1 || dp[i + 1][j - 1])) + dp[i][j] = true; + //和LeetCode 647,差別就在這個if statement。 + //如果當前[i, j]範圍內的substring是回文子串(dp[i][j]) 且(&&) 長度大於當前要記錄的最終長度(j - i + 1 > finalLen) + //我們就更新 當前最長的連續回文子串長度、最終起點、最終終點 + if (dp[i][j] && j - i + 1 > finalLen) { + finalLen = j - i + 1; + finalStart = i; + finalEnd = j; + } + } + } + //String.substring這個method的用法是[起點, 終點),包含起點,不包含終點(左閉右開區間),故終點 + 1。 + return s.substring(finalStart, finalEnd + 1); + } +} +``` Python: From 1f31f092f1ec713a6df0f7617c39bc2107a9ba5f Mon Sep 17 00:00:00 2001 From: Ao Liu <63785048+Ao-Last@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:38:00 +0800 Subject: [PATCH 004/122] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?.md=20md=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 101dd6f2..4ea43947 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -163,6 +163,7 @@ class Solution(object): a_count = Counter(s) b_count = Counter(t) return a_count == b_count +``` Go: From b92fcc936e4515fd922e4f00ecfd0e0816f275b4 Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Thu, 22 Jun 2023 03:13:04 -0400 Subject: [PATCH 005/122] =?UTF-8?q?=E8=A7=A3=E6=B1=BA=E8=B7=91=E6=9D=BF?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=20+=20=E6=8F=90=E4=BE=9Bjava=E8=A7=A3?= =?UTF-8?q?=E6=B3=95(=E5=92=8C=E5=8D=A1=E5=93=A5=E9=82=8F=E8=BC=AF?= =?UTF-8?q?=E4=B8=80=E8=87=B4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 跑版: 原本的code box 結束的```不見了,所以看不到結尾圖片。 新解法和原本JAVA解法的差異: 原版的應該是自己寫的DFS,邏輯大致一致,但還是有差異,故提供用卡哥C++ code改的版本 --- problems/0200.岛屿数量.深搜版.md | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0200.岛屿数量.深搜版.md b/problems/0200.岛屿数量.深搜版.md index 6d42162a..c30ace19 100644 --- a/problems/0200.岛屿数量.深搜版.md +++ b/problems/0200.岛屿数量.深搜版.md @@ -176,6 +176,48 @@ public void dfs(char[][] grid, int i, int j){ dfs(grid,i,j + 1); dfs(grid,i,j - 1); } +``` +```java +//graph - dfs (和卡哥的代碼邏輯一致) +class Solution { + boolean[][] visited; + int dir[][] = { + {0, 1}, //right + {1, 0}, //down + {-1, 0}, //up + {0, -1} //left + }; + public int numIslands(char[][] grid) { + int count = 0; + visited = new boolean[grid.length][grid[0].length]; + + for(int i = 0; i < grid.length; i++){ + for(int j = 0; j < grid[0].length; j++){ + if(visited[i][j] == false && grid[i][j] == '1'){ + count++; + dfs(grid, i, j); + } + } + } + return count; + } + + private void dfs(char[][]grid, int x, int y){ + if(visited[x][y] == true || grid[x][y] == '0') + return; + + visited[x][y] = true; + + for(int i = 0; i < 4; i++){ + int nextX = x + dir[i][0]; + int nextY = y + dir[i][1]; + if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length) + continue; + dfs(grid, nextX, nextY); + } + } +} +```
From f8f8539393813c4f4ec73427d5eb157acc641fe1 Mon Sep 17 00:00:00 2001
From: Yifan Liu <39271063+yifanliuu@users.noreply.github.com>
Date: Thu, 29 Jun 2023 13:49:22 +0800
Subject: [PATCH 009/122] =?UTF-8?q?=E6=96=B0=E5=A2=9Epython=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95=E9=80=92=E5=BD=92=E7=89=88=200024.=E4=B8=A4=E4=B8=A4?=
=?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?=
=?UTF-8?q?=E7=82=B9.md?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0024.两两交换链表中的节点.md | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md
index ab204d89..d7c03b64 100644
--- a/problems/0024.两两交换链表中的节点.md
+++ b/problems/0024.两两交换链表中的节点.md
@@ -177,6 +177,30 @@ class Solution {
```
Python:
+```python
+# 递归版本
+# Definition for singly-linked list.
+# class ListNode:
+# def __init__(self, val=0, next=None):
+# self.val = val
+# self.next = next
+
+class Solution:
+ def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
+ if head is None or head.next is None:
+ return head
+
+ # 待翻转的两个node分别是pre和cur
+ pre = head
+ cur = head.next
+ next = head.next.next
+
+ cur.next = pre # 交换
+ pre.next = self.swapPairs(next) # 将以next为head的后续链表两两交换
+
+ return cur
+```
+
```python
# Definition for singly-linked list.
# class ListNode:
From 5085e78e601fa6450e80b0d5d9b060df26857dcb Mon Sep 17 00:00:00 2001
From: Leon He
From 85daaae4656f0411eab2e1ec6ff1bbfa608120c1 Mon Sep 17 00:00:00 2001
From: fwqaaq
From 2444c084b15fce271ac3639bca32bc579c594ad1 Mon Sep 17 00:00:00 2001
From: fwqaaq
From 7d92df37eb7aca18042512789a286ec4eec3123a Mon Sep 17 00:00:00 2001
From: fwqaaq
From e563783a71fe3de6564c7c01e8ffbfc961f3efee Mon Sep 17 00:00:00 2001
From: fwqaaq
From d945a304c49f4f41de91b2794b35de0c5f79110b Mon Sep 17 00:00:00 2001
From: Binbin 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
-
+
+ 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
+ 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
+ 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
- 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
- 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
From 33eca1635693f64d0eeb6d7c477093665dded20b Mon Sep 17 00:00:00 2001
From: Terry Liu <102352821+Lozakaka@users.noreply.github.com>
Date: Sat, 24 Jun 2023 20:18:36 -0400
Subject: [PATCH 006/122] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
1. 調整語言順序
2. 新增java - DFS, java - BFS解法
---
problems/0695.岛屿的最大面积.md | 162 +++++++++++++++++++++++++-------
1 file changed, 129 insertions(+), 33 deletions(-)
diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md
index e5deb897..37a601bc 100644
--- a/problems/0695.岛屿的最大面积.md
+++ b/problems/0695.岛屿的最大面积.md
@@ -189,6 +189,135 @@ public:
```
# 其它语言版本
+## Java
+### DFS
+```java
+// DFS
+class Solution {
+ int[][] dir = {
+ {0, 1}, //right
+ {1, 0}, //down
+ {0, -1}, //left
+ {-1, 0} //up
+ };
+ boolean visited[][];
+ int count;
+ public int maxAreaOfIsland(int[][] grid) {
+ int res = 0;
+ visited = new boolean[grid.length][grid[0].length];
+ for(int i = 0; i < grid.length; i++){
+ for(int j = 0; j < grid[0].length; j++){
+ if(visited[i][j] == false && grid[i][j] == 1){
+ count = 0;
+ dfs(grid, i, j);
+ res = Math.max(res, count);
+ }
+ }
+ }
+ return res;
+ }
+ private void dfs(int[][] grid, int x, int y){
+ if(visited[x][y] == true || grid[x][y] == 0)
+ return;
+
+ visited[x][y] = true;
+ count++;
+
+ for(int i = 0; i < 4; i++){
+ int nextX = x + dir[i][0];
+ int nextY = y + dir[i][1];
+
+ if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length)
+ continue;
+ dfs(grid, nextX, nextY);
+ }
+ }
+}
+
+
+```
+### BFS
+```java
+//BFS
+class Solution {
+ int[][] dir = {
+ {0, 1}, {1, 0}, {0, -1}, {-1, 0}
+ };
+
+ int count;
+ boolean visited[][];
+
+ public int maxAreaOfIsland(int[][] grid) {
+ int res = 0;
+ visited = new boolean[grid.length][grid[0].length];
+
+ for(int i = 0; i < grid.length; i++){
+ for(int j = 0; j < grid[0].length; j++){
+ if(visited[i][j] == false && grid[i][j] == 1){
+ count = 0;
+ bfs(grid, i, j);
+ res = Math.max(res, count);
+ }
+ }
+ }
+ return res;
+ }
+ private void bfs(int[][] grid, int x, int y){
+ Queue
From 2b3ce4453520a16bac6d65c2b3295cc65b90f00e Mon Sep 17 00:00:00 2001
From: Terry Liu <102352821+Lozakaka@users.noreply.github.com>
Date: Sat, 24 Jun 2023 23:38:40 -0400
Subject: [PATCH 007/122] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/1020.飞地的数量.md | 126 +++++++++++++++++++++++++++++++++++-
1 file changed, 124 insertions(+), 2 deletions(-)
diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md
index e92b2412..7538d774 100644
--- a/problems/1020.飞地的数量.md
+++ b/problems/1020.飞地的数量.md
@@ -149,7 +149,63 @@ public:
### Java
-深度优先遍历版本:
+深度优先遍历(没有终止条件 + 空間優化(淹沒島嶼,沒有使用visited數組))
+```java
+//DFS
+class Solution {
+ int count = 0;
+ int[][] dir ={
+ {0, 1},
+ {1, 0},
+ {-1, 0},
+ {0, -1}
+ };
+ private void dfs(int[][] grid, int x, int y){
+ if(grid[x][y] == 0)
+ return;
+
+ grid[x][y] = 0;
+ count++;
+
+ for(int i = 0; i < 4; i++){
+ int nextX = x + dir[i][0];
+ int nextY = y + dir[i][1];
+
+ if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length)
+ continue;
+ dfs(grid, nextX, nextY);
+ }
+
+ }
+
+ public int numEnclaves(int[][] grid) {
+ for(int i = 0; i < grid.length; i++){
+ if(grid[i][0] == 1)
+ dfs(grid, i, 0);
+ if(grid[i][grid[0].length - 1] == 1)
+ dfs(grid, i, grid[0].length - 1);
+ }
+ //初始化的時候,j 的上下限有調整過,必免重複操作。
+ for(int j = 1; j < grid[0].length - 1; j++){
+ if(grid[0][j] == 1)
+ dfs(grid, 0, j);
+ if(grid[grid.length - 1][j] == 1)
+ dfs(grid, grid.length - 1, j);
+ }
+ count = 0;
+
+ for(int i = 1; i < grid.length - 1; i++){
+ for(int j = 1; j < grid[0].length - 1; j++){
+ if(grid[i][j] == 1)
+ dfs(grid, i, j);
+ }
+ }
+ return count;
+ }
+}
+```
+
+深度优先遍历(没有终止条件)
```java
class Solution {
@@ -206,7 +262,7 @@ class Solution {
}
```
-广度优先遍历版本:
+广度优先遍历(使用visited數組)
```java
class Solution {
@@ -269,6 +325,72 @@ class Solution {
}
```
+廣度优先遍历(空間優化(淹沒島嶼,沒有使用visited數組))
+```java
+//BFS
+class Solution {
+ int count = 0;
+ int[][] dir ={
+ {0, 1},
+ {1, 0},
+ {-1, 0},
+ {0, -1}
+ };
+ private void bfs(int[][] grid, int x, int y){
+ Queue
+```
+### Python
+BFS solution
+```python
+class Solution:
+ def __init__(self):
+ self.dirs = [[0, 1], [1, 0], [-1, 0], [0, -1]]
+
+ def numIslands(self, grid: List[List[str]]) -> int:
+ m = len(grid)
+ n = len(grid[0])
+ visited = [[False]*n for _ in range(m)]
+ res = 0
+ for i in range(m):
+ for j in range(n):
+ if visited[i][j] == False and grid[i][j] == '1':
+ res += 1
+ self.bfs(grid, i, j, visited) # Call bfs within this condition
+ return res
+
+ def bfs(self, grid, i, j, visited):
+ q = deque()
+ q.append((i,j))
+ visited[i][j] = True
+ while q:
+ x, y = q.popleft()
+ for k in range(4):
+ next_i = x + self.dirs[k][0]
+ next_j = y + self.dirs[k][1]
+
+ if next_i < 0 or next_i >= len(grid):
+ continue
+ if next_j < 0 or next_j >= len(grid[0]):
+ continue
+ if visited[next_i][next_j]:
+ continue
+ if grid[next_i][next_j] == '0':
+ continue
+ q.append((next_i, next_j))
+ visited[next_i][next_j] = True
+```
From 6cd9b165290298868b6b86ca88693c5b77408841 Mon Sep 17 00:00:00 2001
From: Terry Liu <102352821+Lozakaka@users.noreply.github.com>
Date: Thu, 29 Jun 2023 16:56:19 -0400
Subject: [PATCH 011/122] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=20DFS=E8=A7=A3?=
=?UTF-8?q?=E6=B3=95=E7=95=B6=E4=BD=9C=E5=BB=B6=E4=BC=B8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0463.岛屿的周长.md | 47 +++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md
index 2f399f40..18f1d01e 100644
--- a/problems/0463.岛屿的周长.md
+++ b/problems/0463.岛屿的周长.md
@@ -142,6 +142,53 @@ class Solution {
return landSum * 4 - cover * 2;
}
}
+// 延伸 - 傳統DFS解法(使用visited數組)(遇到邊界 或是 海水 就edge ++)
+class Solution {
+ int dir[][] ={
+ {0, 1},
+ {0, -1},
+ {1, 0},
+ {-1, 0}
+ };
+
+ boolean visited[][];
+ int res = 0;
+
+ public int islandPerimeter(int[][] grid) {
+ int row = grid.length;
+ int col = grid[0].length;
+ visited = new boolean[row][col];
+
+ int result = 0;
+
+ for(int i = 0; i < row; i++){
+ for(int j = 0; j < col; j++){
+ if(visited[i][j] == false && grid[i][j] == 1)
+ result += dfs(grid, i, j);
+ }
+ }
+ return result;
+ }
+
+ private int dfs(int[][] grid, int x, int y){
+ //如果遇到 邊界(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length)或是 遇到海水(grid[x][y] == 0)就return 1(edge + 1)
+ if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == 0)
+ return 1;
+ //如果該地已經拜訪過,就return 0 避免重複計算
+ if(visited[x][y])
+ return 0;
+ int temp = 0;
+ visited[x][y] = true;
+ for(int i = 0; i < 4; i++){
+ int nextX = x + dir[i][0];
+ int nextY = y + dir[i][1];
+ //用temp 把edge存起來
+ temp +=dfs(grid, nextX, nextY);
+ }
+ return temp;
+ }
+}
+
```
Python:
From c1b2141ea20e58b031c874f55c341fbfc8b69470 Mon Sep 17 00:00:00 2001
From: fwqaaq
-
-```
+
+## 其他语言版本
### Python
BFS solution
```python
@@ -241,3 +238,9 @@ class Solution:
q.append((next_i, next_j))
visited[next_i][next_j] = True
```
+
+
+
+```
From 3a76b3a85e097446ab5f9e7dbb6028bac81c75de Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Mon, 17 Jul 2023 11:52:50 +0800
Subject: [PATCH 026/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200704.=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0704.二分查找.md | 36 ++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index c59ae868..d202dfa1 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -35,7 +35,7 @@
## 算法公开课
-***[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你撕出正确的二分法](https://www.bilibili.com/video/BV1fA4y1o715),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -160,7 +160,7 @@ public:
## 其他语言版本
-**Java:**
+### **Java:**
(版本一)左闭右闭区间
@@ -206,7 +206,7 @@ class Solution {
}
```
-**Python:**
+### **Python:**
(版本一)左闭右闭区间
@@ -246,7 +246,7 @@ class Solution:
return -1 # 未找到目标值
```
-**Go:**
+### **Go:**
(版本一)左闭右闭区间
@@ -288,7 +288,7 @@ func search(nums []int, target int) int {
}
```
-**JavaScript:**
+### **JavaScript:**
(版本一)左闭右闭区间 [left, right]
```js
@@ -345,7 +345,7 @@ var search = function(nums, target) {
};
```
-**TypeScript**
+### **TypeScript**
(版本一)左闭右闭区间
@@ -387,7 +387,7 @@ function search(nums: number[], target: number): number {
};
```
-**Ruby:**
+### **Ruby:**
```ruby
# (版本一)左闭右闭区间
@@ -425,7 +425,7 @@ def search(nums, target)
end
```
-**Swift:**
+### **Swift:**
```swift
// (版本一)左闭右闭区间
@@ -479,7 +479,7 @@ func search(nums: [Int], target: Int) -> Int {
```
-**Rust:**
+### **Rust:**
```rust
# (版本一)左闭右闭区间
@@ -523,7 +523,8 @@ impl Solution {
}
```
-**C:**
+### **C:**
+
```c
// (版本一) 左闭右闭区间 [left, right]
int search(int* nums, int numsSize, int target){
@@ -575,7 +576,8 @@ int search(int* nums, int numsSize, int target){
}
```
-**PHP:**
+### **PHP:**
+
```php
// 左闭右闭区间
class Solution {
@@ -607,7 +609,8 @@ class Solution {
}
```
-**C#:**
+### **C#:**
+
```csharp
//左闭右闭
public class Solution {
@@ -652,7 +655,8 @@ public class Solution{
}
```
-**Kotlin:**
+### **Kotlin:**
+
```kotlin
class Solution {
fun search(nums: IntArray, target: Int): Int {
@@ -682,9 +686,8 @@ class Solution {
}
```
+### **Kotlin:**
-
-**Kotlin:**
```Kotlin
// (版本一)左闭右开区间
class Solution {
@@ -715,7 +718,7 @@ class Solution {
}
}
```
-**Scala:**
+### **Scala:**
(版本一)左闭右闭区间
```scala
@@ -763,3 +766,4 @@ object Solution {
+
From 948aca860da277d744c9fb130039fe96dd79491f Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Mon, 17 Jul 2023 14:59:20 +0800
Subject: [PATCH 027/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200704.=E4=BA=8C?=
=?UTF-8?q?=E5=88=86=E6=9F=A5=E6=89=BE=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0704.二分查找.md | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index d202dfa1..52abf578 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -5,7 +5,7 @@
-
From fdbc7442ac2b685414544f0af79d64c40da05d0f Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Mon, 17 Jul 2023 15:11:55 +0800
Subject: [PATCH 028/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20027.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0027.移除元素.md | 35 +++++++++++++++++++++--------------
1 file changed, 21 insertions(+), 14 deletions(-)
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md
index 3d43a199..ce9eccf0 100644
--- a/problems/0027.移除元素.md
+++ b/problems/0027.移除元素.md
@@ -5,7 +5,7 @@
+
From 76c84efca0bcfab9e889bf97458dd6b6423e0f05 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Mon, 17 Jul 2023 15:32:24 +0800
Subject: [PATCH 030/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200209.=20=E9=95=BF?=
=?UTF-8?q?=E5=BA=A6=E6=9C=80=E5=B0=8F=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?=
=?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0209.长度最小的子数组.md | 41 +++++++++++++++++--------------
1 file changed, 23 insertions(+), 18 deletions(-)
diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md
index 82d551ea..4b1d0e96 100644
--- a/problems/0209.长度最小的子数组.md
+++ b/problems/0209.长度最小的子数组.md
@@ -23,14 +23,14 @@
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^5
-# 算法公开课
+## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下滑动窗口! | LeetCode 209 长度最小的子数组](https://www.bilibili.com/video/BV1tZ4y1q7XE),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
-## 暴力解法
+### 暴力解法
这道题目暴力解法当然是 两个for循环,然后不断的寻找符合条件的子序列,时间复杂度很明显是O(n^2)。
@@ -64,7 +64,7 @@ public:
后面力扣更新了数据,暴力解法已经超时了。
-## 滑动窗口
+### 滑动窗口
接下来就开始介绍数组操作中另一个重要的方法:**滑动窗口**。
@@ -151,8 +151,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
@@ -173,7 +173,7 @@ class Solution {
}
```
-Python:
+### Python:
```python
(版本一)滑动窗口法
@@ -216,7 +216,8 @@ class Solution:
return min_len if min_len != float('inf') else 0
```
-Go:
+### Go:
+
```go
func minSubArrayLen(target int, nums []int) int {
i := 0
@@ -242,8 +243,7 @@ func minSubArrayLen(target int, nums []int) int {
}
```
-
-JavaScript:
+### JavaScript:
```js
var minSubArrayLen = function(target, nums) {
@@ -266,7 +266,7 @@ var minSubArrayLen = function(target, nums) {
};
```
-Typescript:
+### Typescript:
```typescript
function minSubArrayLen(target: number, nums: number[]): number {
@@ -288,7 +288,7 @@ function minSubArrayLen(target: number, nums: number[]): number {
};
```
-Swift:
+### Swift:
```swift
func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
@@ -309,7 +309,7 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
}
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -336,7 +336,8 @@ impl Solution {
}
```
-PHP:
+### PHP:
+
```php
// 双指针 - 滑动窗口
class Solution {
@@ -365,7 +366,7 @@ class Solution {
}
```
-Ruby:
+### Ruby:
```ruby
def min_sub_array_len(target, nums)
@@ -383,8 +384,9 @@ def min_sub_array_len(target, nums)
end
```
-C:
+### C:
暴力解法:
+
```c
int minSubArrayLen(int target, int* nums, int numsSize){
//初始化最小长度为INT_MAX
@@ -433,7 +435,8 @@ int minSubArrayLen(int target, int* nums, int numsSize){
}
```
-Kotlin:
+### Kotlin:
+
```kotlin
class Solution {
fun minSubArrayLen(target: Int, nums: IntArray): Int {
@@ -485,7 +488,7 @@ class Solution {
}
}
```
-Scala:
+### Scala:
滑动窗口:
```scala
@@ -533,7 +536,8 @@ object Solution {
}
}
```
-C#:
+### C#:
+
```csharp
public class Solution {
public int MinSubArrayLen(int s, int[] nums) {
@@ -559,3 +563,4 @@ public class Solution {
+
From 7a30d500cb5f328c3e4c952f2f9ae0ecb3382b45 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Mon, 17 Jul 2023 15:47:48 +0800
Subject: [PATCH 031/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20059.=E8=9E=BA?=
=?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0059.螺旋矩阵II.md | 33 +++++++++++++++++++--------------
1 file changed, 19 insertions(+), 14 deletions(-)
diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md
index fd40f3fc..f03fcdad 100644
--- a/problems/0059.螺旋矩阵II.md
+++ b/problems/0059.螺旋矩阵II.md
@@ -26,7 +26,7 @@
## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿下螺旋矩阵!LeetCode:59.螺旋矩阵II](https://www.bilibili.com/video/BV1SL4y1N7mV),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
+
## 思路
这道题目可以说在面试中出现频率较高的题目,**本题并不涉及到什么算法,就是模拟过程,但却十分考察对代码的掌控能力。**
@@ -125,15 +125,15 @@ public:
## 类似题目
-* 54.螺旋矩阵
-* 剑指Offer 29.顺时针打印矩阵
+* [54.螺旋矩阵](https://leetcode.cn/problems/spiral-matrix/)
+* [剑指Offer 29.顺时针打印矩阵](https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
## 其他语言版本
-Java:
+### Java:
```Java
class Solution {
@@ -176,7 +176,7 @@ class Solution {
}
```
-python3:
+### python3:
```python
class Solution:
@@ -207,7 +207,7 @@ class Solution:
return nums
```
-javaScript
+### JavaScript:
```javascript
@@ -259,7 +259,7 @@ var generateMatrix = function(n) {
```
-TypeScript:
+### TypeScript:
```typescript
function generateMatrix(n: number): number[][] {
@@ -304,7 +304,7 @@ function generateMatrix(n: number): number[][] {
};
```
-Go:
+### Go:
```go
package main
@@ -397,7 +397,7 @@ func generateMatrix(n int) [][]int {
}
```
-Swift:
+### Swift:
```swift
func generateMatrix(_ n: Int) -> [[Int]] {
@@ -453,7 +453,7 @@ func generateMatrix(_ n: Int) -> [[Int]] {
}
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -506,7 +506,8 @@ impl Solution {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -548,7 +549,8 @@ class Solution {
}
```
-C:
+### C:
+
```c
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
//初始化返回的结果数组的大小
@@ -607,7 +609,8 @@ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
return ans;
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
def generateMatrix(n: Int): Array[Array[Int]] = {
@@ -659,7 +662,8 @@ object Solution {
}
}
```
-C#:
+### C#:
+
```csharp
public class Solution {
public int[][] GenerateMatrix(int n) {
@@ -688,3 +692,4 @@ public class Solution {
+
From 053632c31d05cae6391b29db2ec76f1945eb2cfe Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Mon, 17 Jul 2023 15:51:53 +0800
Subject: [PATCH 032/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=95=B0=E7=BB=84?=
=?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/数组总结篇.md | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/problems/数组总结篇.md b/problems/数组总结篇.md
index ef962187..7550ce02 100644
--- a/problems/数组总结篇.md
+++ b/problems/数组总结篇.md
@@ -4,9 +4,9 @@
+
From a78116889f705d9e7f72f927decb0a9e844bd807 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 14:28:12 +0800
Subject: [PATCH 035/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200203.=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0203.移除链表元素.md | 33 ++++++++++++++++-----------------
1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md
index 300f98e9..f09c572b 100644
--- a/problems/0203.移除链表元素.md
+++ b/problems/0203.移除链表元素.md
@@ -27,14 +27,12 @@
输入:head = [7,7,7,7], val = 7
输出:[]
-# 算法公开课
+## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
-
-为了方便大家理解,我特意录制了视频:[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),结合视频在看本题解,事半功倍。
+## 思路
这里以链表 1 4 2 4 来举例,移除元素4。
@@ -91,7 +89,7 @@
最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点
-# C++代码
+### C++代码
**直接使用原来的链表来进行移除节点操作:**
@@ -159,7 +157,7 @@ public:
## 其他语言版本
-C:
+### C:
用原来的链表操作:
```c
@@ -227,7 +225,7 @@ struct ListNode* removeElements(struct ListNode* head, int val){
}
```
-Java:
+### Java:
```java
/**
@@ -308,7 +306,7 @@ public ListNode removeElements(ListNode head, int val) {
}
```
-Python:
+### Python:
```python
(版本一)虚拟头节点法
@@ -334,7 +332,7 @@ class Solution:
```
-Go:
+### Go:
```go
/**
@@ -359,7 +357,7 @@ func removeElements(head *ListNode, val int) *ListNode {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -381,7 +379,7 @@ var removeElements = function(head, val) {
};
```
-TypeScript:
+### TypeScript:
版本一(在原链表上直接删除):
@@ -437,7 +435,7 @@ function removeElements(head: ListNode | null, val: number): ListNode | null {
};
```
-Swift:
+### Swift:
```swift
/**
@@ -465,7 +463,7 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
}
```
-PHP:
+### PHP:
```php
/**
@@ -493,7 +491,7 @@ func removeElements(head *ListNode, val int) *ListNode {
}
```
-RUST:
+### Rust:
```rust
// Definition for singly-linked list.
@@ -531,7 +529,7 @@ impl Solution {
}
```
-Scala:
+### Scala:
```scala
/**
@@ -564,7 +562,7 @@ object Solution {
}
```
-Kotlin:
+### Kotlin:
```kotlin
/**
@@ -600,7 +598,8 @@ class Solution {
}
```
-C#
+### C#
+
```CSharp
/**
* Definition for singly-linked list.
From 61366ff59037d5f22f256e5bc410efd8a36a35d3 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 14:41:02 +0800
Subject: [PATCH 036/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200707.=E8=AE=BE?=
=?UTF-8?q?=E8=AE=A1=E9=93=BE=E8=A1=A8=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0203.移除链表元素.md | 4 +---
problems/0707.设计链表.md | 34 ++++++++++++++++++----------------
2 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md
index f09c572b..c8f802a1 100644
--- a/problems/0203.移除链表元素.md
+++ b/problems/0203.移除链表元素.md
@@ -88,9 +88,6 @@
最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点
-
-### C++代码
-
**直接使用原来的链表来进行移除节点操作:**
```CPP
@@ -638,3 +635,4 @@ public class Solution
+
diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md
index 3f096a22..c27f0107 100644
--- a/problems/0707.设计链表.md
+++ b/problems/0707.设计链表.md
@@ -25,12 +25,12 @@

-# 算法公开课
+## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表操作学个通透!LeetCode:707.设计链表](https://www.bilibili.com/video/BV1FU4y1X7WD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
如果对链表的基础知识还不太懂,可以看这篇文章:[关于链表,你该了解这些!](https://programmercarl.com/链表理论基础.html)
@@ -58,8 +58,6 @@
下面采用的设置一个虚拟头结点(这样更方便一些,大家看代码就会感受出来)。
-
-## 代码
```CPP
class MyLinkedList {
public:
@@ -167,7 +165,8 @@ private:
## 其他语言版本
-C:
+### C:
+
```C
typedef struct MyLinkedList {
int val;
@@ -291,7 +290,8 @@ void myLinkedListFree(MyLinkedList* obj) {
*/
```
-Java:
+### Java:
+
```Java
//单链表
class ListNode {
@@ -487,7 +487,8 @@ class MyLinkedList {
*/
```
-Python:
+### Python:
+
```python
(版本一)单链表法
class ListNode:
@@ -661,7 +662,7 @@ class MyLinkedList:
# obj.deleteAtIndex(index)
```
-Go:
+### Go:
```go
//单链表实现
@@ -915,7 +916,7 @@ func (this *MyLinkedList) DeleteAtIndex(index int) {
}
```
-javaScript:
+### JavaScript:
```js
@@ -1055,7 +1056,8 @@ MyLinkedList.prototype.deleteAtIndex = function(index) {
*/
```
-TypeScript:
+### TypeScript:
+
```TypeScript
class ListNode {
public val: number;
@@ -1173,7 +1175,8 @@ class MyLinkedList {
}
```
-Kotlin:
+### Kotlin:
+
```kotlin
class MyLinkedList {
@@ -1241,8 +1244,7 @@ class MyLinkedList {
}
```
-
-Swift:
+### Swift:
```swift
class MyLinkedList {
@@ -1323,7 +1325,8 @@ class MyLinkedList {
}
```
-Scala:
+### Scala:
+
```scala
class ListNode(_x: Int = 0, _next: ListNode = null) {
var next: ListNode = _next
@@ -1393,7 +1396,7 @@ class MyLinkedList() {
}
```
-Rust:
+### Rust:
```rust
#[derive(Debug)]
@@ -1486,4 +1489,3 @@ impl MyLinkedList {
-
From 32bf073bc8c2fe08aabc9aaf0ef61184febfb0a8 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 14:50:44 +0800
Subject: [PATCH 037/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200206.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0206.反转链表.md | 745 ++++++++++++++++++++++++++++++++++++++
problems/0206.翻转链表.md | 55 ++-
2 files changed, 772 insertions(+), 28 deletions(-)
create mode 100644 problems/0206.反转链表.md
diff --git a/problems/0206.反转链表.md b/problems/0206.反转链表.md
new file mode 100644
index 00000000..5a57939a
--- /dev/null
+++ b/problems/0206.反转链表.md
@@ -0,0 +1,745 @@
+
+
+
+
diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md
index c63c998d..5a57939a 100644
--- a/problems/0206.翻转链表.md
+++ b/problems/0206.翻转链表.md
@@ -17,14 +17,12 @@
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
-# 算法公开课
+## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
-
-本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+## 思路
如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。
@@ -51,9 +49,7 @@
最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。
-# C++代码
-
-## 双指针法
+### 双指针法
```CPP
class Solution {
public:
@@ -76,7 +72,7 @@ public:
* 时间复杂度: O(n)
* 空间复杂度: O(1)
-## 递归法
+### 递归法
递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。
@@ -137,8 +133,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
// 双指针
class Solution {
@@ -198,7 +194,8 @@ class Solution {
}
```
-Python
+### Python:
+
```python
(版本一)双指针法
# Definition for singly-linked list.
@@ -219,8 +216,6 @@ class Solution:
return pre
```
-Python递归法:
-
```python
(版本二)递归法
# Definition for singly-linked list.
@@ -242,7 +237,7 @@ class Solution:
-Go:
+### Go:
```go
//双指针
@@ -273,7 +268,7 @@ func help(pre, head *ListNode)*ListNode{
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -328,7 +323,7 @@ var reverseList = function(head) {
};
```
-TypeScript:
+### TypeScript:
```typescript
// 双指针法
@@ -376,7 +371,7 @@ function reverseList(head: ListNode | null): ListNode | null {
};
```
-Ruby:
+### Ruby:
```ruby
# 双指针
@@ -421,7 +416,8 @@ def reverse(pre, cur)
end
```
-Kotlin:
+### Kotlin:
+
```Kotlin
fun reverseList(head: ListNode?): ListNode? {
var pre: ListNode? = null
@@ -471,7 +467,8 @@ class Solution {
}
```
-Swift:
+### Swift:
+
```swift
/// 双指针法 (迭代)
/// - Parameter head: 头结点
@@ -508,8 +505,9 @@ func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? {
}
```
-C:
+### C:
双指针法:
+
```c
struct ListNode* reverseList(struct ListNode* head){
//保存cur的下一个结点
@@ -549,7 +547,8 @@ struct ListNode* reverseList(struct ListNode* head){
-PHP:
+### PHP:
+
```php
// 双指针法:
function reverseList($head) {
@@ -565,8 +564,9 @@ function reverseList($head) {
}
```
-Scala:
+### Scala:
双指针法:
+
```scala
object Solution {
def reverseList(head: ListNode): ListNode = {
@@ -601,7 +601,7 @@ object Solution {
}
```
-Rust:
+### Rust:
双指针法:
```rust
@@ -640,7 +640,7 @@ impl Solution {
}
}
```
-C#:
+### C#:
三指针法, 感觉会更直观:
```cs
@@ -677,11 +677,11 @@ public class LinkNumbers
}
```
+## 其他解法
+### 使用虚拟头结点解决链表反转
-## 使用虚拟头结点解决链表翻转
-
-> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈)
+> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈)
```java
// 迭代方法:增加虚头结点,使用头插法实现链表翻转
@@ -704,7 +704,7 @@ public static ListNode reverseList1(ListNode head) {
-## 使用栈解决反转链表的问题
+### 使用栈解决反转链表的问题
* 首先将所有的结点入栈
* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。
@@ -743,4 +743,3 @@ public ListNode reverseList(ListNode head) {
-
From 2acceb7474b60ad4aa16eef4b9bf94657ac85aeb Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 15:00:29 +0800
Subject: [PATCH 038/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20024.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?=
=?UTF-8?q?=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0024.两两交换链表中的节点.md | 37 +-
problems/0206.反转链表.md | 745 --------------------------
2 files changed, 23 insertions(+), 759 deletions(-)
delete mode 100644 problems/0206.反转链表.md
diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md
index d7c03b64..c612c4b3 100644
--- a/problems/0024.两两交换链表中的节点.md
+++ b/problems/0024.两两交换链表中的节点.md
@@ -5,7 +5,7 @@
-## 思路
+## 算法公开课
-《代码随想录》算法公开课:[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点](https://www.bilibili.com/video/BV1YT411g7br),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目正常模拟就可以了。
@@ -88,7 +90,8 @@ public:
## 其他语言版本
-C:
+### C:
+
```c
/**
* Definition for singly-linked list.
@@ -132,7 +135,7 @@ struct ListNode* swapPairs(struct ListNode* head){
}
```
-Java:
+### Java:
```Java
// 递归版本
@@ -176,7 +179,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
# 递归版本
# Definition for singly-linked list.
@@ -226,7 +230,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
func swapPairs(head *ListNode) *ListNode {
dummy := &ListNode{
@@ -262,7 +267,8 @@ func swapPairs(head *ListNode) *ListNode {
}
```
-Javascript:
+### Javascript:
+
```javascript
var swapPairs = function (head) {
let ret = new ListNode(0, head), temp = ret;
@@ -277,7 +283,7 @@ var swapPairs = function (head) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function swapPairs(head: ListNode | null): ListNode | null {
@@ -296,7 +302,7 @@ function swapPairs(head: ListNode | null): ListNode | null {
};
```
-Kotlin:
+### Kotlin:
```kotlin
fun swapPairs(head: ListNode?): ListNode? {
@@ -316,7 +322,8 @@ fun swapPairs(head: ListNode?): ListNode? {
}
```
-Swift:
+### Swift:
+
```swift
func swapPairs(_ head: ListNode?) -> ListNode? {
if head == nil || head?.next == nil {
@@ -337,7 +344,8 @@ func swapPairs(_ head: ListNode?) -> ListNode? {
return dummyHead.next
}
```
-Scala:
+### Scala:
+
```scala
// 虚拟头节点
object Solution {
@@ -361,7 +369,8 @@ object Solution {
}
```
-PHP:
+### PHP:
+
```php
//虚拟头结点
function swapPairs($head) {
@@ -404,7 +413,7 @@ function swapPairs($head)
}
```
-Rust:
+### Rust:
```rust
// 虚拟头节点
diff --git a/problems/0206.反转链表.md b/problems/0206.反转链表.md
deleted file mode 100644
index 5a57939a..00000000
--- a/problems/0206.反转链表.md
+++ /dev/null
@@ -1,745 +0,0 @@
-
-
-
-
From a64e11b09ad66f4a1ea22cdc3603e5cde85d88ef Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 15:13:42 +0800
Subject: [PATCH 039/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20019.=E5=88=A0?=
=?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?=
=?UTF-8?q?n=E4=B8=AA=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0019.删除链表的倒数第N个节点.md | 39 +++++++++++++++---------
1 file changed, 24 insertions(+), 15 deletions(-)
diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md
index 84eac96b..1c95ad5b 100644
--- a/problems/0019.删除链表的倒数第N个节点.md
+++ b/problems/0019.删除链表的倒数第N个节点.md
@@ -7,7 +7,7 @@
-## 19.删除链表的倒数第N个节点
+# 19.删除链表的倒数第N个节点
[力扣题目链接](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/)
@@ -31,11 +31,13 @@
输入:head = [1,2], n = 1
输出:[1]
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频再看本篇题解,更有助于大家对链表的理解。**
+
## 思路
-《代码随想录》算法公开课:[链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点](https://www.bilibili.com/video/BV1vW4y1U7Gf),相信结合视频在看本篇题解,更有助于大家对链表的理解。
-
双指针的经典应用,如果要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了。
@@ -93,7 +95,7 @@ public:
## 其他语言版本
-java:
+### Java:
```java
public ListNode removeNthFromEnd(ListNode head, int n){
@@ -120,7 +122,8 @@ public ListNode removeNthFromEnd(ListNode head, int n){
}
```
-Python:
+### Python:
+
```python
# Definition for singly-linked list.
# class ListNode:
@@ -151,7 +154,8 @@ class Solution:
return dummy_head.next
```
-Go:
+### Go:
+
```Go
/**
* Definition for singly-linked list.
@@ -178,7 +182,7 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
}
```
-JavaScript:
+### JavaScript:
```js
/**
@@ -198,7 +202,7 @@ var removeNthFromEnd = function(head, n) {
return ret.next;
};
```
-TypeScript:
+### TypeScript:
版本一(快慢指针法):
@@ -263,7 +267,7 @@ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
};
```
-Kotlin:
+### Kotlin:
```Kotlin
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
@@ -284,7 +288,8 @@ fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
}
```
-Swift:
+### Swift:
+
```swift
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
if head == nil {
@@ -309,8 +314,8 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
}
```
+### PHP:
-PHP:
```php
function removeNthFromEnd($head, $n) {
// 设置虚拟头节点
@@ -332,7 +337,8 @@ function removeNthFromEnd($head, $n) {
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
def removeNthFromEnd(head: ListNode, n: Int): ListNode = {
@@ -356,7 +362,8 @@ object Solution {
}
```
-Rust:
+### Rust:
+
```rust
impl Solution {
pub fn remove_nth_from_end(head: Option
+
From 17d56ed722d41a8d463de2de71c72345a88a2df5 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 15:30:49 +0800
Subject: [PATCH 041/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200142.=E7=8E=AF?=
=?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0141.环形链表.md | 11 ++++++-----
problems/0142.环形链表II.md | 30 +++++++++++++++---------------
2 files changed, 21 insertions(+), 20 deletions(-)
diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md
index 7d7121a0..b1f42ba9 100644
--- a/problems/0141.环形链表.md
+++ b/problems/0141.环形链表.md
@@ -70,7 +70,7 @@ public:
## 其他语言版本
-### Java
+### Java:
```java
public class Solution {
@@ -90,7 +90,7 @@ public class Solution {
}
```
-### Python
+### Python:
```python
class Solution:
@@ -105,7 +105,7 @@ class Solution:
return False
```
-### Go
+### Go:
```go
func hasCycle(head *ListNode) bool {
@@ -125,7 +125,7 @@ func hasCycle(head *ListNode) bool {
}
```
-### JavaScript
+### JavaScript:
```js
var hasCycle = function(head) {
@@ -141,7 +141,7 @@ var hasCycle = function(head) {
};
```
-### TypeScript
+### TypeScript:
```typescript
function hasCycle(head: ListNode | null): boolean {
@@ -163,3 +163,4 @@ function hasCycle(head: ListNode | null): boolean {
+
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md
index f87d2cd9..d20101a7 100644
--- a/problems/0142.环形链表II.md
+++ b/problems/0142.环形链表II.md
@@ -11,7 +11,7 @@
> 找到有没有环已经很不容易了,还要让我找到环的入口?
-## 142.环形链表II
+# 142.环形链表II
[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/)
@@ -24,9 +24,11 @@

-## 思路
+## 算法公开课
-《代码随想录》算法公开课:[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。**
+
+## 思路
这道题目,不仅考察对链表的操作,而且还需要一些数学运算。
@@ -148,7 +150,7 @@ public:
* 时间复杂度: O(n),快慢指针相遇前,指针走的次数小于链表长度,快慢指针相遇后,两个index指针走的次数也小于链表长度,总体为走的次数小于 2n
* 空间复杂度: O(1)
-## 补充
+### 补充
在推理过程中,大家可能有一个疑问就是:**为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y 呢?**
@@ -190,8 +192,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```java
public class Solution {
@@ -217,8 +218,7 @@ public class Solution {
}
```
-
-Python:
+### Python:
```python
(版本一)快慢指针法
@@ -270,7 +270,7 @@ class Solution:
return None
```
-Go:
+### Go:
```go
func detectCycle(head *ListNode) *ListNode {
@@ -290,7 +290,7 @@ func detectCycle(head *ListNode) *ListNode {
}
```
-javaScript
+### JavaScript
```js
// 两种循环实现方式
@@ -334,7 +334,7 @@ var detectCycle = function(head) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function detectCycle(head: ListNode | null): ListNode | null {
@@ -356,7 +356,7 @@ function detectCycle(head: ListNode | null): ListNode | null {
};
```
-Swift:
+### Swift:
```swift
class Solution {
@@ -391,7 +391,7 @@ extension ListNode: Equatable {
}
```
-C:
+### C:
```c
ListNode *detectCycle(ListNode *head) {
@@ -410,7 +410,7 @@ ListNode *detectCycle(ListNode *head) {
}
```
-Scala:
+### Scala:
```scala
object Solution {
@@ -437,7 +437,7 @@ object Solution {
}
```
-C#:
+### C#:
```CSharp
public class Solution
{
From 28b58782375755eda70381aaab630e52b50992c8 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Tue, 18 Jul 2023 15:33:00 +0800
Subject: [PATCH 042/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=93=BE=E8=A1=A8?=
=?UTF-8?q?=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/链表总结篇.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/problems/链表总结篇.md b/problems/链表总结篇.md
index cfbafc45..dacd4dee 100644
--- a/problems/链表总结篇.md
+++ b/problems/链表总结篇.md
@@ -3,7 +3,7 @@
+
From 607e4ebd49c67b6ddd4fcdd91b248e74ed0ed75e Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:18:19 +0800
Subject: [PATCH 043/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?=
=?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/哈希表理论基础.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/problems/哈希表理论基础.md b/problems/哈希表理论基础.md
index 0ab17ec0..3055875a 100644
--- a/problems/哈希表理论基础.md
+++ b/problems/哈希表理论基础.md
@@ -8,6 +8,8 @@
+# 哈希表理论基础
+
## 哈希表
首先什么是 哈希表,哈希表(英文名字为Hash table,国内也有一些算法书籍翻译为散列表,大家看到这两个名称知道都是指hash table就可以了)。
From c628aa65d438aaf4933f2b029ae71c93e3cfce63 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:25:34 +0800
Subject: [PATCH 044/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200242.=E6=9C=89?=
=?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?=
=?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0242.有效的字母异位词.md | 38 ++++++++++++++++---------------
1 file changed, 20 insertions(+), 18 deletions(-)
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md
index 4ea43947..f47d8b05 100644
--- a/problems/0242.有效的字母异位词.md
+++ b/problems/0242.有效的字母异位词.md
@@ -7,7 +7,7 @@
> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的
-## 242.有效的字母异位词
+# 242.有效的字母异位词
[力扣题目链接](https://leetcode.cn/problems/valid-anagram/)
@@ -21,13 +21,14 @@
输入: s = "rat", t = "car"
输出: false
-
**说明:**
你可以假设字符串只包含小写字母。
-## 思路
+## 算法公开课
-本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA)
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。
@@ -88,12 +89,10 @@ public:
* 时间复杂度: O(n)
* 空间复杂度: O(1)
-
-
## 其他语言版本
+### Java:
-Java:
```java
/**
* 242. 有效的字母异位词 字典解法
@@ -121,7 +120,7 @@ class Solution {
}
```
-Python:
+### Python:
```python
class Solution:
@@ -165,7 +164,7 @@ class Solution(object):
return a_count == b_count
```
-Go:
+### Go:
```go
func isAnagram(s string, t string) bool {
@@ -182,7 +181,7 @@ func isAnagram(s string, t string) bool {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -218,7 +217,7 @@ var isAnagram = function(s, t) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function isAnagram(s: string, t: string): boolean {
@@ -233,7 +232,7 @@ function isAnagram(s: string, t: string): boolean {
};
```
-Swift:
+### Swift:
```Swift
func isAnagram(_ s: String, _ t: String) -> Bool {
@@ -257,7 +256,8 @@ func isAnagram(_ s: String, _ t: String) -> Bool {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -292,7 +292,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```rust
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
@@ -312,8 +313,8 @@ impl Solution {
}
```
+### Scala:
-Scala:
```scala
object Solution {
def isAnagram(s: String, t: String): Boolean = {
@@ -337,8 +338,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public bool IsAnagram(string s, string t) {
int sl=s.Length,tl=t.Length;
@@ -360,11 +361,12 @@ C#:
## 相关题目
* [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html)
-* 49.字母异位词分组
-* 438.找到字符串中所有字母异位词
+* [49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/)
+* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)
+
From 19dfa98e4f936f4b5f73886e6c1503c07f913bf8 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:30:12 +0800
Subject: [PATCH 045/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200349.=E4=B8=A4?=
=?UTF-8?q?=E4=B8=AA=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20?=
=?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0349.两个数组的交集.md | 39 +++++++++++++++++++--------------
1 file changed, 22 insertions(+), 17 deletions(-)
diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md
index c2f6ef46..8daf5a35 100644
--- a/problems/0349.两个数组的交集.md
+++ b/problems/0349.两个数组的交集.md
@@ -10,7 +10,7 @@
> 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费!
-## 349. 两个数组的交集
+# 349. 两个数组的交集
[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/)
@@ -22,9 +22,11 @@
输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。
-## 思路
+## 算法公开课
-关于本题,我录制了讲解视频:[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),看视频配合题解,事半功倍。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。
@@ -118,8 +120,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
import java.util.HashSet;
@@ -159,8 +160,9 @@ class Solution {
}
```
-Python3:
+### Python3:
(版本一) 使用字典和集合
+
```python
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
@@ -206,7 +208,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
func intersection(nums1 []int, nums2 []int) []int {
set:=make(map[int]struct{},0) // 用map模拟set
@@ -227,7 +230,7 @@ func intersection(nums1 []int, nums2 []int) []int {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -255,7 +258,7 @@ var intersection = function(nums1, nums2) {
};
```
-TypeScript:
+### TypeScript:
版本一(正常解法):
@@ -280,7 +283,7 @@ function intersection(nums1: number[], nums2: number[]): number[] {
};
```
-Swift:
+### Swift:
```swift
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
@@ -298,7 +301,8 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -327,7 +331,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```rust
use std::collections::HashSet;
impl Solution {
@@ -363,7 +368,8 @@ impl Solution {
}
```
-C:
+### C:
+
```C
int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
@@ -394,7 +400,7 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
}
```
-Scala:
+### Scala:
正常解法:
```scala
@@ -439,8 +445,8 @@ object Solution {
```
+### C#:
-C#:
```csharp
public int[] Intersection(int[] nums1, int[] nums2) {
if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0)
@@ -461,11 +467,10 @@ C#:
```
## 相关题目
-* 350.两个数组的交集 II
+* [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)
-
From 764b3e9e51608baf17183a5e36cab0888a6e46f9 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:34:27 +0800
Subject: [PATCH 046/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200202.=E5=BF=AB?=
=?UTF-8?q?=E4=B9=90=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0202.快乐数.md | 42 +++++++++++++++++++++++------------------
1 file changed, 24 insertions(+), 18 deletions(-)
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md
index 7fe8cd8d..4a77e2b6 100644
--- a/problems/0202.快乐数.md
+++ b/problems/0202.快乐数.md
@@ -28,7 +28,7 @@
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
-# 思路
+## 思路
这道题目看上去貌似一道数学问题,其实并不是!
@@ -80,10 +80,10 @@ public:
-# 其他语言版本
+## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public boolean isHappy(int n) {
@@ -107,8 +107,9 @@ class Solution {
}
```
-Python:
+### Python:
(版本一)使用集合
+
```python
class Solution:
def isHappy(self, n: int) -> bool:
@@ -131,7 +132,7 @@ class Solution:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
- ```
+```
(版本二)使用集合
```python
class Solution:
@@ -146,7 +147,7 @@ class Solution:
if new_num==1: return True
else: n = new_num
return False
-```
+ ```
(版本三)使用数组
```python
class Solution:
@@ -161,7 +162,7 @@ class Solution:
if new_num==1: return True
else: n = new_num
return False
-```
+ ```
(版本四)使用快慢指针
```python
class Solution:
@@ -180,7 +181,7 @@ class Solution:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
-```
+ ```
(版本五)使用集合+精简
```python
class Solution:
@@ -192,7 +193,7 @@ class Solution:
return False
seen.add(n)
return True
-```
+ ```
(版本六)使用数组+精简
```python
class Solution:
@@ -204,8 +205,9 @@ class Solution:
return False
seen.append(n)
return True
-```
-Go:
+ ```
+### Go:
+
```go
func isHappy(n int) bool {
m := make(map[int]bool)
@@ -225,7 +227,7 @@ func getSum(n int) int {
}
```
-javaScript:
+### JavaScript:
```js
var isHappy = function (n) {
@@ -303,7 +305,7 @@ var isHappy = function(n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function isHappy(n: number): boolean {
@@ -322,7 +324,7 @@ function isHappy(n: number): boolean {
};
```
-Swift:
+### Swift:
```swift
// number 每个位置上的数字的平方和
@@ -355,7 +357,8 @@ func isHappy(_ n: Int) -> Bool {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -386,7 +389,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```Rust
use std::collections::HashSet;
impl Solution {
@@ -416,7 +420,8 @@ impl Solution {
}
```
-C:
+### C:
+
```C
typedef struct HashNodeTag {
int key; /* num */
@@ -473,8 +478,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public class Solution {
private int getSum(int n) {
@@ -500,3 +505,4 @@ public class Solution {
+
From 23950d1c3e8541420eaca3f55f0896a84d3ba007 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:43:14 +0800
Subject: [PATCH 047/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20001.=E4=B8=A4?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0001.两数之和.md | 41 ++++++++++++++++++++++-----------------
1 file changed, 23 insertions(+), 18 deletions(-)
diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md
index ca62e3ed..e3fb0fb5 100644
--- a/problems/0001.两数之和.md
+++ b/problems/0001.两数之和.md
@@ -5,7 +5,7 @@
+
From 0922ede8c7ce3fedbd7494834beb224aa3da6311 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:49:56 +0800
Subject: [PATCH 049/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200383.=E8=B5=8E?=
=?UTF-8?q?=E9=87=91=E4=BF=A1=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0383.赎金信.md | 28 ++++++++++++++++------------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md
index e74cdf71..8122240e 100644
--- a/problems/0383.赎金信.md
+++ b/problems/0383.赎金信.md
@@ -34,7 +34,7 @@ canConstruct("aa", "aab") -> true
* 第二点 “你可以假设两个字符串均只含有小写字母。” *说明只有小写字母*,这一点很重要
-## 暴力解法
+### 暴力解法
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
@@ -66,7 +66,7 @@ public:
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
-## 哈希解法
+### 哈希解法
因为题目所只有小写字母,那可以采用空间换取时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数。
@@ -112,8 +112,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
@@ -146,8 +146,9 @@ class Solution {
```
-Python:
+### Python:
(版本一)使用数组
+
```python
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
@@ -213,7 +214,7 @@ class Solution:
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))
```
-Go:
+### Go:
```go
func canConstruct(ransomNote string, magazine string) bool {
@@ -231,7 +232,7 @@ func canConstruct(ransomNote string, magazine string) bool {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -254,7 +255,7 @@ var canConstruct = function(ransomNote, magazine) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function canConstruct(ransomNote: string, magazine: string): boolean {
@@ -275,8 +276,8 @@ function canConstruct(ransomNote: string, magazine: string): boolean {
};
```
+### PHP:
-PHP:
```php
class Solution {
/**
@@ -301,7 +302,8 @@ class Solution {
}
```
-Swift:
+### Swift:
+
```swift
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
var record = Array(repeating: 0, count: 26);
@@ -324,7 +326,8 @@ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
}
```
-Rust:
+### Rust:
+
```rust
impl Solution {
pub fn can_construct(ransom_note: String, magazine: String) -> bool {
@@ -347,7 +350,7 @@ impl Solution {
}
```
-Scala:
+### Scala:
版本一: 使用数组作为哈希表
```scala
@@ -411,8 +414,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public bool CanConstruct(string ransomNote, string magazine) {
if(ransomNote.Length > magazine.Length) return false;
@@ -434,3 +437,4 @@ public bool CanConstruct(string ransomNote, string magazine) {
+
From eb2cdf24bf03dd143072a4260fb11b76ececcc37 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:55:09 +0800
Subject: [PATCH 050/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200015.=E4=B8=89?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0015.三数之和.md | 55 +++++++++++++++++++++++----------------
1 file changed, 32 insertions(+), 23 deletions(-)
diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md
index 9cca779b..4951c90c 100644
--- a/problems/0015.三数之和.md
+++ b/problems/0015.三数之和.md
@@ -26,14 +26,15 @@
[-1, -1, 2]
]
+## 算法公开课
-# 思路
-
-针对本题,我录制了视频讲解:[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),结合本题解一起看,事半功倍!
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦破碎的地方!| LeetCode:15.三数之和](https://www.bilibili.com/video/BV1GW4y127qo),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
**注意[0, 0, 0, 0] 这组数据**
-## 哈希解法
+## 思路
+
+### 哈希解法
两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。
@@ -87,7 +88,7 @@ public:
* 空间复杂度: O(n),额外的 set 开销
-## 双指针
+### 双指针
**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。
@@ -166,9 +167,9 @@ public:
* 空间复杂度: O(1)
-## 去重逻辑的思考
+### 去重逻辑的思考
-### a的去重
+#### a的去重
说道去重,其实主要考虑三个数的去重。 a, b ,c, 对应的就是 nums[i],nums[left],nums[right]
@@ -188,7 +189,7 @@ a 如果重复了怎么办,a是nums里遍历的元素,那么应该直接跳
if (nums[i] == nums[i + 1]) { // 去重操作
continue;
}
-```
+```
那就我们就把 三元组中出现重复元素的情况直接pass掉了。 例如{-1, -1 ,2} 这组数据,当遍历到第一个-1 的时候,判断 下一个也是-1,那这组数据就pass了。
@@ -208,7 +209,7 @@ if (i > 0 && nums[i] == nums[i - 1]) {
这是一个非常细节的思考过程。
-### b与c的去重
+#### b与c的去重
很多同学写本题的时候,去重的逻辑多加了 对right 和left 的去重:(代码中注释部分)
@@ -225,7 +226,7 @@ while (right > left) {
} else {
}
}
-```
+```
但细想一下,这种去重其实对提升程序运行效率是没有帮助的。
@@ -238,7 +239,7 @@ while (right > left) {
所以这种去重 是可以不加的。 仅仅是 把去重的逻辑提前了而已。
-# 思考题
+## 思考题
既然三数之和可以使用双指针法,我们之前讲过的[1.两数之和](https://programmercarl.com/0001.两数之和.html),可不可以使用双指针法呢?
@@ -254,8 +255,8 @@ while (right > left) {
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public List> threeSum(int[] nums) {
@@ -297,8 +298,9 @@ class Solution {
}
```
-Python:
+### Python:
(版本一) 双指针
+
```Python
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
@@ -366,7 +368,7 @@ class Solution:
return result
```
-Go:
+### Go:
```Go
func threeSum(nums []int) [][]int {
@@ -407,7 +409,7 @@ func threeSum(nums []int) [][]int {
}
```
-javaScript:
+### JavaScript:
```js
var threeSum = function(nums) {
@@ -512,7 +514,7 @@ var threeSum = function (nums) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function threeSum(nums: number[]): number[][] {
@@ -553,7 +555,8 @@ function threeSum(nums: number[]): number[][] {
};
```
-ruby:
+### Ruby:
+
```ruby
def is_valid(strs)
symbol_map = {')' => '(', '}' => '{', ']' => '['}
@@ -571,8 +574,8 @@ def is_valid(strs)
end
```
+### PHP:
-PHP:
```php
class Solution {
/**
@@ -613,7 +616,8 @@ class Solution {
}
```
-Swift:
+### Swift:
+
```swift
// 双指针法
func threeSum(_ nums: [Int]) -> [[Int]] {
@@ -654,7 +658,8 @@ func threeSum(_ nums: [Int]) -> [[Int]] {
}
```
-Rust:
+### Rust:
+
```Rust
// 哈希解法
use std::collections::HashSet;
@@ -718,7 +723,8 @@ impl Solution {
}
```
-C:
+### C:
+
```C
//qsort辅助cmp函数
int cmp(const void* ptr1, const void* ptr2) {
@@ -792,7 +798,8 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes
}
```
-C#:
+### C#:
+
```csharp
public class Solution
{
@@ -850,7 +857,8 @@ public class Solution
}
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
// 导包
@@ -898,3 +906,4 @@ object Solution {
+
From 3fe673d804209e55705b2b7206d86e3fc6e87e18 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 14:59:05 +0800
Subject: [PATCH 051/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200018.=E5=9B=9B?=
=?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0018.四数之和.md | 41 +++++++++++++++++++++------------------
1 file changed, 22 insertions(+), 19 deletions(-)
diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md
index a4d41d9b..28c20b7a 100644
--- a/problems/0018.四数之和.md
+++ b/problems/0018.四数之和.md
@@ -27,9 +27,11 @@
[-2, 0, 0, 2]
]
-# 思路
+## 算法公开课
-针对本题,我录制了视频讲解:[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),结合本题解一起看,事半功倍!
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[难在去重和剪枝!| LeetCode:18. 四数之和](https://www.bilibili.com/video/BV1DS4y147US),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
四数之和,和[15.三数之和](https://programmercarl.com/0015.三数之和.html)是一个思路,都是使用双指针法, 基本解法就是在[15.三数之和](https://programmercarl.com/0015.三数之和.html) 的基础上再套一层for循环。
@@ -141,22 +143,16 @@ if (nums[k] + nums[i] > target && nums[k] + nums[i] >= 0) {
if (nums[k] + nums[i] > target && nums[i] >= 0) {
break;
}
-```
+```
因为只要 nums[k] + nums[i] > target,那么 nums[i] 后面的数都是正数的话,就一定 不符合条件了。
不过这种剪枝 其实有点 小绕,大家能够理解 文章给的完整代码的剪枝 就够了。
-
-
-
-
-
-
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public List> fourSum(int[] nums, int target) {
@@ -206,8 +202,9 @@ class Solution {
}
```
-Python:
+### Python:
(版本一) 双指针
+
```python
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
@@ -273,7 +270,8 @@ class Solution(object):
```
-Go:
+### Go:
+
```go
func fourSum(nums []int, target int) [][]int {
if len(nums) < 4 {
@@ -323,7 +321,7 @@ func fourSum(nums []int, target int) [][]int {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -359,7 +357,7 @@ var fourSum = function(nums, target) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function fourSum(nums: number[], target: number): number[][] {
@@ -400,7 +398,7 @@ function fourSum(nums: number[], target: number): number[][] {
};
```
-PHP:
+### PHP:
```php
class Solution {
@@ -445,7 +443,8 @@ class Solution {
}
```
-Swift:
+### Swift:
+
```swift
func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
var res = [[Int]]()
@@ -493,7 +492,8 @@ func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
}
```
-C#:
+### C#:
+
```csharp
public class Solution
{
@@ -555,7 +555,8 @@ public class Solution
}
```
-Rust:
+### Rust:
+
```Rust
use std::cmp::Ordering;
impl Solution {
@@ -603,7 +604,8 @@ impl Solution {
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
// 导包
@@ -651,3 +653,4 @@ object Solution {
+
From ddee6adbbba4db9466cf720307b2e44f10e22140 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:01:01 +0800
Subject: [PATCH 052/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=93=88=E5=B8=8C?=
=?UTF-8?q?=E8=A1=A8=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/哈希表总结.md | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/problems/哈希表总结.md b/problems/哈希表总结.md
index 9ee84f99..67506363 100644
--- a/problems/哈希表总结.md
+++ b/problems/哈希表总结.md
@@ -7,8 +7,10 @@
> 哈希表总结篇如约而至
+# 哈希表总结篇
-# 哈希表理论基础
+
+## 哈希表理论基础
在[关于哈希表,你该了解这些!](https://programmercarl.com/哈希表理论基础.html)中,我们介绍了哈希表的基础理论知识,不同于枯燥的讲解,这里介绍了都是对刷题有帮助的理论知识点。
@@ -32,9 +34,9 @@
**只有对这些数据结构的底层实现很熟悉,才能灵活使用,否则很容易写出效率低下的程序**。
-# 哈希表经典题目
+## 哈希表经典题目
-## 数组作为哈希表
+### 数组作为哈希表
一些应用场景就是为数组量身定做的。
@@ -51,7 +53,7 @@
**上面两道题目用map确实可以,但使用map的空间消耗要比数组大一些,因为map要维护红黑树或者符号表,而且还要做哈希函数的运算。所以数组更加简单直接有效!**
-## set作为哈希表
+### set作为哈希表
在[349. 两个数组的交集](https://programmercarl.com/0349.两个数组的交集.html)中我们给出了什么时候用数组就不行了,需要用set。
@@ -75,7 +77,7 @@ std::set和std::multiset底层实现都是红黑树,std::unordered_set的底
在[202.快乐数](https://programmercarl.com/0202.快乐数.html)中,我们再次使用了unordered_set来判断一个数是否重复出现过。
-## map作为哈希表
+### map作为哈希表
在[1.两数之和](https://programmercarl.com/0001.两数之和.html)中map正式登场。
@@ -110,7 +112,7 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层
所以18. 四数之和,15.三数之和都推荐使用双指针法!
-# 总结
+## 总结
对于哈希表的知识相信很多同学都知道,但是没有成体系。
@@ -123,9 +125,8 @@ std::unordered_map 底层实现为哈希,std::map 和std::multimap 的底层
-
-
+
From 00ef6084c6ea18933e7a63923aad71addee70c6e Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:32:11 +0800
Subject: [PATCH 053/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200344.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0344.反转字符串.md | 35 +++++++++++++++++++++--------------
1 file changed, 21 insertions(+), 14 deletions(-)
diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md
index 1c74f9aa..8a4fed45 100644
--- a/problems/0344.反转字符串.md
+++ b/problems/0344.反转字符串.md
@@ -26,10 +26,12 @@
输入:["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-针对本题,我录制了视频讲解:[字符串基础操作! | LeetCode:344.反转字符串](https://www.bilibili.com/video/BV1fV4y17748),结合本题解一起看,事半功倍!
+
+## 思路
先说一说题外话:
@@ -138,8 +140,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public void reverseString(char[] s) {
@@ -173,8 +175,9 @@ class Solution {
```
-Python:
+### Python:
(版本一) 双指针
+
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
@@ -247,7 +250,8 @@ class Solution:
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
```
-Go:
+### Go:
+
```Go
func reverseString(s []byte) {
left := 0
@@ -260,7 +264,7 @@ func reverseString(s []byte) {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -278,7 +282,7 @@ var reverse = function(s) {
};
```
-TypeScript:
+### TypeScript:
```typescript
/**
@@ -299,7 +303,7 @@ function reverseString(s: string[]): void {
};
```
-Swift:
+### Swift:
```swift
// 双指针 - 元组
@@ -316,7 +320,8 @@ func reverseString(_ s: inout [Character]) {
```
-Rust:
+### Rust:
+
```Rust
impl Solution {
pub fn reverse_string(s: &mut Vec
-
From 5cb250100b49e1b8109ddf01ba2109b257094b47 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:37:57 +0800
Subject: [PATCH 054/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200541.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0541.反转字符串II.md | 33 +++++++++++++++++----------------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md
index 179395b3..80e662f9 100644
--- a/problems/0541.反转字符串II.md
+++ b/problems/0541.反转字符串II.md
@@ -23,9 +23,11 @@
输入: s = "abcdefg", k = 2
输出: "bacdfeg"
-# 思路
+## 算法公开课
-针对本题,我录制了视频讲解:[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),结合本题解一起看,事半功倍!
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目其实也是模拟,实现题目中规定的反转规则就可以了。
@@ -42,8 +44,6 @@
那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。
-# C++代码
-
使用C++库函数reverse的版本如下:
```CPP
@@ -129,7 +129,7 @@ public:
## 其他语言版本
-C:
+### C:
```c
char * reverseStr(char * s, int k){
@@ -152,7 +152,7 @@ char * reverseStr(char * s, int k){
}
```
-Java:
+### Java:
```Java
//解法一
@@ -256,7 +256,8 @@ class Solution {
}
}
```
-Python:
+### Python:
+
```python
class Solution:
def reverseStr(self, s: str, k: int) -> str:
@@ -281,7 +282,7 @@ class Solution:
return ''.join(res)
```
-Python3 (v2):
+### Python3 (v2):
```python
class Solution:
@@ -296,7 +297,7 @@ class Solution:
return s
```
-Go:
+### Go:
```go
func reverseStr(s string, k int) string {
@@ -325,7 +326,7 @@ func reverse(b []byte) {
}
```
-javaScript:
+### JavaScript:
```js
@@ -346,7 +347,7 @@ var reverseStr = function(s, k) {
```
-TypeScript:
+### TypeScript:
```typescript
function reverseStr(s: string, k: number): string {
@@ -368,7 +369,7 @@ function reverseStr(s: string, k: number): string {
};
```
-Swift:
+### Swift:
```swift
func reverseStr(_ s: String, _ k: Int) -> String {
@@ -388,7 +389,8 @@ func reverseStr(_ s: String, _ k: Int) -> String {
}
```
-C#:
+### C#:
+
```csharp
public class Solution
{
@@ -403,7 +405,7 @@ public class Solution
}
}
```
-Scala:
+### Scala:
版本一: (正常解法)
```scala
@@ -469,7 +471,7 @@ object Solution {
}
```
-Rust:
+### Rust:
```Rust
impl Solution {
@@ -503,4 +505,3 @@ impl Solution {
-
From 963eb8fc8e58094388bf6c0f27be7563d7a488b9 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:41:59 +0800
Subject: [PATCH 055/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87?=
=?UTF-8?q?Offer05.=E6=9B=BF=E6=8D=A2=E7=A9=BA=E6=A0=BC=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/剑指Offer05.替换空格.md | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md
index dbad781e..fed08a53 100644
--- a/problems/剑指Offer05.替换空格.md
+++ b/problems/剑指Offer05.替换空格.md
@@ -15,7 +15,7 @@
输入:s = "We are happy."
输出:"We%20are%20happy."
-# 思路
+## 思路
如果想把这道题目做到极致,就不要只用额外的辅助空间了!
@@ -86,7 +86,7 @@ public:
* [142.环形链表II](https://programmercarl.com/0142.环形链表II.html)
* [344.反转字符串](https://programmercarl.com/0344.反转字符串.html)
-# 拓展
+## 拓展
这里也给大家拓展一下字符串和数组有什么差别,
@@ -121,7 +121,8 @@ for (int i = 0; i < a.size(); i++) {
## 其他语言版本
-C:
+### C:
+
```C
char* replaceSpace(char* s){
//统计空格数量
@@ -152,8 +153,8 @@ char* replaceSpace(char* s){
}
```
+### Java:
-Java:
```Java
//使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制
public static String replaceSpace(String s) {
@@ -211,8 +212,8 @@ public String replaceSpace(String s) {
}
```
+### Go:
-Go:
```go
// 遍历添加
func replaceSpace(s string) string {
@@ -264,9 +265,10 @@ func replaceSpace(s string) string {
+### python:
+
+因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1)
-python:
-#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1)
(版本一)转换成列表,并且添加相匹配的空间,然后进行填充
```python
class Solution:
@@ -328,7 +330,7 @@ class Solution:
def replaceSpace(self, s: str) -> str:
return s.replace(' ', '%20')
```
-javaScript:
+### JavaScript:
```js
/**
@@ -366,7 +368,7 @@ javaScript:
};
```
-TypeScript:
+### TypeScript:
```typescript
function replaceSpace(s: string): string {
@@ -393,7 +395,7 @@ function replaceSpace(s: string): string {
};
```
-Swift:
+### Swift:
```swift
func replaceSpace(_ s: String) -> String {
@@ -434,7 +436,7 @@ func replaceSpace(_ s: String) -> String {
}
```
-Scala:
+### Scala:
方式一: 双指针
```scala
@@ -491,8 +493,8 @@ object Solution {
}
```
+### PHP:
-PHP:
```php
function replaceSpace($s){
$sLen = strlen($s);
@@ -527,7 +529,7 @@ function spaceLen($s){
}
```
-Rust
+### Rust:
```Rust
impl Solution {
@@ -563,3 +565,4 @@ impl Solution {
+
From e70ca923440e656d4c62761fcff2ff40e9c17b32 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:47:12 +0800
Subject: [PATCH 056/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200151.=E5=8F=8D?=
=?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E5=8D=95?=
=?UTF-8?q?=E8=AF=8D=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0151.翻转字符串里的单词.md | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md
index 6dd3cd49..19ccb725 100644
--- a/problems/0151.翻转字符串里的单词.md
+++ b/problems/0151.翻转字符串里的单词.md
@@ -28,10 +28,11 @@
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-针对本题,我录制了视频讲解:[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),结合本题解一起看,事半功倍!
+## 思路
**这道题目可以说是综合考察了字符串的多种操作。**
@@ -204,8 +205,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
class Solution {
@@ -433,9 +433,10 @@ class Solution {
}
```
-python:
+### python:
(版本一)先删除空白,然后整个反转,最后单词反转。
**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)**
+
```Python
class Solution:
def reverseWords(self, s: str) -> str:
@@ -467,7 +468,7 @@ class Solution:
return " ".join(words)
```
-Go:
+### Go:
版本一:
@@ -571,7 +572,8 @@ func reverse(b *[]byte, left, right int) {
-javaScript:
+### JavaScript:
+
```js
/**
* @param {string} s
@@ -630,7 +632,7 @@ function reverse(strArr, start, end) {
}
```
-TypeScript:
+### TypeScript:
```typescript
function reverseWords(s: string): string {
@@ -689,7 +691,7 @@ function reverseWords(s: string): string {
};
```
-Swift:
+### Swift:
```swift
func reverseWords(_ s: String) -> String {
@@ -766,7 +768,7 @@ func reverseWord(_ s: inout [Character]) {
}
```
-Scala:
+### Scala:
```scala
object Solution {
@@ -824,8 +826,8 @@ object Solution {
}
```
+### PHP:
-PHP:
```php
function reverseWords($s) {
$this->removeExtraSpaces($s);
@@ -872,7 +874,7 @@ function reverseString(&$s, $start, $end) {
return ;
}
```
-Rust:
+### Rust:
```Rust
// 根据C++版本二思路进行实现
@@ -924,7 +926,7 @@ pub fn remove_extra_spaces(s: &mut Vec
+
From 370a4d1c05b1204c652c00bcb90702947df11795 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 15:50:44 +0800
Subject: [PATCH 057/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=89=91=E6=8C=87?=
=?UTF-8?q?Offer58-II.=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97=E7=AC=A6?=
=?UTF-8?q?=E4=B8=B2=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/剑指Offer58-II.左旋转字符串.md | 26 ++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md
index 6cd88456..008b7915 100644
--- a/problems/剑指Offer58-II.左旋转字符串.md
+++ b/problems/剑指Offer58-II.左旋转字符串.md
@@ -24,7 +24,7 @@
限制:
1 <= k < s.length <= 10000
-# 思路
+## 思路
为了让本题更有意义,提升一下本题难度:**不能申请额外空间,只能在本串上操作**。
@@ -71,7 +71,7 @@ public:
是不是发现这代码也太简单了,哈哈。
-# 总结
+## 总结
此时我们已经反转好多次字符串了,来一起回顾一下吧。
@@ -86,7 +86,7 @@ public:
好了,反转字符串一共就介绍到这里,相信大家此时对反转字符串的常见操作已经很了解了。
-# 题外话
+## 题外话
一些同学热衷于使用substr,来做这道题。
其实使用substr 和 反转 时间复杂度是一样的 ,都是O(n),但是使用substr申请了额外空间,所以空间复杂度是O(n),而反转方法的空间复杂度是O(1)。
@@ -96,7 +96,8 @@ public:
## 其他语言版本
-Java:
+### Java:
+
```java
class Solution {
public String reverseLeftWords(String s, int n) {
@@ -141,7 +142,7 @@ class Solution {
}
```
-python:
+### python:
(版本一)使用切片
```python
@@ -211,7 +212,7 @@ class Solution:
```
-Go:
+### Go:
```go
func reverseLeftWords(s string, n int) string {
@@ -234,8 +235,7 @@ func reverse(b []byte, left, right int){
}
```
-
-JavaScript:
+### JavaScript:
```javascript
var reverseLeftWords = function(s, n) {
@@ -279,7 +279,7 @@ var reverseLeftWords = function (s, n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function reverseLeftWords(s: string, n: number): string {
@@ -311,7 +311,7 @@ function reverseLeftWords(s: string, n: number): string {
};
```
-Swift:
+### Swift:
```swift
func reverseLeftWords(_ s: String, _ n: Int) -> String {
@@ -358,8 +358,7 @@ function reverse(&$s, $start, $end) {
}
```
-
-Scala:
+### Scala:
```scala
object Solution {
@@ -388,7 +387,7 @@ object Solution {
}
```
-Rust:
+### Rust:
```Rust
impl Solution {
@@ -419,3 +418,4 @@ impl Solution {
+
From 5acebcc6f70f37d675f03356ecfc2f8f43b5949b Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 16:00:41 +0800
Subject: [PATCH 058/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200028.=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0strStr=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0028.实现strStr.md | 51 ++++++++++++++++++-------------------
1 file changed, 25 insertions(+), 26 deletions(-)
diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md
index ac984d1a..53b57fd5 100644
--- a/problems/0028.实现strStr.md
+++ b/problems/0028.实现strStr.md
@@ -28,7 +28,7 @@
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
-# 思路
+## 思路
本题是KMP 经典题目。
@@ -60,13 +60,13 @@ KMP的经典思想就是:**当出现字符串不匹配时,可以记录一部
读完本篇可以顺便把leetcode上28.实现strStr()题目做了。
-# 什么是KMP
+### 什么是KMP
说到KMP,先说一下KMP这个名字是怎么来的,为什么叫做KMP呢。
因为是由这三位学者发明的:Knuth,Morris和Pratt,所以取了三位学者名字的首字母。所以叫做KMP
-# KMP有什么用
+### KMP有什么用
KMP主要应用在字符串匹配上。
@@ -84,7 +84,7 @@ KMP的主要思想是**当出现字符串不匹配时,可以知道一部分之
下面Carl就带大家把KMP的精髓,next数组弄清楚。
-# 什么是前缀表
+### 什么是前缀表
写过KMP的同学,一定都写过next数组,那么这个next数组究竟是个啥呢?
@@ -122,7 +122,7 @@ next数组就是一个前缀表(prefix table)。
那么什么是前缀表:**记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。**
-# 最长公共前后缀?
+### 最长公共前后缀
文章中字符串的**前缀是指不包含最后一个字符的所有以第一个字符开头的连续子串**。
@@ -144,7 +144,7 @@ next数组就是一个前缀表(prefix table)。
等等.....。
-# 为什么一定要用前缀表
+### 为什么一定要用前缀表
这就是前缀表,那为啥就能告诉我们 上次匹配的位置,并跳过去呢?
@@ -163,7 +163,7 @@ next数组就是一个前缀表(prefix table)。
**很多介绍KMP的文章或者视频并没有把为什么要用前缀表?这个问题说清楚,而是直接默认使用前缀表。**
-# 如何计算前缀表
+### 如何计算前缀表
接下来就要说一说怎么计算前缀表。
@@ -205,7 +205,7 @@ next数组就是一个前缀表(prefix table)。
最后就在文本串中找到了和模式串匹配的子串了。
-# 前缀表与next数组
+### 前缀表与next数组
很多KMP算法的时间都是使用next数组来做回退操作,那么next数组与前缀表有什么关系呢?
@@ -217,7 +217,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减
后面我会提供两种不同的实现代码,大家就明白了。
-# 使用next数组来匹配
+### 使用next数组来匹配
**以下我们以前缀表统一减一之后的next数组来做演示**。
@@ -229,7 +229,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减

-# 时间复杂度分析
+### 时间复杂度分析
其中n为文本串长度,m为模式串长度,因为在匹配的过程中,根据前缀表不断调整匹配的位置,可以看出匹配的过程是O(n),之前还要单独生成next数组,时间复杂度是O(m)。所以整个KMP算法的时间复杂度是O(n+m)的。
@@ -239,7 +239,7 @@ next数组就可以是前缀表,但是很多实现都是把前缀表统一减
都知道使用KMP算法,一定要构造next数组。
-# 构造next数组
+### 构造next数组
我们定义一个函数getNext来构建next数组,函数参数为指向next数组的指针,和一个字符串。 代码如下:
@@ -338,7 +338,7 @@ void getNext(int* next, const string& s){
得到了next数组之后,就要用这个来做匹配了。
-# 使用next数组来做匹配
+### 使用next数组来做匹配
在文本串s里 找是否出现过模式串t。
@@ -403,7 +403,7 @@ for (int i = 0; i < s.size(); i++) { // 注意i就从0开始
此时所有逻辑的代码都已经写出来了,力扣 28.实现strStr 题目的整体代码如下:
-# 前缀表统一减一 C++代码实现
+### 前缀表统一减一 C++代码实现
```CPP
class Solution {
@@ -447,7 +447,7 @@ public:
* 时间复杂度: O(n + m)
* 空间复杂度: O(m), 只需要保存字符串needle的前缀表
-# 前缀表(不减一)C++实现
+### 前缀表(不减一)C++实现
那么前缀表就不减一了,也不右移的,到底行不行呢?
@@ -546,7 +546,7 @@ public:
* 空间复杂度: O(m)
-# 总结
+## 总结
我们介绍了什么是KMP,KMP可以解决什么问题,然后分析KMP算法里的next数组,知道了next数组就是前缀表,再分析为什么要是前缀表而不是什么其他表。
@@ -563,8 +563,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
class Solution {
@@ -691,8 +690,9 @@ class Solution {
}
```
-Python3:
+### Python3:
(版本一)前缀表(减一)
+
```python
class Solution:
def getNext(self, next, s):
@@ -781,9 +781,9 @@ class Solution:
def strStr(self, haystack: str, needle: str) -> int:
return haystack.find(needle)
-```
+```
-Go:
+### Go:
```go
// 方法一:前缀表使用减1实现
@@ -871,7 +871,7 @@ func strStr(haystack string, needle string) int {
}
```
-JavaScript版本
+### JavaScript:
> 前缀表统一减一
@@ -959,7 +959,7 @@ var strStr = function (haystack, needle) {
};
```
-TypeScript版本:
+### TypeScript:
> 前缀表统一减一
@@ -1036,7 +1036,7 @@ function strStr(haystack: string, needle: string): number {
}
```
-Swift 版本
+### Swift:
> 前缀表统一减一
@@ -1196,7 +1196,7 @@ func strStr(_ haystack: String, _ needle: String) -> Int {
```
-PHP:
+### PHP:
> 前缀表统一减一
```php
@@ -1272,7 +1272,7 @@ function getNext(&$next, $s){
}
```
-Rust:
+### Rust:
> 前缀表统一不减一
```Rust
@@ -1362,4 +1362,3 @@ impl Solution {
-
From 202dd382d29eb34f311d5f35f001255b2b770402 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Wed, 19 Jul 2023 16:06:12 +0800
Subject: [PATCH 059/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200459.=E9=87=8D?=
=?UTF-8?q?=E5=A4=8D=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0459.重复的子字符串.md | 35 +++++++++++++++------------------
1 file changed, 16 insertions(+), 19 deletions(-)
diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md
index e26d04ad..f99102ab 100644
--- a/problems/0459.重复的子字符串.md
+++ b/problems/0459.重复的子字符串.md
@@ -5,8 +5,6 @@
+
From c3ebc91d4db0fc5b86945b910babd8c4ad2328b2 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Thu, 20 Jul 2023 14:27:16 +0800
Subject: [PATCH 061/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=8F=8C=E6=8C=87?=
=?UTF-8?q?=E9=92=88=E6=80=BB=E7=BB=93=20=E6=8E=92=E7=89=88=E6=A0=BC?=
=?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/双指针总结.md | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/problems/双指针总结.md b/problems/双指针总结.md
index 04a8cb9a..6621e039 100644
--- a/problems/双指针总结.md
+++ b/problems/双指针总结.md
@@ -8,7 +8,8 @@
相信大家已经对双指针法很熟悉了,但是双指针法并不隶属于某一种数据结构,我们在讲解数组,链表,字符串都用到了双指针法,所有有必要针对双指针法做一个总结。
-# 数组篇
+# 双指针总结篇
+## 数组篇
在[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)中,原地移除数组上的元素,我们说到了数组上的元素,不能真正的删除,只能覆盖。
@@ -26,7 +27,7 @@ for (int i = 0; i < array.size(); i++) {
所以此时使用双指针法才展现出效率的优势:**通过两个指针在一个for循环下完成两个for循环的工作。**
-# 字符串篇
+## 字符串篇
在[字符串:这道题目,使用库函数一行代码搞定](https://programmercarl.com/0344.反转字符串.html)中讲解了反转字符串,注意这里强调要原地反转,要不然就失去了题目的意义。
@@ -48,7 +49,7 @@ for (int i = 0; i < array.size(); i++) {
**主要还是大家用erase用的比较随意,一定要注意for循环下用erase的情况,一般可以用双指针写效率更高!**
-# 链表篇
+## 链表篇
翻转链表是现场面试,白纸写代码的好题,考察了候选者对链表以及指针的熟悉程度,而且代码也不长,适合在白纸上写。
@@ -62,7 +63,7 @@ for (int i = 0; i < array.size(); i++) {
那么找到环的入口,其实需要点简单的数学推理,我在文章中把找环的入口清清楚楚的推理的一遍,如果对找环入口不够清楚的同学建议自己看一看[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)。
-# N数之和篇
+## N数之和篇
在[哈希表:解决了两数之和,那么能解决三数之和么?](https://programmercarl.com/0015.三数之和.html)中,讲到使用哈希法可以解决1.两数之和的问题
@@ -87,7 +88,7 @@ for (int i = 0; i < array.size(); i++) {
同样的道理,五数之和,n数之和都是在这个基础上累加。
-# 总结
+## 总结
本文中一共介绍了leetcode上九道使用双指针解决问题的经典题目,除了链表一些题目一定要使用双指针,其他题目都是使用双指针来提高效率,一般是将O(n^2)的时间复杂度,降为$O(n)$。
From de770c60e5ae781a9e65071e03ba4876237ed6fb Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Thu, 20 Jul 2023 14:36:04 +0800
Subject: [PATCH 062/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E6=A0=88=E4=B8=8E?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?=
=?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/栈与队列理论基础.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/problems/栈与队列理论基础.md b/problems/栈与队列理论基础.md
index 0075deb6..ad748e48 100644
--- a/problems/栈与队列理论基础.md
+++ b/problems/栈与队列理论基础.md
@@ -4,8 +4,11 @@
+
From 4b5e198b170bade71d565d8ba3f7e29543f3bbef Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Thu, 20 Jul 2023 14:40:33 +0800
Subject: [PATCH 063/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200232.=E7=94=A8?=
=?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0232.用栈实现队列.md | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md
index 4a57ee96..c510fc12 100644
--- a/problems/0232.用栈实现队列.md
+++ b/problems/0232.用栈实现队列.md
@@ -36,11 +36,12 @@ queue.empty(); // 返回 false
* 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
* 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
## 思路
-《代码随想录》算法公开课:[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对栈和队列的理解。
-
-
这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。
使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈**一个输入栈,一个输出栈**,这里要注意输入栈和输出栈的关系。
@@ -132,7 +133,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
class MyQueue {
@@ -179,8 +180,8 @@ class MyQueue {
```
+### Python:
-Python:
```python
class MyQueue:
@@ -231,8 +232,8 @@ class MyQueue:
```
+### Go:
-Go:
```Go
type MyQueue struct {
stackIn []int //输入栈
@@ -283,7 +284,7 @@ func (this *MyQueue) Empty() bool {
}
```
- javaScript:
+### JavaScript:
```js
// 使用两个数组的栈方法(push, pop) 实现队列
@@ -338,7 +339,7 @@ MyQueue.prototype.empty = function() {
};
```
-TypeScript:
+### TypeScript:
```typescript
class MyQueue {
@@ -374,7 +375,7 @@ class MyQueue {
}
```
-Swift:
+### Swift:
```swift
class MyQueue {
@@ -413,7 +414,8 @@ class MyQueue {
}
```
-C:
+### C:
+
```C
/*
1.两个type为int的数组(栈),大小为100
@@ -490,8 +492,8 @@ void myQueueFree(MyQueue* obj) {
}
```
+### C#:
-C#:
```csharp
public class MyQueue {
Stack
-
From c18dbf059544b7bd100ff101cc92a2ee241434e5 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Thu, 20 Jul 2023 14:45:24 +0800
Subject: [PATCH 064/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200225.=E7=94=A8?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0225.用队列实现栈.md | 37 ++++++++++++++++++-----------------
1 file changed, 19 insertions(+), 18 deletions(-)
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md
index 94c79404..13b742f8 100644
--- a/problems/0225.用队列实现栈.md
+++ b/problems/0225.用队列实现栈.md
@@ -25,11 +25,11 @@
* 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
* 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
-《代码随想录》算法公开课:[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对链表的理解。
+## 思路
(这里要强调是单向队列)
@@ -114,7 +114,7 @@ public:
* 时间复杂度: push为O(n),其他为O(1)
* 空间复杂度: O(n)
-# 优化
+## 优化
其实这道题目就是用一个队列就够了。
@@ -162,9 +162,9 @@ public:
* 空间复杂度: O(n)
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
使用两个 Queue 实现方法1
```java
@@ -404,7 +404,7 @@ class MyStack {
}
```
-Python:
+### Python:
```python
from collections import deque
@@ -496,8 +496,7 @@ class MyStack:
return not self.que
```
-
-Go:
+### Go:
使用两个队列实现
```go
@@ -628,9 +627,7 @@ func (this *MyStack) Empty() bool {
*/
```
-
-
-javaScript:
+### JavaScript:
使用数组(push, shift)模拟队列
@@ -740,7 +737,7 @@ MyStack.prototype.empty = function() {
```
-TypeScript:
+### TypeScript:
版本一:使用两个队列模拟栈
@@ -812,7 +809,7 @@ class MyStack {
}
```
-Swift
+### Swift:
```Swift
// 定义一个队列数据结构
@@ -931,8 +928,9 @@ class MyStack {
}
}
```
-Scala:
+### Scala:
使用两个队列模拟栈:
+
```scala
import scala.collection.mutable
@@ -1015,8 +1013,8 @@ class MyStack() {
}
```
+### C#:
-C#:
```csharp
public class MyStack {
Queue
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+# 栈与队列总结篇 - -# 栈与队列的理论基础 +## 栈与队列的理论基础 首先我们在[栈与队列:来看看栈和队列不为人知的一面](https://programmercarl.com/栈与队列理论基础.html)中讲解了栈和队列的理论基础。 @@ -37,9 +37,9 @@ **一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时在去弹出元素就是栈的顺序了。** -# 栈经典题目 +## 栈经典题目 -## 栈在系统中的应用 +### 栈在系统中的应用 如果还记得编译原理的话,编译器在 词法分析的过程中处理括号、花括号等这个符号的逻辑,就是使用了栈这种数据结构。 @@ -59,7 +59,7 @@ cd a/b/c/../../ **所以数据结构与算法的应用往往隐藏在我们看不到的地方!** -## 括号匹配问题 +### 括号匹配问题 在[栈与队列:系统中处处都是栈的应用](https://programmercarl.com/0020.有效的括号.html)中我们讲解了括号匹配问题。 @@ -75,23 +75,23 @@ cd a/b/c/../../ 这里还有一些技巧,在匹配左括号的时候,右括号先入栈,就只需要比较当前元素和栈顶相不相等就可以了,比左括号先入栈代码实现要简单的多了! -## 字符串去重问题 +### 字符串去重问题 在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中讲解了字符串去重问题。 1047. 删除字符串中的所有相邻重复项 思路就是可以把字符串顺序放到一个栈中,然后如果相同的话 栈就弹出,这样最后栈里剩下的元素都是相邻不相同的元素了。 -## 逆波兰表达式问题 +### 逆波兰表达式问题 在[栈与队列:有没有想过计算机是如何处理表达式的?](https://programmercarl.com/0150.逆波兰表达式求值.html)中讲解了求逆波兰表达式。 本题中每一个子表达式要得出一个结果,然后拿这个结果再进行运算,那么**这岂不就是一个相邻字符串消除的过程,和[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中的对对碰游戏是不是就非常像了。** -# 队列的经典题目 +## 队列的经典题目 -## 滑动窗口最大值问题 +### 滑动窗口最大值问题 在[栈与队列:滑动窗口里求最大值引出一个重要数据结构](https://programmercarl.com/0239.滑动窗口最大值.html)中讲解了一种数据结构:单调队列。 @@ -119,7 +119,7 @@ cd a/b/c/../../ 我们用deque作为单调队列的底层数据结构,C++中deque是stack和queue默认的底层实现容器(这个我们之前已经讲过),deque是可以两边扩展的,而且deque里元素并不是严格的连续分布的。 -## 求前 K 个高频元素 +### 求前 K 个高频元素 在[栈与队列:求前 K 个高频元素和队列有啥关系?](https://programmercarl.com/0347.前K个高频元素.html)中讲解了求前 K 个高频元素。 @@ -143,7 +143,7 @@ cd a/b/c/../../ 所以排序的过程的时间复杂度是$O(\log k)$,整个算法的时间复杂度是$O(n\log k)$。 -# 总结 +## 总结 在栈与队列系列中,我们强调栈与队列的基础,也是很多同学容易忽视的点。 @@ -162,3 +162,4 @@ cd a/b/c/../../
+
From c6bcd423d6bdab6375e3af8bc7c3c01bfc0518a1 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Thu, 20 Jul 2023 15:48:05 +0800
Subject: [PATCH 071/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?=
=?UTF-8?q?=E6=A0=91=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/二叉树理论基础.md | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md
index 4098cf1f..184dba60 100644
--- a/problems/二叉树理论基础.md
+++ b/problems/二叉树理论基础.md
@@ -8,8 +8,11 @@
# 二叉树理论基础篇
+## 算法公开课
-《代码随想录》算法视频公开课:[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[关于二叉树,你该了解这些!](https://www.bilibili.com/video/BV1Hy4y1t7ij),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 题目分类
题目分类大纲如下:
@@ -189,8 +192,7 @@ struct TreeNode {
## 其他语言版本
-
-Java:
+### Java:
```java
public class TreeNode {
@@ -208,8 +210,7 @@ public class TreeNode {
}
```
-
-Python:
+### Python:
```python
class TreeNode:
@@ -219,7 +220,7 @@ class TreeNode:
self.right = right
```
-Go:
+### Go:
```go
type TreeNode struct {
@@ -229,7 +230,7 @@ type TreeNode struct {
}
```
-JavaScript:
+### JavaScript:
```javascript
function TreeNode(val, left, right) {
@@ -239,7 +240,7 @@ function TreeNode(val, left, right) {
}
```
-TypeScript:
+### TypeScript:
```typescript
class TreeNode {
@@ -254,7 +255,7 @@ class TreeNode {
}
```
-Swift:
+### Swift:
```Swift
class TreeNode参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
- +> 一看就会,一写就废! # 二叉树的递归遍历 +## 算法公开课 -《代码随想录》算法视频公开课:[每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!](https://www.bilibili.com/video/BV1Wh411S7xt),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!](https://www.bilibili.com/video/BV1Wh411S7xt),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - -> 一看就会,一写就废! +## 思路 这次我们要好好谈一谈递归,为什么很多同学看递归算法都是“一看就会,一写就废”。 @@ -109,14 +109,10 @@ void traversal(TreeNode* cur, vector参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+> 听说还可以用非递归的方式 # 二叉树的迭代遍历 -《代码随想录》算法视频公开课: -* [写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2) -* [写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g) -相信结合视频在看本篇题解,更有助于大家对本题的理解。 +## 算法公开课 +[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html): -> 听说还可以用非递归的方式 +* **[写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2)** +* **[写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g)** +**相信结合视频在看本篇题解,更有助于大家对本题的理解。** 看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目: @@ -21,13 +22,15 @@ * [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/) * [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) +## 思路 + 为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢? 我们在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中提到了,**递归的实现就是:每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中**,然后递归返回的时候,从栈顶弹出上一次递归的各项参数,所以这就是递归为什么可以返回上一层位置的原因。 此时大家应该知道我们用栈也可以是实现二叉树的前后中序遍历了。 -## 前序遍历(迭代法) +### 前序遍历(迭代法) 我们先看一下前序遍历。 @@ -69,7 +72,7 @@ public: 但接下来,**再用迭代法写中序遍历的时候,会发现套路又不一样了,目前的前序遍历的逻辑无法直接应用到中序遍历上。** -## 中序遍历(迭代法) +### 中序遍历(迭代法) 为了解释清楚,我说明一下 刚刚在迭代的过程中,其实我们有两个操作: @@ -112,7 +115,7 @@ public: ``` -## 后序遍历(迭代法) +### 后序遍历(迭代法) 再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: @@ -142,7 +145,7 @@ public: ``` -# 总结 +## 总结 此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不像递归写法那样代码稍做调整,就可以实现前后中序。 @@ -155,11 +158,9 @@ public: 当然可以,这种写法,还不是很好理解,我们将在下一篇文章里重点讲解,敬请期待! +## 其他语言版本 - -# 其他语言版本 - -Java: +### Java: ```java // 前序遍历顺序:中-左-右,入栈顺序:中-右-左 @@ -233,10 +234,7 @@ class Solution { } ``` - - - -Python: +### Python: ```python # 前序遍历-迭代-LC144_二叉树的前序遍历 @@ -281,7 +279,7 @@ class Solution: # 取栈顶元素右结点 cur = cur.right return result - ``` +``` ```python # 后序遍历-迭代-LC145_二叉树的后序遍历 @@ -303,10 +301,9 @@ class Solution: stack.append(node.right) # 将最终的数组翻转 return result[::-1] -``` + ``` - -Go: +### Go: > 迭代法前序遍历 @@ -400,7 +397,7 @@ func inorderTraversal(root *TreeNode) []int { } ``` -javaScript: +### JavaScript: ```js @@ -464,7 +461,7 @@ var postorderTraversal = function(root, res = []) { }; ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历(迭代法) @@ -519,7 +516,7 @@ function postorderTraversal(root: TreeNode | null): number[] { }; ``` -Swift: +### Swift: ```swift // 前序遍历迭代法 @@ -578,7 +575,8 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] { return result } ``` -Scala: +### Scala: + ```scala // 前序遍历(迭代法) object Solution { @@ -645,7 +643,7 @@ object Solution { } ``` -rust: +### Rust: ```rust use std::cell::RefCell; From cc510357c1fe6f0cf25fd4e800c2a89b03b4659f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 16:22:00 +0800 Subject: [PATCH 074/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 27464269..8089af64 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -5,10 +5,11 @@参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+> 统一写法是一种什么感觉 # 二叉树的统一迭代法 -> 统一写法是一种什么感觉 +## 思路 此时我们在[二叉树:一入递归深似海,从此offer是路人](https://programmercarl.com/二叉树的递归遍历.html)中用递归的方式,实现了二叉树前中后序的遍历。 @@ -28,7 +29,7 @@ 如何标记呢,**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法也可以叫做标记法。 -## 迭代法中序遍历 +### 迭代法中序遍历 中序遍历代码如下:(详细注释) @@ -71,7 +72,7 @@ public: 此时我们再来看前序遍历代码。 -## 迭代法前序遍历 +### 迭代法前序遍历 迭代法前序遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -102,7 +103,7 @@ public: }; ``` -## 迭代法后序遍历 +### 迭代法后序遍历 后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -143,15 +144,11 @@ public: 所以大家根据自己的个人喜好,对于二叉树的前中后序遍历,选择一种自己容易理解的递归和迭代法。 +## 其他语言版本 - - - -# 其他语言版本 - - -Java: +### Java: 迭代法前序遍历代码如下: + ```java class Solution { public List
+
From 8e34d5ff5749229eeaa7dce33e1ed74c0dd992a2 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Thu, 20 Jul 2023 19:17:38 +0800
Subject: [PATCH 075/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200102.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?=
=?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0102.二叉树的层序遍历.md | 284 ++++++++++++++++--------------
1 file changed, 149 insertions(+), 135 deletions(-)
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index c2ad9508..ce9a247c 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -8,21 +8,23 @@
# 二叉树层序遍历登场!
-《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
学会二叉树的层序遍历,可以一口气打完以下十题:
-* 102.二叉树的层序遍历
-* 107.二叉树的层次遍历II
-* 199.二叉树的右视图
-* 637.二叉树的层平均值
-* 429.N叉树的层序遍历
-* 515.在每个树行中找最大值
-* 116.填充每个节点的下一个右侧节点指针
-* 117.填充每个节点的下一个右侧节点指针II
-* 104.二叉树的最大深度
-* 111.二叉树的最小深度
+* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
+* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
+* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
+* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
+* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
+* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
+* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
+* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
@@ -31,7 +33,7 @@
-# 102.二叉树的层序遍历
+## 102.二叉树的层序遍历
[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
@@ -39,7 +41,7 @@

-思路:
+### 思路
我们之前讲过了三篇关于二叉树的深度优先遍历的文章:
@@ -63,7 +65,7 @@
代码如下:**这份代码也可以作为二叉树层序遍历的模板,打十个就靠它了**。
-C++代码:
+c++代码如下:
```CPP
class Solution {
@@ -111,7 +113,9 @@ public:
};
```
-java:
+### 其他语言版本
+
+#### Java:
```Java
// 102.二叉树的层序遍历
@@ -167,7 +171,7 @@ class Solution {
}
```
-python3代码:
+#### Python:
```python
@@ -219,12 +223,9 @@ class Solution:
self.helper(node.left, level + 1, levels)
self.helper(node.right, level + 1, levels)
-
```
-
-
-go:
+#### Go:
```go
/**
@@ -320,7 +321,7 @@ func levelOrder(root *TreeNode) (res [][]int) {
}
```
-javascript代码:
+#### Javascript:
```javascript
var levelOrder = function(root) {
@@ -350,7 +351,7 @@ var levelOrder = function(root) {
```
-TypeScript:
+#### TypeScript:
```typescript
function levelOrder(root: TreeNode | null): number[][] {
@@ -377,7 +378,7 @@ function levelOrder(root: TreeNode | null): number[][] {
};
```
-Swift:
+#### Swift:
```swift
func levelOrder(_ root: TreeNode?) -> [[Int]] {
@@ -403,7 +404,7 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
}
```
-Scala:
+#### Scala:
```scala
// 102.二叉树的层序遍历
@@ -430,7 +431,7 @@ object Solution {
}
```
-Rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -466,7 +467,7 @@ impl Solution {
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
-# 107.二叉树的层次遍历 II
+## 107.二叉树的层次遍历 II
[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
@@ -474,7 +475,7 @@ impl Solution {

-思路:
+### 思路
相对于102.二叉树的层序遍历,就是最后把result数组反转一下就可以了。
@@ -506,7 +507,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
class Solution:
@@ -537,7 +540,7 @@ class Solution:
return result[::-1]
```
-Java:
+#### Java:
```java
// 107. 二叉树的层序遍历 II
@@ -618,12 +621,10 @@ class Solution {
return ans;
}
-}
+
```
-
-
-go:
+#### Go:
```GO
/**
@@ -662,7 +663,7 @@ func levelOrderBottom(root *TreeNode) [][]int {
}
```
-javascript代码
+#### Javascript:
```javascript
var levelOrderBottom = function(root) {
@@ -688,7 +689,7 @@ var levelOrderBottom = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function levelOrderBottom(root: TreeNode | null): number[][] {
@@ -711,7 +712,7 @@ function levelOrderBottom(root: TreeNode | null): number[][] {
};
```
-Swift:
+#### Swift:
```swift
func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
@@ -737,8 +738,7 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
}
```
-
-Scala:
+#### Scala:
```scala
// 107.二叉树的层次遍历II
@@ -764,7 +764,10 @@ object Solution {
res.reverse.toList
}
-Rust:
+
+```
+
+#### Rust:
```rust
use std::cell::RefCell;
@@ -796,7 +799,7 @@ impl Solution {
}
```
-# 199.二叉树的右视图
+## 199.二叉树的右视图
[力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/)
@@ -804,7 +807,7 @@ impl Solution {

-思路:
+### 思路
层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。
@@ -832,7 +835,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
# Definition for a binary tree node.
@@ -866,8 +871,7 @@ class Solution:
return right_view
```
-
-Java:
+#### Java:
```java
// 199.二叉树的右视图
@@ -911,7 +915,7 @@ public class N0199 {
}
```
-go:
+#### Go:
```GO
/**
@@ -945,8 +949,7 @@ func rightSideView(root *TreeNode) []int {
}
```
-
-javascript代码:
+#### Javascript:
```javascript
var rightSideView = function(root) {
@@ -972,7 +975,7 @@ var rightSideView = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function rightSideView(root: TreeNode | null): number[] {
@@ -992,7 +995,7 @@ function rightSideView(root: TreeNode | null): number[] {
};
```
-Swift:
+#### Swift:
```swift
func rightSideView(_ root: TreeNode?) -> [Int] {
@@ -1017,7 +1020,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] {
}
```
-Scala:
+#### Scala:
```scala
// 199.二叉树的右视图
@@ -1043,7 +1046,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -1076,7 +1079,7 @@ impl Solution {
}
```
-# 637.二叉树的层平均值
+## 637.二叉树的层平均值
[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
@@ -1084,7 +1087,7 @@ impl Solution {

-思路:
+### 思路
本题就是层序遍历的时候把一层求个总和在取一个均值。
@@ -1115,7 +1118,9 @@ public:
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
class Solution:
@@ -1155,7 +1160,7 @@ class Solution:
return averages
```
-java:
+#### Java:
```java
// 637. 二叉树的层平均值
@@ -1197,7 +1202,7 @@ public class N0637 {
}
```
-go:
+#### Go:
```GO
/**
@@ -1235,7 +1240,7 @@ func averageOfLevels(root *TreeNode) []float64 {
}
```
-javascript代码:
+#### Javascript:
```javascript
var averageOfLevels = function(root) {
@@ -1262,7 +1267,7 @@ var averageOfLevels = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function averageOfLevels(root: TreeNode | null): number[] {
@@ -1286,7 +1291,7 @@ function averageOfLevels(root: TreeNode | null): number[] {
};
```
-Swift:
+#### Swift:
```swift
func averageOfLevels(_ root: TreeNode?) -> [Double] {
@@ -1313,7 +1318,7 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] {
}
```
-Scala:
+#### Scala:
```scala
// 637.二叉树的层平均值
@@ -1339,7 +1344,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -1372,7 +1377,7 @@ impl Solution {
}
```
-# 429.N叉树的层序遍历
+## 429.N叉树的层序遍历
[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
@@ -1390,8 +1395,7 @@ impl Solution {
[5,6]
]
-
-思路:
+### 思路
这道题依旧是模板题,只不过一个节点有多个孩子了
@@ -1423,7 +1427,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
"""
@@ -1475,7 +1481,7 @@ class Solution:
return result
```
-java:
+#### Java:
```java
// 429. N 叉树的层序遍历
@@ -1535,8 +1541,7 @@ public class N0429 {
}
```
-
-go:
+#### Go:
```GO
/**
@@ -1567,7 +1572,7 @@ func levelOrder(root *Node) [][]int {
}
```
-JavaScript代码:
+#### JavaScript:
```JavaScript
var levelOrder = function(root) {
@@ -1596,7 +1601,7 @@ var levelOrder = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function levelOrder(root: Node | null): number[][] {
@@ -1618,7 +1623,7 @@ function levelOrder(root: Node | null): number[][] {
};
```
-Swift:
+#### Swift:
```swift
func levelOrder(_ root: Node?) -> [[Int]] {
@@ -1643,7 +1648,7 @@ func levelOrder(_ root: Node?) -> [[Int]] {
}
```
-Scala:
+#### Scala:
```scala
// 429.N叉树的层序遍历
@@ -1672,7 +1677,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
pub struct Solution;
@@ -1720,7 +1725,7 @@ impl Solution {
}
```
-# 515.在每个树行中找最大值
+## 515.在每个树行中找最大值
[力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
@@ -1728,7 +1733,7 @@ impl Solution {

-思路:
+### 思路
层序遍历,取每一层的最大值
@@ -1758,7 +1763,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
# Definition for a binary tree node.
@@ -1794,7 +1801,7 @@ class Solution:
return result
```
-java代码:
+#### Java:
```java
class Solution {
@@ -1820,7 +1827,7 @@ class Solution {
}
```
-go:
+#### Go:
```GO
/**
@@ -1864,7 +1871,7 @@ func max(x, y int) int {
}
```
-javascript代码:
+#### Javascript:
```javascript
var largestValues = function(root) {
@@ -1890,7 +1897,7 @@ var largestValues = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function largestValues(root: TreeNode | null): number[] {
@@ -1916,7 +1923,7 @@ function largestValues(root: TreeNode | null): number[] {
};
```
-Swift:
+#### Swift:
```swift
func largestValues(_ root: TreeNode?) -> [Int] {
@@ -1943,7 +1950,7 @@ func largestValues(_ root: TreeNode?) -> [Int] {
}
```
-Scala:
+#### Scala:
```scala
// 515.在每个树行中找最大值
@@ -1970,7 +1977,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -2002,7 +2009,7 @@ impl Solution {
}
```
-# 116.填充每个节点的下一个右侧节点指针
+## 116.填充每个节点的下一个右侧节点指针
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
@@ -2024,7 +2031,7 @@ struct Node {

-思路:
+### 思路
本题依然是层序遍历,只不过在单层遍历的时候记录一下本层的头部节点,然后在遍历的时候让前一个节点指向本节点就可以了
@@ -2063,7 +2070,9 @@ public:
};
```
-java代码:
+### 其他语言版本
+
+#### Java:
```java
class Solution {
@@ -2093,7 +2102,7 @@ class Solution {
}
```
-python代码:
+#### Python:
```python
"""
@@ -2133,7 +2142,7 @@ class Solution:
return root
```
-go:
+#### Go:
```GO
/**
@@ -2173,7 +2182,7 @@ func connect(root *Node) *Node {
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -2209,7 +2218,7 @@ var connect = function(root) {
```
-TypeScript:
+#### TypeScript:
```typescript
function connect(root: Node | null): Node | null {
@@ -2234,7 +2243,7 @@ function connect(root: Node | null): Node | null {
};
```
-Swift:
+#### Swift:
```swift
func connect(_ root: Node?) -> Node? {
@@ -2266,7 +2275,7 @@ func connect(_ root: Node?) -> Node? {
}
```
-Scala:
+#### Scala:
```scala
// 116.填充每个节点的下一个右侧节点指针
@@ -2297,11 +2306,11 @@ object Solution {
}
```
-# 117.填充每个节点的下一个右侧节点指针II
+## 117.填充每个节点的下一个右侧节点指针II
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
-思路:
+### 思路
这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道
@@ -2339,7 +2348,9 @@ public:
};
```
-Java 代码:
+### 其他语言版本
+
+#### Java:
```java
// 二叉树之层次遍历
@@ -2377,7 +2388,7 @@ class Solution {
}
```
-python代码:
+#### Python:
```python
# 层序遍历解法
@@ -2420,7 +2431,7 @@ class Solution:
```
-go:
+#### Go:
```GO
/**
@@ -2459,7 +2470,7 @@ func connect(root *Node) *Node {
}
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -2494,7 +2505,7 @@ var connect = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function connect(root: Node | null): Node | null {
@@ -2519,7 +2530,7 @@ function connect(root: Node | null): Node | null {
};
```
-Swift:
+#### Swift:
```swift
func connect(_ root: Node?) -> Node? {
@@ -2551,7 +2562,7 @@ func connect(_ root: Node?) -> Node? {
}
```
-Scala:
+#### Scala:
```scala
// 117.填充每个节点的下一个右侧节点指针II
@@ -2582,7 +2593,7 @@ object Solution {
}
```
-# 104.二叉树的最大深度
+## 104.二叉树的最大深度
[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
@@ -2600,7 +2611,7 @@ object Solution {
返回它的最大深度 3 。
-思路:
+### 思路
使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
@@ -2635,7 +2646,9 @@ public:
};
```
-Java:
+### 其他语言版本
+
+#### Java:
```Java
class Solution {
@@ -2661,7 +2674,7 @@ class Solution {
}
```
-Python:
+#### Python:
```python 3
# Definition for a binary tree node.
@@ -2691,7 +2704,7 @@ class Solution:
```
-Go:
+#### Go:
```go
/**
@@ -2726,7 +2739,7 @@ func maxDepth(root *TreeNode) int {
}
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -2759,7 +2772,7 @@ var maxDepth = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function maxDepth(root: TreeNode | null): number {
@@ -2779,7 +2792,7 @@ function maxDepth(root: TreeNode | null): number {
};
```
-Swift:
+#### Swift:
```swift
func maxDepth(_ root: TreeNode?) -> Int {
@@ -2804,7 +2817,7 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
-Scala:
+#### Scala:
```scala
// 104.二叉树的最大深度
@@ -2829,7 +2842,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -2859,10 +2872,12 @@ impl Solution {
}
```
-# 111.二叉树的最小深度
+## 111.二叉树的最小深度
[力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
+### 思路
+
相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。
**需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点**
@@ -2895,7 +2910,9 @@ public:
};
```
-Java:
+### 其他语言版本
+
+#### Java:
```java
class Solution {
@@ -2925,9 +2942,7 @@ class Solution {
}
```
-
-
-Python 3:
+#### Python:
```python 3
# Definition for a binary tree node.
@@ -2960,7 +2975,7 @@ class Solution:
return depth
```
-Go:
+#### Go:
```go
/**
@@ -2999,7 +3014,7 @@ func minDepth(root *TreeNode) int {
}
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -3035,7 +3050,7 @@ var minDepth = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function minDepth(root: TreeNode | null): number {
@@ -3056,7 +3071,7 @@ function minDepth(root: TreeNode | null): number {
};
```
-Swift:
+#### Swift:
```swift
func minDepth(_ root: TreeNode?) -> Int {
@@ -3082,7 +3097,7 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
-Scala:
+#### Scala:
```scala
// 111.二叉树的最小深度
@@ -3108,7 +3123,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -3141,28 +3156,27 @@ impl Solution {
}
```
-# 总结
+## 总结
二叉树的层序遍历,**就是图论中的广度优先搜索在二叉树中的应用**,需要借助队列来实现(此时又发现队列的一个应用了)。
来吧,一口气打十个:
-* 102.二叉树的层序遍历
-* 107.二叉树的层次遍历II
-* 199.二叉树的右视图
-* 637.二叉树的层平均值
-* 429.N叉树的层序遍历
-* 515.在每个树行中找最大值
-* 116.填充每个节点的下一个右侧节点指针
-* 117.填充每个节点的下一个右侧节点指针II
-* 104.二叉树的最大深度
-* 111.二叉树的最小深度
+* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
+* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
+* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
+* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
+* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
+* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
+* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
+* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
**致敬叶师傅!**
-
-
+
From 1abe3506ea6108a14d32b3176bb44674212cd9b1 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Fri, 21 Jul 2023 14:19:58 +0800
Subject: [PATCH 076/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200226.=E7=BF=BB?=
=?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0226.翻转二叉树.md | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)
diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md
index b3aea9ed..11517783 100644
--- a/problems/0226.翻转二叉树.md
+++ b/problems/0226.翻转二叉树.md
@@ -16,7 +16,11 @@
这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈)
-# 题外话
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 题外话
这道题目是非常经典的题目,也是比较简单的题目(至少一看就会)。
@@ -24,9 +28,7 @@
如果做过这道题的同学也建议认真看完,相信一定有所收获!
-# 思路
-
-《代码随想录》算法视频公开课:[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+## 思路
我们之前介绍的都是各种方式遍历二叉树,这次要翻转了,感觉还是有点懵逼。
@@ -49,7 +51,7 @@
那么层序遍历可以不可以呢?**依然可以的!只要把每一个节点的左右孩子翻转一下的遍历方式都是可以的!**
-## 递归法
+### 递归法
对于二叉树的递归法的前中后序遍历,已经在[二叉树:前中后序递归遍历](https://programmercarl.com/二叉树的递归遍历.html)详细讲解了。
@@ -102,9 +104,9 @@ public:
};
```
-## 迭代法
+### 迭代法
-### 深度优先遍历
+#### 深度优先遍历
[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)中给出了前中后序迭代方式的写法,所以本题可以很轻松的写出如下迭代法的代码:
@@ -163,7 +165,7 @@ public:
如果上面这个代码看不懂,回顾一下文章[二叉树:前中后序迭代方式的统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。
-### 广度优先遍历
+#### 广度优先遍历
也就是层序遍历,层数遍历也是可以翻转这棵树的,因为层序遍历也可以把每个节点的左右孩子都翻转一遍,代码如下:
@@ -259,7 +261,7 @@ public:
## 其他语言版本
-### Java
+### Java:
```Java
//DFS递归
class Solution {
@@ -310,7 +312,7 @@ class Solution {
}
```
-### Python
+### Python:
递归法:前序遍历:
```python
@@ -466,7 +468,7 @@ class Solution:
```
-### Go
+### Go:
递归版本的前序遍历
```Go
@@ -575,7 +577,7 @@ func invertTree(root *TreeNode) *TreeNode {
}
```
-### JavaScript
+### JavaScript:
使用递归版本的前序遍历
```javascript
@@ -783,7 +785,7 @@ function invertTree(root: TreeNode | null): TreeNode | null {
};
```
-### C
+### C:
递归法
```c
@@ -961,7 +963,7 @@ object Solution {
}
```
-### rust
+### Rust:
```rust
impl Solution {
@@ -991,7 +993,7 @@ impl Solution {
}
```
-### C#
+### C#:
```csharp
//递归
@@ -1042,3 +1044,4 @@ public class Solution {
+
From 49a38691744fbce62c7735a722b917448ab3e4a6 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Fri, 21 Jul 2023 14:46:55 +0800
Subject: [PATCH 077/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200101.=E5=AF=B9?=
=?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0101.对称二叉树.md | 39 ++++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 18 deletions(-)
diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md
index 4418c62d..36b39740 100644
--- a/problems/0101.对称二叉树.md
+++ b/problems/0101.对称二叉树.md
@@ -13,9 +13,11 @@

-# 思路
+## 算法公开课
-《代码随想录》算法视频公开课:[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf), 相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
**首先想清楚,判断对称二叉树要比较的是哪两个节点,要比较的可不是左右节点!**
@@ -41,7 +43,7 @@
那么我们先来看看递归法的代码应该怎么写。
-## 递归法
+### 递归法
递归三部曲
@@ -159,13 +161,13 @@ public:
**所以建议大家做题的时候,一定要想清楚逻辑,每一步做什么。把题目所有情况想到位,相应的代码写出来之后,再去追求简洁代码的效果。**
-## 迭代法
+### 迭代法
这道题目我们也可以使用迭代法,但要注意,这里的迭代法可不是前中后序的迭代写法,因为本题的本质是判断两个树是否是相互翻转的,其实已经不是所谓二叉树遍历的前中后序的关系了。
这里我们可以使用队列来比较两个树(根节点的左右子树)是否相互翻转,(**注意这不是层序遍历**)
-### 使用队列
+#### 使用队列
通过队列来判断根节点的左子树和右子树的内侧和外侧是否相等,如动画所示:
@@ -207,7 +209,7 @@ public:
};
```
-### 使用栈
+#### 使用栈
细心的话,其实可以发现,这个迭代法,其实是把左右两个子树要比较的元素顺序放进一个容器,然后成对成对的取出来进行比较,那么其实使用栈也是可以的。
@@ -254,12 +256,12 @@ public:
这两道题目基本和本题是一样的,只要稍加修改就可以AC。
-* 100.相同的树
-* 572.另一个树的子树
+* [100.相同的树](https://leetcode.cn/problems/same-tree/)
+* [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/)
-# 其他语言版本
+## 其他语言版本
-Java
+### Java:
```Java
/**
@@ -364,7 +366,7 @@ Java
```
-Python
+### Python:
递归法:
```python
@@ -470,7 +472,8 @@ class Solution:
return True
```
-Go
+### Go:
+
```go
/**
* Definition for a binary tree node.
@@ -521,8 +524,7 @@ func isSymmetric(root *TreeNode) bool {
}
```
-
-JavaScript
+### JavaScript:
递归判断是否为对称二叉树:
```javascript
@@ -610,7 +612,7 @@ var isSymmetric = function(root) {
};
```
-TypeScript:
+### TypeScript:
> 递归法
@@ -679,7 +681,7 @@ function isSymmetric(root: TreeNode | null): boolean {
};
```
-Swift:
+### Swift:
> 递归
```swift
@@ -761,7 +763,7 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
}
```
-Scala
+### Scala:
> 递归:
```scala
@@ -835,7 +837,7 @@ object Solution {
}
```
-## Rust
+### Rust:
递归:
```rust
@@ -900,3 +902,4 @@ impl Solution {
+
From 4e77ae645077ade4306bbb1ef4219bc4e41c7d29 Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Fri, 21 Jul 2023 14:57:25 +0800
Subject: [PATCH 078/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200104.=E4=BA=8C?=
=?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?=
=?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0104.二叉树的最大深度.md | 83 +++++++++++++++++--------------
1 file changed, 45 insertions(+), 38 deletions(-)
diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md
index 7130867b..05044375 100644
--- a/problems/0104.二叉树的最大深度.md
+++ b/problems/0104.二叉树的最大深度.md
@@ -5,6 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+ # 104.二叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) @@ -23,17 +24,19 @@ 返回它的最大深度 3 。 -# 思路 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 看完本篇可以一起做了如下两道题目: -* 104.二叉树的最大深度 -* 559.n叉树的最大深度 - -《代码随想录》算法视频公开课:[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) +* [559.n叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) -## 递归法 +### 递归法 本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。 @@ -164,7 +167,7 @@ public: }; ``` -## 迭代法 +### 迭代法 使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。 @@ -202,10 +205,11 @@ public: }; ``` - 那么我们可以顺便解决一下n叉树的最大深度问题 -# 559.n叉树的最大深度 +## 相关题目推荐 + +### 559.n叉树的最大深度 [力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/) @@ -219,11 +223,11 @@ public: 我们应返回其最大深度,3。 -思路: +### 思路 依然可以提供递归法和迭代法,来解决这个问题,思路是和二叉树思路一样的,直接给出代码如下: -## 递归法 +#### 递归法 c++代码: @@ -240,7 +244,7 @@ public: } }; ``` -## 迭代法 +#### 迭代法 依然是层序遍历,代码如下: @@ -267,11 +271,11 @@ public: }; ``` -# 其他语言版本 +## 其他语言版本 -## java +### Java: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```java class solution { @@ -344,7 +348,8 @@ class solution { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 + ```java class Solution { /*递归法,后序遍历求root节点的高度*/ @@ -391,9 +396,9 @@ class solution { } ``` -## python +### Python : -### 104.二叉树的最大深度 +104.二叉树的最大深度 递归法: ```python @@ -448,7 +453,7 @@ class Solution: ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 递归法: ```python @@ -522,9 +527,10 @@ class Solution: return max_depth ``` +### Go: + +104.二叉树的最大深度 -## go -### 104.二叉树的最大深度 ```go /** * definition for a binary tree node. @@ -574,7 +580,7 @@ func maxdepth(root *treenode) int { ``` -### 559. n叉树的最大深度 +559. n叉树的最大深度 ```go func maxDepth(root *Node) int { @@ -598,9 +604,9 @@ func maxDepth(root *Node) int { } ``` -## javascript +### Javascript : -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```javascript var maxdepth = function(root) { @@ -649,7 +655,7 @@ var maxDepth = function(root) { }; ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 N叉树的最大深度 递归写法 ```js @@ -683,9 +689,9 @@ var maxDepth = function(root) { }; ``` -## TypeScript +### TypeScript: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```typescript // 后续遍历(自下而上) @@ -728,7 +734,7 @@ function maxDepth(root: TreeNode | null): number { }; ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 ```typescript // 后续遍历(自下而上) @@ -756,9 +762,9 @@ function maxDepth(root: TreeNode | null): number { ``` -## C +### C: -### 104.二叉树的最大深度 +104.二叉树的最大深度 二叉树最大深度递归 ```c @@ -814,9 +820,9 @@ int maxDepth(struct TreeNode* root){ } ``` -## Swift +### Swift: -### 104.二叉树的最大深度 +104.二叉树的最大深度 ```swift // 递归 - 后序 @@ -856,7 +862,7 @@ func maxDepth(_ root: TreeNode?) -> Int { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 ```swift // 递归 @@ -893,9 +899,10 @@ func maxDepth1(_ root: Node?) -> Int { } ``` -## Scala +### Scala: + +104.二叉树的最大深度 -### 104.二叉树的最大深度 递归法: ```scala object Solution { @@ -934,7 +941,7 @@ object Solution { } ``` -### 559.n叉树的最大深度 +559.n叉树的最大深度 递归法: ```scala @@ -972,8 +979,8 @@ object Solution { } ``` -## rust -### 0104.二叉树的最大深度 +### Rust: +0104.二叉树的最大深度 递归: ```rust From 600a42730c360df82e626ce91e27efc00342b310 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:03:31 +0800 Subject: [PATCH 079/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index a1fc8a93..61f9beb7 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -26,9 +26,11 @@ 返回它的最小深度 2. -# 思路 +## 算法公开课 -《代码随想录》算法视频公开课:[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频在看本篇题解,更有助于大家对本题的理解。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 看完了这篇[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),再来看看如何求最小深度。 @@ -52,7 +54,7 @@ 什么是叶子节点,左右孩子都为空的节点才是叶子节点! -## 递归法 +### 递归法 来来来,一起递归三部曲: @@ -199,7 +201,7 @@ public: }; ``` -## 迭代法 +### 迭代法 相对于[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),本题还可以使用层序遍历的方式来解决,思路是一样的。 @@ -237,10 +239,10 @@ public: ``` -# 其他语言版本 +## 其他语言版本 -## Java +### Java: ```Java class Solution { @@ -300,7 +302,7 @@ class Solution { } ``` -## Python +### Python : 递归法(版本一) @@ -400,9 +402,7 @@ class Solution: return depth ``` - - -## Go +### Go: ```go /** @@ -463,7 +463,7 @@ func minDepth(root *TreeNode) int { ``` -## JavaScript +### JavaScript: 递归法: @@ -509,7 +509,7 @@ var minDepth = function(root) { }; ``` -## TypeScript +### TypeScript: > 递归法 @@ -547,7 +547,7 @@ function minDepth(root: TreeNode | null): number { }; ``` -## Swift +### Swift: > 递归 ```Swift @@ -594,7 +594,7 @@ func minDepth(_ root: TreeNode?) -> Int { ``` -## Scala +### Scala: 递归法: ```scala @@ -633,7 +633,8 @@ object Solution { } ``` -rust: +### Rust: + ```rust impl Solution { // 递归 From 40f0e737046592e9b32075f7dad43b305158f90b Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:08:43 +0800 Subject: [PATCH 080/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0222.完全二叉树的节点个数.md | 41 ++++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 795a6f37..d54f9b85 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -29,14 +29,17 @@ * 0 <= Node.val <= 5 * 10^4 * 题目数据保证输入的树是 完全二叉树 +## 算法公开课 -# 思路 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + + +## 思路 -《代码随想录》算法视频公开课:[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频在看本篇题解,更有助于大家对本题的理解。 本篇给出按照普通二叉树的求法以及利用完全二叉树性质的求法。 -## 普通二叉树 +### 普通二叉树 首先按照普通二叉树的逻辑来求。 @@ -44,7 +47,7 @@ 递归遍历的顺序依然是后序(左右中)。 -### 递归 +#### 递归 如果对求二叉树深度还不熟悉的话,看这篇:[二叉树:看看这些树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)。 @@ -112,7 +115,7 @@ public: **网上基本都是这个精简的代码版本,其实不建议大家照着这个来写,代码确实精简,但隐藏了一些内容,连遍历的顺序都看不出来,所以初学者建议学习版本一的代码,稳稳的打基础**。 -### 迭代法 +#### 迭代 如果对求二叉树层序遍历还不熟悉的话,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。 @@ -142,7 +145,7 @@ public: * 时间复杂度:O(n) * 空间复杂度:O(n) -## 完全二叉树 +### 完全二叉树 以上方法都是按照普通二叉树来做的,对于完全二叉树特性不了解的同学可以看这篇 [关于二叉树,你该了解这些!](https://programmercarl.com/二叉树理论基础.html),这篇详细介绍了各种二叉树的特性。 @@ -249,9 +252,9 @@ public: * 时间复杂度:O(log n × log n) * 空间复杂度:O(log n) -# 其他语言版本 +## 其他语言版本 -## Java +### Java: ```java class Solution { // 通用递归解法 @@ -312,7 +315,7 @@ class Solution { } ``` -## Python +### Python: 递归法: ```python @@ -408,7 +411,7 @@ class Solution: # 利用完全二叉树特性 return 1+self.countNodes(root.left)+self.countNodes(root.right) ``` -## Go +### Go: 递归版本 @@ -488,9 +491,7 @@ func countNodes(root *TreeNode) int { } ``` - - -## JavaScript: +### JavaScript: 递归版本 ```javascript @@ -559,7 +560,7 @@ var countNodes = function(root) { }; ``` -## TypeScrpt: +### TypeScrpt: > 递归法 @@ -614,7 +615,7 @@ function countNodes(root: TreeNode | null): number { }; ``` -## C: +### C: 递归法 ```c @@ -690,7 +691,7 @@ int countNodes(struct TreeNode* root){ } ``` -## Swift: +### Swift: > 递归 ```swift @@ -758,7 +759,7 @@ func countNodes(_ root: TreeNode?) -> Int { } ``` -## Scala +### Scala: 递归: ```scala @@ -821,9 +822,9 @@ object Solution { } ``` -rust: +### Rust: -// 递归 +递归 ```rust use std::cell::RefCell; use std::rc::Rc; @@ -838,7 +839,7 @@ impl Solution { } ``` -// 迭代 +迭代 ```rust use std::rc::Rc; use std::cell::RefCell; From ff6ff4adb86b80bc748c34f5f1c679f08d9c1ec2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 15:12:28 +0800 Subject: [PATCH 081/122] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index e10a612a..c7df9c8f 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -33,8 +33,9 @@ 返回 false 。 +## 算法公开课 -**《代码随想录》算法视频公开课:[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 题外话 @@ -357,7 +358,7 @@ public: ## 其他语言版本 -### Java +### Java: ```Java class Solution { @@ -498,7 +499,7 @@ class Solution { } ``` -### Python +### Python: 递归法: @@ -620,7 +621,7 @@ class Solution: height_map[real_node] = 1 + max(left, right) return True ``` -### Go +### Go: ```Go func isBalanced(root *TreeNode) bool { @@ -652,7 +653,7 @@ func max(a, b int) int { } ``` -### JavaScript +### JavaScript: 递归法: @@ -723,7 +724,7 @@ var isBalanced = function (root) { }; ``` -### TypeScript +### TypeScript: ```typescript // 递归法 @@ -741,7 +742,7 @@ function isBalanced(root: TreeNode | null): boolean { }; ``` -### C +### C: 递归法: @@ -876,7 +877,7 @@ func getHeight(_ root: TreeNode?) -> Int { } ``` -### rust +### Rust: 递归 @@ -912,3 +913,4 @@ impl Solution {
+
From 95b5bb195d5af819f553cafaede57fd4c990a96d Mon Sep 17 00:00:00 2001
From: jinbudaily <18336218010@163.com>
Date: Fri, 21 Jul 2023 15:18:37 +0800
Subject: [PATCH 082/122] =?UTF-8?q?0257.=E4=BA=8C=E5=8F=89=E6=A0=91?=
=?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84=20=E6=8E=92?=
=?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
problems/0257.二叉树的所有路径.md | 43 ++++++++++++++-----------------
1 file changed, 20 insertions(+), 23 deletions(-)
diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md
index 06153507..44c0fd85 100644
--- a/problems/0257.二叉树的所有路径.md
+++ b/problems/0257.二叉树的所有路径.md
@@ -18,9 +18,11 @@
示例:

-# 思路
+## 算法公开课
-**《代码随想录》算法视频公开课:[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。
@@ -32,7 +34,7 @@
我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。**
-## 递归
+### 递归
1. 递归函数参数以及返回值
@@ -305,7 +307,7 @@ public:
**综合以上,第二种递归的代码虽然精简但把很多重要的点隐藏在了代码细节里,第一种递归写法虽然代码多一些,但是把每一个逻辑处理都完整的展现出来了。**
-## 拓展
+### 拓展
这里讲解本题解的写法逻辑以及一些更具体的细节,下面的讲解中,涉及到C++语法特性,如果不是C++的录友,就可以不看了,避免越看越晕。
@@ -328,7 +330,7 @@ public:
所以,第一个代码版本中,我才使用 vector 类型的path,这样方便给大家演示代码中回溯的操作。 vector类型的path,不管 每次 路径收集的数字是几位数,总之一定是int,所以就一次 pop_back就可以。
-## 迭代法
+### 迭代法
至于非递归的方式,我们可以依然可以使用前序遍历的迭代方式来模拟遍历路径的过程,对该迭代方式不了解的同学,可以看文章[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。
@@ -368,7 +370,7 @@ public:
```
当然,使用java的同学,可以直接定义一个成员变量为object的栈`Stack