Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 0106.从中序与后序遍历序列构造二叉树.md #2919

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -69,13 +69,14 @@

## 前序

* [「代码随想录」学习社区](https://programmercarl.com/other/kstar.html)
* [做项目(多个C++、Java、Go、前端、测开项目)](https://programmercarl.com/other/kstar.html)


* 编程语言
* [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP)
* [编程语言基础课](https://kamacoder.com/courseshop.php)
* [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern)
* [大厂算法笔试题](https://kamacoder.com/company.php)

* 工具
* [一站式vim配置](https://github.com/youngyangyang04/PowerVim)
@@ -384,10 +385,10 @@
8. [图论:孤岛的总面积](./problems/kamacoder/0101.孤岛的总面积.md)
9. [图论:沉没孤岛](./problems/kamacoder/0102.沉没孤岛.md)
10. [图论:水流问题](./problems/kamacoder/0103.水流问题.md)
11. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md)
12. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md)
13. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md)
14. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md)
11. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.md)
12. [图论:建造最大岛屿](./problems/kamacoder/0104.建造最大岛屿.md)
13. [图论:字符串接龙](./problems/kamacoder/0110.字符串接龙.md)
14. [图论:有向图的完全可达性](./problems/kamacoder/0105.有向图的完全可达性.md)
15. [图论:并查集理论基础](./problems/kamacoder/图论并查集理论基础.md)
16. [图论:寻找存在的路径](./problems/kamacoder/0107.寻找存在的路径.md)
17. [图论:冗余连接](./problems/kamacoder/0108.冗余连接.md)
16 changes: 5 additions & 11 deletions problems/0001.两数之和.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 1. 两数之和
@@ -85,10 +83,10 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候,

过程如下:

![过程一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220711202638.png)
![过程一](images/0001.两数之和-01.png)


![过程二](https://code-thinking-1253855093.file.myqcloud.com/pics/20230220223536.png)
![过程二](images/0001.两数之和-02.png)

C++代码:

@@ -557,7 +555,3 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
16 changes: 5 additions & 11 deletions problems/0005.最长回文子串.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



@@ -108,7 +106,7 @@ dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹

dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图:

![647.回文子串](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171032473.jpg)
![647.回文子串](images/0005.最长回文子串-01.jpg)

如果这矩阵是从上到下,从左到右遍历,那么会用到没有计算过的dp[i + 1][j - 1],也就是根据不确定是不是回文的区间[i+1,j-1],来判断了[i,j]是不是回文,那结果一定是不对的。

@@ -142,7 +140,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序

举例,输入:"aaa",dp[i][j]状态如下:

![647.回文子串1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210121171059951.jpg)
![647.回文子串1](images/0005.最长回文子串-02.jpg)

**注意因为dp[i][j]的定义,所以j一定是大于等于i的,那么在填充dp[i][j]的时候一定是只填充右上半部分**。

@@ -731,8 +729,4 @@ public class Solution {
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

14 changes: 4 additions & 10 deletions problems/0015.三数之和.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



@@ -102,7 +100,7 @@ public:

动画效果如下:

![15.三数之和](https://code-thinking.cdn.bcebos.com/gifs/15.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.gif)
![15.三数之和](images/0015.三数之和-01.gif)

拿这个nums数组来举例,首先将数组排序,然后有一层for循环,i从下标0的地方开始,同时定一个下标left 定义在i+1的位置上,定义下标right 在数组结尾的位置上。

@@ -980,7 +978,3 @@ object Solution {
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
16 changes: 5 additions & 11 deletions problems/0017.电话号码的字母组合.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 17.电话号码的字母组合
@@ -13,7 +11,7 @@

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

![17.电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/2020102916424043.png)
![17.电话号码的字母组合](images/0017.电话号码的字母组合-01.png)

示例:
* 输入:"23"
@@ -66,7 +64,7 @@ const string letterMap[10] = {

例如:输入:"23",抽象为树形结构,如图所示:

![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123200304469.png)
![17. 电话号码的字母组合](images/0017.电话号码的字母组合-02.png)

图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。

@@ -766,7 +764,3 @@ public class Solution
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
12 changes: 3 additions & 9 deletions problems/0018.四数之和.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


> 一样的道理,能解决四数之和
@@ -798,8 +796,4 @@ def four_sum(nums, target)
end
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

22 changes: 8 additions & 14 deletions problems/0019.删除链表的倒数第N个节点.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



@@ -18,7 +16,7 @@
示例 1:


![19.删除链表的倒数第N个节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20210510085957392.png)
![19.删除链表的倒数第N个节点](images/0019.删除链表的倒数第N个节点-01.png)

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
@@ -51,16 +49,16 @@

* 定义fast指针和slow指针,初始值为虚拟头结点,如图:

<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.png' width=600> </img></div>
<img src='images/0019.删除链表的倒数第N个节点-02.png' width=600> </img></div>

* fast首先走n + 1步 ,为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作),如图:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B91.png' width=600> </img></div>
<img src='images/0019.删除链表的倒数第N个节点-03.png' width=600> </img></div>

* fast和slow同时移动,直到fast指向末尾,如题:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B92.png' width=600> </img></div>
<img src='images/0019.删除链表的倒数第N个节点-04.png' width=600> </img></div>
//图片中有错别词:应该将“只到”改为“直到”
* 删除slow指向的下一个节点,如图:
<img src='https://code-thinking.cdn.bcebos.com/pics/19.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B93.png' width=600> </img></div>
<img src='images/0019.删除链表的倒数第N个节点-05.png' width=600> </img></div>

此时不难写出如下C++代码:

@@ -479,7 +477,3 @@ public class Solution {
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
20 changes: 7 additions & 13 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)



@@ -83,21 +81,21 @@ cd a/b/c/../../


1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。
![括号匹配1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020080915505387.png)
![括号匹配1](images/0020.有效的括号-01.png)

2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。
![括号匹配2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155107397.png)
![括号匹配2](images/0020.有效的括号-02.png)

3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。
![括号匹配3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155115779.png)
![括号匹配3](images/0020.有效的括号-03.png)



我们的代码只要覆盖了这三种不匹配的情况,就不会出问题,可以看出 动手之前分析好题目的重要性。

动画如下:

![20.有效括号](https://code-thinking.cdn.bcebos.com/gifs/20.有效括号.gif)
![20.有效括号](images/0020.有效的括号-04.gif)


第一种情况:已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false
@@ -574,8 +572,4 @@ impl Solution {
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

22 changes: 8 additions & 14 deletions problems/0024.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 24. 两两交换链表中的节点
@@ -14,7 +12,7 @@
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。


<img src='https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9-%E9%A2%98%E6%84%8F.jpg' width=600 alt='24.两两交换链表中的节点-题意'> </img></div>
<img src='images/0024.两两交换链表中的节点-01.jpg' width=600 alt='24.两两交换链表中的节点-题意'> </img></div>

## 算法公开课

@@ -33,16 +31,16 @@

初始时,cur指向虚拟头结点,然后进行如下三步:

![24.两两交换链表中的节点1](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B91.png)
![24.两两交换链表中的节点1](images/0024.两两交换链表中的节点-02.png)

操作之后,链表如下:

![24.两两交换链表中的节点2](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B92.png)
![24.两两交换链表中的节点2](images/0024.两两交换链表中的节点-03.png)

看这个可能就更直观一些了:


![24.两两交换链表中的节点3](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B93.png)
![24.两两交换链表中的节点3](images/0024.两两交换链表中的节点-04.png)

对应的C++代码实现如下: (注释中详细和如上图中的三步做对应)

@@ -83,7 +81,7 @@ public:

心想应该没有更好的方法了吧,也就 $O(n)$ 的时间复杂度,重复提交几次,这样了:

![24.两两交换链表中的节点](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png)
![24.两两交换链表中的节点](images/0024.两两交换链表中的节点-05.png)

力扣上的统计如果两份代码是 100ms 和 300ms的耗时,其实是需要注意的。

@@ -527,7 +525,3 @@ public ListNode SwapPairs(ListNode head)
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
16 changes: 5 additions & 11 deletions problems/0027.移除元素.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
* [做项目(多个C++、Java、Go、测开、前端项目)](https://www.programmercarl.com/other/kstar.html)
* [刷算法(两个月高强度学算法)](https://www.programmercarl.com/xunlian/xunlianying.html)
* [背八股(40天挑战高频面试题)](https://www.programmercarl.com/xunlian/bagu.html)


# 27. 移除元素
@@ -45,7 +43,7 @@

删除过程如下:

![27.移除元素-暴力解法](https://code-thinking.cdn.bcebos.com/gifs/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E6%9A%B4%E5%8A%9B%E8%A7%A3%E6%B3%95.gif)
![27.移除元素-暴力解法](images/0027.移除元素-01.gif)

很明显暴力解法的时间复杂度是O(n^2),这道题目暴力解法在leetcode上是可以过的。

@@ -89,7 +87,7 @@ public:

删除过程如下:

![27.移除元素-双指针法](https://code-thinking.cdn.bcebos.com/gifs/27.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0-%E5%8F%8C%E6%8C%87%E9%92%88%E6%B3%95.gif)
![27.移除元素-双指针法](images/0027.移除元素-02.gif)

很多同学不了解

@@ -519,7 +517,3 @@ int removeElement(List<int> nums, int val) {

```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
Loading
Oops, something went wrong.