Merge branch 'youngyangyang04:master' into master

This commit is contained in:
AsaoOoo0o0o
2025-04-21 16:57:51 +08:00
committed by GitHub
289 changed files with 2139 additions and 3366 deletions

View File

@@ -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)
@@ -180,7 +181,7 @@
题目分类大纲如下:
<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20240424172231.png' width=600 alt='二叉树大纲'> </img></div>
<img src='https://file.kamacoder.com/pics/20240424172231.png' width=600 alt='二叉树大纲'> </img></div>
1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md)
2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md)
@@ -221,7 +222,7 @@
题目分类大纲如下:
<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20240424172311.png' width=600 alt='回溯算法大纲'> </img></div>
<img src='https://file.kamacoder.com/pics/20240424172311.png' width=600 alt='回溯算法大纲'> </img></div>
1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md)
2. [回溯算法77.组合](./problems/0077.组合.md)
@@ -251,7 +252,7 @@
题目分类大纲如下:
<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20210917104315.png' width=600 alt='贪心算法大纲'> </img></div>
<img src='https://file.kamacoder.com/pics/20210917104315.png' width=600 alt='贪心算法大纲'> </img></div>
1. [关于贪心算法,你该了解这些!](./problems/贪心算法理论基础.md)
2. [贪心算法455.分发饼干](./problems/0455.分发饼干.md)
@@ -385,9 +386,9 @@
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)
12. [图论:岛屿的周长](./problems/kamacoder/0106.岛屿的周长.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)
@@ -502,5 +503,5 @@
添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦**
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/第二企业刷题活码.png" data-img="1" width="200" height="200"></img></div>
<div align="center"><img src="https://file.kamacoder.com/pics/第二企业刷题活码.png" data-img="1" width="200" height="200"></img></div>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 333 KiB

View File

@@ -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)
![过程一](https://file.kamacoder.com/pics/20220711202638.png)
![过程二](https://code-thinking-1253855093.file.myqcloud.com/pics/20230220223536.png)
![过程二](https://file.kamacoder.com/pics/20230220223536.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>

View File

@@ -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.回文子串](https://file.kamacoder.com/pics/20210121171032473.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](https://file.kamacoder.com/pics/20210121171059951.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>

View File

@@ -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)
@@ -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>

View File

@@ -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.电话号码的字母组合](https://file.kamacoder.com/pics/2020102916424043.png)
示例:
* 输入:"23"
@@ -66,7 +64,7 @@ const string letterMap[10] = {
例如:输入:"23",抽象为树形结构,如图所示:
![17. 电话号码的字母组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123200304469.png)
![17. 电话号码的字母组合](https://file.kamacoder.com/pics/20201123200304469.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>

View File

@@ -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>

View File

@@ -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个节点](https://file.kamacoder.com/pics/20210510085957392.png)
输入head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
@@ -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>

View File

@@ -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,13 +81,13 @@ cd a/b/c/../../
1. 第一种情况,字符串里左方向的括号多余了 ,所以不匹配。
![括号匹配1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020080915505387.png)
![括号匹配1](https://file.kamacoder.com/pics/2020080915505387.png)
2. 第二种情况,括号没有多余,但是 括号的类型没有匹配上。
![括号匹配2](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155107397.png)
![括号匹配2](https://file.kamacoder.com/pics/20200809155107397.png)
3. 第三种情况,字符串里右方向的括号多余了,所以不匹配。
![括号匹配3](https://code-thinking-1253855093.file.myqcloud.com/pics/20200809155115779.png)
![括号匹配3](https://file.kamacoder.com/pics/20200809155115779.png)
@@ -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>

View File

@@ -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. 两两交换链表中的节点
@@ -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>

View File

@@ -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. 移除元素
@@ -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>

View File

@@ -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)
> 在一个串中查找是否出现过另一个串这是KMP的看家本领。
@@ -1520,7 +1518,3 @@ int strStr(char* haystack, char* needle) {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -269,7 +267,3 @@ var nextPermutation = function(nums) {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 34. 在排序数组中查找元素的第一个和最后一个位置
@@ -855,7 +853,3 @@ int* searchRange(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>

View File

@@ -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)
@@ -43,7 +41,7 @@
这道题目,要在数组中插入目标值,无非是这四种情况。
![35_搜索插入位置3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232148471.png)
![35_搜索插入位置3](https://file.kamacoder.com/pics/20201216232148471.png)
* 目标值在数组所有元素之前
* 目标值等于数组中某一个元素
@@ -84,14 +82,14 @@ public:
效率如下:
![35_搜索插入位置](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232127268.png)
![35_搜索插入位置](https://file.kamacoder.com/pics/20201216232127268.png)
### 二分法
既然暴力解法的时间复杂度是O(n),就要尝试一下使用二分查找法。
![35_搜索插入位置4](https://code-thinking-1253855093.file.myqcloud.com/pics/202012162326354.png)
![35_搜索插入位置4](https://file.kamacoder.com/pics/202012162326354.png)
大家注意这道题目的前提是数组是有序数组,这也是使用二分查找的基础条件。
@@ -101,7 +99,7 @@ public:
大体讲解一下二分法的思路这里来举一个例子例如在这个数组中使用二分法寻找元素为5的位置并返回其下标。
![35_搜索插入位置5](https://code-thinking-1253855093.file.myqcloud.com/pics/20201216232659199.png)
![35_搜索插入位置5](https://file.kamacoder.com/pics/20201216232659199.png)
二分查找涉及的很多的边界条件,逻辑比较简单,就是写不好。
@@ -152,7 +150,7 @@ public:
* 空间复杂度O(1)
效率如下:
![35_搜索插入位置2](https://code-thinking-1253855093.file.myqcloud.com/pics/2020121623272877.png)
![35_搜索插入位置2](https://file.kamacoder.com/pics/2020121623272877.png)
### 二分法第二种写法
@@ -549,7 +547,3 @@ int searchInsert(int* nums, int numsSize, int target){
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -20,11 +18,11 @@
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
空白格用 '.' 表示。
![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/202011171912586.png)
![解数独](https://file.kamacoder.com/pics/202011171912586.png)
一个数独。
![解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117191340669.png)
![解数独](https://file.kamacoder.com/pics/20201117191340669.png)
答案被标成红色。
@@ -54,7 +52,7 @@
因为这个树形结构太大了,我抽取一部分,如图所示:
![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131816104.png)
![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131816104.png)
### 回溯三部曲
@@ -85,7 +83,7 @@ bool backtracking(vector<vector<char>>& board)
* 递归单层搜索逻辑
![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131822254.png)
![37.解数独](https://file.kamacoder.com/pics/2020111720451790-20230310131822254.png)
在树形图中可以看出我们需要的是一个二维的递归 (一行一列)
@@ -893,7 +891,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -52,7 +50,7 @@ candidates 中的数字可以无限制重复被选取。
本题搜索的过程抽象成树形结构如下:
![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367.png)
![39.组合总和](https://file.kamacoder.com/pics/20201223170730367.png)
注意图中叶子节点的返回条件因为本题没有组合数量要求仅仅是总和的限制所以递归没有层数的限制只要选取的元素总和超过target就返回
而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层因为要取k个元素的组合。
@@ -87,7 +85,7 @@ void backtracking(vector<int>& candidates, int target, int sum, int startIndex)
在如下树形结构中:
![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135337214.png)
![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135337214.png)
从叶子节点可以清晰看到终止只有两种情况sum大于target和sum等于target。
@@ -160,7 +158,7 @@ public:
在这个树形结构中:
![39.组合总和](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170730367-20230310135342472.png)
![39.组合总和](https://file.kamacoder.com/pics/20201223170730367-20230310135342472.png)
以及上面的版本一的代码大家可以看到对于sum已经大于target的情况其实是依然进入了下一层递归只是下一层递归结束判断的时候会判断sum > target的话就返回。
@@ -173,7 +171,7 @@ public:
如图:
![39.组合总和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223170809182.png)
![39.组合总和1](https://file.kamacoder.com/pics/20201223170809182.png)
for循环剪枝代码如下
@@ -661,7 +659,3 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -1,12 +1,8 @@
<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)
> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了!
# 40.组合总和II
[力扣题目链接](https://leetcode.cn/problems/combination-sum-ii/)
@@ -80,7 +76,7 @@ candidates 中的每个数字在每个组合中只能使用一次。
选择过程树形结构如图所示:
![40.组合总和II](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000918.png)
![40.组合总和II](https://file.kamacoder.com/pics/20230310000918.png)
可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组这个used数组下面会重点介绍。
@@ -130,7 +126,7 @@ if (sum == target) {
这块比较抽象,如图:
![40.组合总和II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000954.png)
![40.组合总和II1](https://file.kamacoder.com/pics/20230310000954.png)
我在图中将used的变化用橘黄色标注上可以看出在candidates[i] == candidates[i - 1]相同的情况下:
@@ -141,7 +137,7 @@ if (sum == target) {
而 used[i - 1] == true说明是进入下一层递归去下一个数所以是树枝上如图所示
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221021163812.png)
![](https://file.kamacoder.com/pics/20221021163812.png)
**这块去重的逻辑很抽象,网上搜的题解基本没有能讲清楚的,如果大家之前思考过这个问题或者刷过这道题目,看到这里一定会感觉通透了很多!**
@@ -807,7 +803,3 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -1,13 +1,10 @@
<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)
> 这个图就是大厂面试经典题目,接雨水! 最常青藤的一道题,面试官百出不厌!
# 42. 接雨水
@@ -50,10 +47,10 @@
首先要明确,要按照行来计算,还是按照列来计算。
按照行来计算如图:
![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091118927.png)
![42.接雨水2](https://file.kamacoder.com/pics/20210402091118927.png)
按照列来计算如图:
![42.接雨水1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210402091208445.png)
![42.接雨水1](https://file.kamacoder.com/pics/20210402091208445.png)
一些同学在实现的时候,很容易一会按照行来计算一会按照列来计算,这样就会越写越乱。
@@ -65,7 +62,7 @@
这句话可以有点绕来举一个理解例如求列4的雨水高度如图
![42.接雨水3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092732301.png)
![42.接雨水3](https://file.kamacoder.com/pics/20210223092732301.png)
列4 左侧最高的柱子是列3高度为2以下用lHeight表示
@@ -204,7 +201,7 @@ public:
1. 首先单调栈是按照行方向来计算雨水,如图:
![42.接雨水2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223092629946.png)
![42.接雨水2](https://file.kamacoder.com/pics/20210223092629946.png)
知道这一点,后面的就可以理解了。
@@ -218,7 +215,7 @@ public:
如图:
![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229.png)
![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229.png)
关于单调栈的顺序给大家一个总结: [739. 每日温度](https://programmercarl.com/0739.每日温度.html) 中求一个元素右边第一个更大元素,单调栈就是递增的,[84.柱状图中最大的矩形](https://programmercarl.com/0084.柱状图中最大的矩形.html)求一个元素右边第一个更小元素,单调栈就是递减的。
@@ -232,7 +229,7 @@ public:
如图所示:
![42.接雨水5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210223094619398.png)
![42.接雨水5](https://file.kamacoder.com/pics/20210223094619398.png)
4. 栈里要保存什么数值
@@ -287,7 +284,7 @@ if (height[i] == height[st.top()]) { // 例如 5 5 1 7 这种情况
如果当前遍历的元素(柱子)高度大于栈顶元素的高度,此时就出现凹槽了,如图所示:
![42.接雨水4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021022309321229-20230310123027977.png)
![42.接雨水4](https://file.kamacoder.com/pics/2021022309321229-20230310123027977.png)
取栈顶元素将栈顶元素弹出这个就是凹槽的底部也就是中间位置下标记为mid对应的高度为height[mid]就是图中的高度1
@@ -1095,7 +1092,3 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心理准备!
@@ -49,7 +47,7 @@
如图:
![45.跳跃游戏II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232309103.png)
![45.跳跃游戏II](https://file.kamacoder.com/pics/20201201232309103.png)
**图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)**
@@ -101,11 +99,11 @@ public:
因为当移动下标指向 nums.size - 2 时:
- 如果移动下标等于当前覆盖最大距离下标, 需要再走一步(即 ans++),因为最后一步一定是可以到的终点。(题目假设总是可以到达数组的最后一个位置),如图:
![45.跳跃游戏II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232445286.png)
![45.跳跃游戏II2](https://file.kamacoder.com/pics/20201201232445286.png)
- 如果移动下标不等于当前覆盖最大距离下标,说明当前覆盖最远距离就可以直接达到终点了,不需要再走一步。如图:
![45.跳跃游戏II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201201232338693.png)
![45.跳跃游戏II1](https://file.kamacoder.com/pics/20201201232338693.png)
代码如下:
@@ -542,7 +540,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 46.全排列
@@ -43,7 +41,7 @@
我以[1,2,3]为例,抽象成树形结构如下:
![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png)
![全排列](https://file.kamacoder.com/pics/20240803180318.png)
### 回溯三部曲
@@ -55,7 +53,7 @@
但排列问题需要一个used数组标记已经选择的元素如图橘黄色部分所示:
![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png)
![全排列](https://file.kamacoder.com/pics/20240803180318.png)
代码如下:
@@ -67,7 +65,7 @@ void backtracking (vector<int>& nums, vector<bool>& used)
* 递归终止条件
![全排列](https://code-thinking-1253855093.file.myqcloud.com/pics/20240803180318.png)
![全排列](https://file.kamacoder.com/pics/20240803180318.png)
可以看出叶子节点,就是收割结果的地方。
@@ -521,8 +519,4 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -50,7 +48,7 @@
我以示例中的 [1,1,2]为例 (为了方便举例,已经排序)抽象为一棵树,去重过程如图:
![47.全排列II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201331223.png)
![47.全排列II1](https://file.kamacoder.com/pics/20201124201331223.png)
图中我们对同一树层前一位也就是nums[i-1])如果使用过,那么就进行去重。
@@ -132,11 +130,11 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) {
树层上去重(used[i - 1] == false),的树形结构如下:
![47.全排列II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201406192.png)
![47.全排列II2](https://file.kamacoder.com/pics/20201124201406192.png)
树枝上去重used[i - 1] == true的树型结构如下
![47.全排列II3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124201431571.png)
![47.全排列II3](https://file.kamacoder.com/pics/20201124201431571.png)
大家应该很清晰的看到,树层上对前一位去重非常彻底,效率很高,树枝上对前一位去重虽然最后可以得到答案,但是做了很多无用搜索。
@@ -555,7 +553,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 51. N皇后
@@ -17,7 +15,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,
示例 1
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211020232201.png)
![](https://file.kamacoder.com/pics/20211020232201.png)
* 输入n = 4
* 输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
@@ -47,7 +45,7 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,
下面我用一个 3 * 3 的棋盘,将搜索过程抽象为一棵树,如图:
![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303.jpg)
![51.N皇后](https://file.kamacoder.com/pics/20210130182532303.jpg)
从图中,可以看出,二维矩阵中矩阵的高就是这棵树的高度,矩阵的宽就是树形结构中每一个节点的宽度。
@@ -87,7 +85,7 @@ void backtracking(int n, int row, vector<string>& chessboard) {
* 递归终止条件
在如下树形结构中:
![51.N皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130182532303-20230310122134167.jpg)
![51.N皇后](https://file.kamacoder.com/pics/20210130182532303-20230310122134167.jpg)
可以看出,当递归到棋盘最底层(也就是叶子节点)的时候,就可以收集结果并返回了。
@@ -921,7 +919,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -15,7 +13,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
上图为 8 皇后问题的一种解法。
![51n皇后](https://code-thinking-1253855093.file.myqcloud.com/pics/20200821152118456.png)
![51n皇后](https://file.kamacoder.com/pics/20200821152118456.png)
给定一个整数 n返回 n 皇后不同的解决方案的数量。
@@ -307,7 +305,3 @@ class Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 53. 最大子序和
@@ -492,7 +490,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 53. 最大子序和
@@ -56,7 +54,7 @@ dp[0]应该是多少呢?
5. 举例推导dp数组
以示例一为例输入nums = [-2,1,-3,4,-1,2,1,-5,4]对应的dp状态如下
![53.最大子序和(动态规划)](https://code-thinking-1253855093.file.myqcloud.com/pics/20210303104129101.png)
![53.最大子序和(动态规划)](https://file.kamacoder.com/pics/20210303104129101.png)
**注意最后的结果可不是dp[nums.size() - 1]** 而是dp[6]。
@@ -244,7 +242,3 @@ function maxSubArray(nums: number[]): number {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -38,7 +36,7 @@
由外向内一圈一圈这么画下去,如下所示:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png)
![](https://file.kamacoder.com/pics/20220922102236.png)
这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。
@@ -485,7 +483,3 @@ func min(x, y int) int {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 55. 跳跃游戏
@@ -50,7 +48,7 @@
如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230203105634.png)
![](https://file.kamacoder.com/pics/20230203105634.png)
i 每次移动只能在 cover 的范围内移动每移动一个元素cover 得到该元素数值(新的覆盖范围)的补充,让 i 继续移动下去。
@@ -293,7 +291,3 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 56. 合并区间
@@ -40,7 +38,7 @@
这么说有点抽象,看图:(**注意图中区间都是按照左边界排序之后了**
![56.合并区间](https://code-thinking-1253855093.file.myqcloud.com/pics/20201223200632791.png)
![56.合并区间](https://file.kamacoder.com/pics/20201223200632791.png)
知道如何判断重复之后,剩下的就是合并了,如何去模拟合并区间呢?
@@ -405,7 +403,3 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -56,7 +54,7 @@
那么我按照左闭右开的原则,来画一圈,大家看一下:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922102236.png)
![](https://file.kamacoder.com/pics/20220922102236.png)
这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。
@@ -829,7 +827,3 @@ def generate_matrix(n)
end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110174033215.png)
![](https://file.kamacoder.com/pics/20210110174033215.png)
* 输入m = 3, n = 7
* 输出28
@@ -64,7 +62,7 @@
如图举例:
![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700.png)
![62.不同路径](https://file.kamacoder.com/pics/20201209113602700.png)
此时问题就可以转化为求二叉树叶子节点的个数,代码如下:
@@ -133,7 +131,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1;
如图所示:
![62.不同路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113631392.png)
![62.不同路径1](https://file.kamacoder.com/pics/20201209113631392.png)
以上动规五部曲分析完毕C++代码如下:
@@ -182,7 +180,7 @@ public:
在这个图中可以看出一共mn的话无论怎么走走到终点都需要 m + n - 2 步。
![62.不同路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113602700-20230310120944078.png)
![62.不同路径](https://file.kamacoder.com/pics/20201209113602700-20230310120944078.png)
在这m + n - 2 步中,一定有 m - 1 步是要向下走的,不用管什么时候向下走。
@@ -192,7 +190,7 @@ public:
那么答案,如图所示:
![62.不同路径2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201209113725324.png)
![62.不同路径2](https://file.kamacoder.com/pics/20201209113725324.png)
**求组合的时候要防止两个int相乘溢出** 所以不能把算式的分子都算出来,分母都算出来再做除法。
@@ -616,7 +614,3 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -16,13 +14,13 @@
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204901338.png)
![](https://file.kamacoder.com/pics/20210111204901338.png)
网格中的障碍物和空位置分别用 1 和 0 来表示。
示例 1
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111204939971.png)
![](https://file.kamacoder.com/pics/20210111204939971.png)
* 输入obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
* 输出2
@@ -34,7 +32,7 @@
示例 2
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210111205857918.png)
![](https://file.kamacoder.com/pics/20210111205857918.png)
* 输入obstacleGrid = [[0,1],[0,0]]
* 输出1
@@ -95,7 +93,7 @@ for (int j = 0; j < n; j++) dp[0][j] = 1;
如图:
![63.不同路径II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114513928.png)
![63.不同路径II](https://file.kamacoder.com/pics/20210104114513928.png)
下标(0, j)的初始化情况同理。
@@ -129,11 +127,11 @@ for (int i = 1; i < m; i++) {
拿示例1来举例如题
![63.不同路径II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114548983.png)
![63.不同路径II1](https://file.kamacoder.com/pics/20210104114548983.png)
对应的dp table 如图:
![63.不同路径II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210104114610256.png)
![63.不同路径II2](https://file.kamacoder.com/pics/20210104114610256.png)
如果这个图看不懂,建议再理解一下递归公式,然后照着文章中说的遍历顺序,自己推导一下!
@@ -781,7 +779,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 70. 爬楼梯
@@ -103,7 +101,7 @@ dp[i] 爬到第i层楼梯有dp[i]种方法
举例当n为5的时候dp tabledp数组应该是这样的
![70.爬楼梯](https://code-thinking-1253855093.file.myqcloud.com/pics/20210105202546299.png)
![70.爬楼梯](https://file.kamacoder.com/pics/20210105202546299.png)
如果代码出问题了就把dp table 打印出来,看看究竟是不是和自己推导的一样。
@@ -520,8 +518,4 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 70. 爬楼梯(进阶版)
@@ -252,7 +250,3 @@ var climbStairs = function (n: number): number {
### Rust:
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 72. 编辑距离
@@ -172,7 +170,7 @@ for (int j = 0; j <= word2.size(); j++) dp[0][j] = j;
可以看出dp[i][j]是依赖左方,上方和左上方元素的,如图:
![72.编辑距离](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162113131.jpg)
![72.编辑距离](https://file.kamacoder.com/pics/20210114162113131.jpg)
所以在dp矩阵中一定是从左到右从上到下去遍历。
@@ -196,7 +194,7 @@ for (int i = 1; i <= word1.size(); i++) {
以示例1为例输入`word1 = "horse", word2 = "ros"`为例dp矩阵状态图如下
![72.编辑距离1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210114162132300.jpg)
![72.编辑距离1](https://file.kamacoder.com/pics/20210114162132300.jpg)
以上动规五部分析完毕C++代码如下:
@@ -462,7 +460,3 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 第77题. 组合
@@ -84,7 +82,7 @@ for (int i = 1; i <= n; i++) {
那么我把组合问题抽象为如下树形结构:
![77.组合](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195223940.png)
![77.组合](https://file.kamacoder.com/pics/20201123195223940.png)
可以看出这棵树,一开始集合是 1234 从左向右取数,取过的数,不再重复取。
@@ -128,7 +126,7 @@ vector<int> path; // 用来存放符合条件结果
从下图中红线部分可以看出,在集合[1,2,3,4]取1之后下一层递归就要在[2,3,4]中取数了,那么下一层递归如何知道从[2,3,4]中取数呢靠的就是startIndex。
![77.组合2](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195328976.png)
![77.组合2](https://file.kamacoder.com/pics/20201123195328976.png)
所以需要startIndex来记录下一层递归搜索的起始位置。
@@ -148,7 +146,7 @@ path这个数组的大小如果达到k说明我们找到了一个子集大小
如图红色部分:
![77.组合3](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195407907.png)
![77.组合3](https://file.kamacoder.com/pics/20201123195407907.png)
此时用result二维数组把path保存起来并终止本层递归。
@@ -165,7 +163,7 @@ if (path.size() == k) {
回溯法的搜索过程就是一个树型结构的遍历过程在如下图中可以看出for循环用来横向遍历递归的过程是纵向遍历。
![77.组合1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195242899.png)
![77.组合1](https://file.kamacoder.com/pics/20201123195242899.png)
如此我们才遍历完图中的这棵树。
@@ -269,7 +267,7 @@ for (int i = startIndex; i <= n; i++) {
这么说有点抽象,如图所示:
![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207-20230310134409532.png)
![77.组合4](https://file.kamacoder.com/pics/20210130194335207-20230310134409532.png)
图中每一个节点图中为矩形就代表本层的一个for循环那么每一层的for循环从第二个数开始遍历的话都没有意义都是无效遍历。
@@ -876,7 +874,3 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -69,7 +67,7 @@ for (int i = startIndex; i <= n; i++) {
这么说有点抽象,如图所示:
![77.组合4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210130194335207.png)
![77.组合4](https://file.kamacoder.com/pics/20210130194335207.png)
图中每一个节点图中为矩形就代表本层的一个for循环那么每一层的for循环从第二个数开始遍历的话都没有意义都是无效遍历。
@@ -412,7 +410,3 @@ object Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 78.子集
@@ -492,7 +490,3 @@ public class Solution {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 84.柱状图中最大的矩形
@@ -13,9 +11,9 @@
求在该柱状图中,能够勾勒出来的矩形的最大面积。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210803220437.png)
![](https://file.kamacoder.com/pics/20210803220437.png)
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210803220506.png)
![](https://file.kamacoder.com/pics/20210803220506.png)
* 1 <= heights.length <=10^5
* 0 <= heights[i] <= 10^4
@@ -116,7 +114,7 @@ public:
我来举一个例子,如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221165730.png)
![](https://file.kamacoder.com/pics/20230221165730.png)
只有栈里从大到小的顺序,才能保证栈顶元素找到左右两边第一个小于栈顶元素的柱子。
@@ -181,7 +179,7 @@ public:
如果数组本身就是升序的,例如[2,4,6,8],那么入栈之后 都是单调递减,一直都没有走 情况三 计算结果的哪一步所以最后输出的就是0了。 如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221163936.png)
![](https://file.kamacoder.com/pics/20230221163936.png)
那么结尾加一个0就会让栈里的所有元素走到情况三的逻辑。
@@ -196,7 +194,7 @@ public:
之后又将6 加入栈此时8已经弹出了然后 就是 4 与 栈口元素 6 进行比较周而复始那么计算的最后结果result就是0。 如图所示:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230221164533.png)
![](https://file.kamacoder.com/pics/20230221164533.png)
所以我们需要在 height数组前后各加一个元素0。
@@ -863,7 +861,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 90.子集II
@@ -41,7 +39,7 @@
用示例中的[1, 2, 2] 来举例,如图所示: **注意去重需要先对集合排序**
![90.子集II](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124195411977.png)
![90.子集II](https://file.kamacoder.com/pics/20201124195411977.png)
从图中可以看出同一树层上重复取2 就要过滤掉同一树枝上就可以重复取2因为同一树枝上元素的集合才是唯一子集
@@ -697,7 +695,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -56,7 +54,7 @@
切割问题可以抽象为树型结构,如图:
![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933.png)
![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933.png)
### 回溯三部曲
@@ -108,7 +106,7 @@ if (pointNum == 3) { // 逗点数量为3时分隔结束
如果不合法就结束本层循环,如图中剪掉的分支:
![93.复原IP地址](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123203735933-20230310132314109.png)
![93.复原IP地址](https://file.kamacoder.com/pics/20201123203735933-20230310132314109.png)
然后就是递归和回溯的过程:
@@ -876,7 +874,3 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -14,7 +12,7 @@
示例:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210113161941835.png)
![](https://file.kamacoder.com/pics/20210113161941835.png)
## 算法公开课
@@ -29,11 +27,11 @@
了解了二叉搜索树之后,我们应该先举几个例子,画画图,看看有没有什么规律,如图:
![96.不同的二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093106367.png)
![96.不同的二叉搜索树](https://file.kamacoder.com/pics/20210107093106367.png)
n为1的时候有一棵树n为2有两棵树这个是很直观的。
![96.不同的二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093129889.png)
![96.不同的二叉搜索树1](https://file.kamacoder.com/pics/20210107093129889.png)
来看看n为3的时候有哪几种情况。
@@ -67,7 +65,7 @@ dp[3],就是 元素1为头结点搜索树的数量 + 元素2为头结点搜索
如图所示:
![96.不同的二叉搜索树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093226241.png)
![96.不同的二叉搜索树2](https://file.kamacoder.com/pics/20210107093226241.png)
此时我们已经找到递推关系了,那么可以用动规五部曲再系统分析一遍。
@@ -120,7 +118,7 @@ for (int i = 1; i <= n; i++) {
n为5时候的dp数组状态如图
![96.不同的二叉搜索树3](https://code-thinking-1253855093.file.myqcloud.com/pics/20210107093253987.png)
![96.不同的二叉搜索树3](https://file.kamacoder.com/pics/20210107093253987.png)
当然如果自己画图举例的话基本举例到n为3就可以了n为4的时候画图已经比较麻烦了。
@@ -348,7 +346,3 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 98.验证二叉搜索树
@@ -18,7 +16,7 @@
* 节点的右子树只包含大于当前节点的数。
* 所有左子树和右子树自身必须也是二叉搜索树。
![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png)
![98.验证二叉搜索树](https://file.kamacoder.com/pics/20230310000750.png)
## 算法公开课
@@ -104,7 +102,7 @@ if (root->val > root->left->val && root->val < root->right->val) {
例如: [10,5,15,null,null,6,20] 这个case
![二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000824.png)
![二叉搜索树](https://file.kamacoder.com/pics/20230310000824.png)
节点10大于左节点5小于右节点15但右子树里出现了一个6 这就不符合了!
@@ -807,7 +805,3 @@ public bool IsValidBST(TreeNode root)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -14,9 +12,9 @@
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726172932.png)
![](https://file.kamacoder.com/pics/20210726172932.png)
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726173011.png)
![](https://file.kamacoder.com/pics/20210726173011.png)
## 思路
@@ -340,7 +338,3 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 101. 对称二叉树
@@ -11,7 +9,7 @@
给定一个二叉树,检查它是否是镜像对称的。
![101. 对称二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144607387.png)
![101. 对称二叉树](https://file.kamacoder.com/pics/20210203144607387.png)
## 算法公开课
@@ -27,7 +25,7 @@
比较的是两个子树的里侧和外侧的元素是否相等。如图所示:
![101. 对称二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144624414.png)
![101. 对称二叉树1](https://file.kamacoder.com/pics/20210203144624414.png)
那么遍历的顺序应该是什么样的呢?
@@ -946,8 +944,4 @@ public bool IsSymmetric(TreeNode root)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -39,7 +37,7 @@
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
![102.二叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203144842988.png)
![102.二叉树的层序遍历](https://file.kamacoder.com/pics/20210203144842988.png)
### 思路
@@ -534,7 +532,7 @@ public IList<IList<int>> LevelOrder(TreeNode root)
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
![107.二叉树的层次遍历II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151058308.png)
![107.二叉树的层次遍历II](https://file.kamacoder.com/pics/20210203151058308.png)
### 思路
@@ -928,7 +926,7 @@ public IList<IList<int>> LevelOrderBottom(TreeNode root)
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
![199.二叉树的右视图](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151307377.png)
![199.二叉树的右视图](https://file.kamacoder.com/pics/20210203151307377.png)
### 思路
@@ -1278,7 +1276,7 @@ public class Solution
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。
![637.二叉树的层平均值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151350500.png)
![637.二叉树的层平均值](https://file.kamacoder.com/pics/20210203151350500.png)
### 思路
@@ -1636,7 +1634,7 @@ public class Solution {
例如,给定一个 3叉树 :
![429. N叉树的层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151439168.png)
![429. N叉树的层序遍历](https://file.kamacoder.com/pics/20210203151439168.png)
返回其层序遍历:
@@ -2008,7 +2006,7 @@ impl Solution {
您需要在二叉树的每一行中找到最大的值。
![515.在每个树行中找最大值](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203151532153.png)
![515.在每个树行中找最大值](https://file.kamacoder.com/pics/20210203151532153.png)
### 思路
@@ -2339,7 +2337,7 @@ struct Node {
初始状态下,所有 next 指针都被设置为 NULL。
![116.填充每个节点的下一个右侧节点指针](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203152044855.jpg)
![116.填充每个节点的下一个右侧节点指针](https://file.kamacoder.com/pics/20210203152044855.jpg)
### 思路
@@ -2973,7 +2971,7 @@ object Solution {
给定二叉树 [3,9,20,null,null,15,7]
![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310134849764.png)
![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310134849764.png)
返回它的最大深度 3 。
@@ -2983,7 +2981,7 @@ object Solution {
在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585-20230310134854803.png)
![层序遍历](https://file.kamacoder.com/pics/20200810193056585-20230310134854803.png)
所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。
@@ -3601,7 +3599,3 @@ impl Solution {
**致敬叶师傅!**
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -20,7 +18,7 @@
给定二叉树 [3,9,20,null,null,15,7]
![104. 二叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203153031914-20230310121809902.png)
![104. 二叉树的最大深度](https://file.kamacoder.com/pics/20210203153031914-20230310121809902.png)
返回它的最大深度 3 。
@@ -174,7 +172,7 @@ public:
在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,如图所示:
![层序遍历](https://code-thinking-1253855093.file.myqcloud.com/pics/20200810193056585.png)
![层序遍历](https://file.kamacoder.com/pics/20200810193056585.png)
所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。
@@ -219,7 +217,7 @@ public:
例如,给定一个 3叉树 :
![559.n叉树的最大深度](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315313214.png)
![559.n叉树的最大深度](https://file.kamacoder.com/pics/2021020315313214.png)
我们应返回其最大深度3。
@@ -1195,7 +1193,3 @@ public int MaxDepth(TreeNode root)
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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,7 +25,7 @@
* 后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png)
![106. 从中序与后序遍历序列构造二叉树1](https://file.kamacoder.com/pics/20210203154316774.png)
## 算法公开课
@@ -42,7 +40,7 @@
流程如图:
![106.从中序与后序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154249860.png)
![106.从中序与后序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154249860.png)
那么代码应该怎么写呢?
@@ -413,7 +411,7 @@ public:
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png)
![105. 从前序与中序遍历序列构造二叉树](https://file.kamacoder.com/pics/20210203154626672.png)
### 思路
@@ -556,7 +554,7 @@ public:
举一个例子:
![106.从中序与后序遍历序列构造二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154720326.png)
![106.从中序与后序遍历序列构造二叉树2](https://file.kamacoder.com/pics/20210203154720326.png)
tree1 的前序遍历是[1 2 3] 后序遍历是[3 2 1]。
@@ -1351,7 +1349,3 @@ public TreeNode BuildTree(int[] inorder, int[] postorder)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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 @@
示例:
![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png)
![108.将有序数组转换为二叉搜索树](https://file.kamacoder.com/pics/20201022164420763.png)
## 算法公开课
@@ -42,7 +40,7 @@
例如 有序数组[-10-3059] 就可以构造成这样的二叉搜索树,如图。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220930173553.png)
![](https://file.kamacoder.com/pics/20220930173553.png)
上图中,是符合二叉搜索树的特性吧,如果要这么做的话,是不是本题意义就不大了,所以才强调是平衡二叉搜索树。
@@ -562,7 +560,3 @@ public TreeNode Traversal(int[] nums, int left, int right)
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -21,7 +19,7 @@
给定二叉树 [3,9,20,null,null,15,7]
![110.平衡二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315542230.png)
![110.平衡二叉树](https://file.kamacoder.com/pics/2021020315542230.png)
返回 true 。
@@ -29,7 +27,7 @@
给定二叉树 [1,2,2,3,3,null,null,4,4]
![110.平衡二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155447919.png)
![110.平衡二叉树1](https://file.kamacoder.com/pics/20210203155447919.png)
返回 false 。
@@ -48,7 +46,7 @@
但leetcode中强调的深度和高度很明显是按照节点来计算的如图
![110.平衡二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155515650.png)
![110.平衡二叉树2](https://file.kamacoder.com/pics/20210203155515650.png)
关于根节点的深度究竟是1 还是 0不同的地方有不一样的标准leetcode的题目中都是以节点为一度即根节点深度是1。但维基百科上定义用边为一度即根节点的深度是0我们暂时以leetcode为准毕竟要在这上面刷题
@@ -998,7 +996,3 @@ public int GetHeight(TreeNode root)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 和求最大深度一个套路?
@@ -22,7 +20,7 @@
给定二叉树 [3,9,20,null,null,15,7],
![111.二叉树的最小深度1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315582586.png)
![111.二叉树的最小深度1](https://file.kamacoder.com/pics/2021020315582586.png)
返回它的最小深度 2.
@@ -752,7 +750,3 @@ public int MinDepth(TreeNode root)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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,7 +15,7 @@
示例:
给定如下二叉树,以及目标和 sum = 22
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230407210247.png)
![](https://file.kamacoder.com/pics/20230407210247.png)
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
@@ -55,7 +53,7 @@
如图所示:
![112.路径总和](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020316051216.png)
![112.路径总和](https://file.kamacoder.com/pics/2021020316051216.png)
图中可以看出遍历的路线并不要遍历整棵树所以递归函数需要返回值可以用bool类型表示。
@@ -232,7 +230,7 @@ public:
给定如下二叉树,以及目标和 sum = 22
![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png)
![113.路径总和ii1.png](https://file.kamacoder.com/pics/20210203160854654.png)
### 思路
@@ -241,7 +239,7 @@ public:
如图:
![113.路径总和ii](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160922745.png)
![113.路径总和ii](https://file.kamacoder.com/pics/20210203160922745.png)
为了尽可能的把细节体现出来,我写出如下代码(**这份代码并不简洁,但是逻辑非常清晰**
@@ -309,25 +307,25 @@ public:
0112.路径总和
```java
class solution {
public boolean haspathsum(treenode root, int targetsum) {
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) {
return false;
}
targetsum -= root.val;
targetSum -= root.val;
// 叶子结点
if (root.left == null && root.right == null) {
return targetsum == 0;
return targetSum == 0;
}
if (root.left != null) {
boolean left = haspathsum(root.left, targetsum);
if (left) { // 已经找到
boolean left = hasPathSum(root.left, targetSum);
if (left) { // 已经找到,提前返回
return true;
}
}
if (root.right != null) {
boolean right = haspathsum(root.right, targetsum);
if (right) { // 已经找到
boolean right = hasPathSum(root.right, targetSum);
if (right) { // 已经找到,提前返回
return true;
}
}
@@ -336,16 +334,16 @@ class solution {
}
// lc112 简洁方法
class solution {
public boolean haspathsum(treenode root, int targetsum) {
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false; // 为空退出
// 叶子节点判断是否符合
if (root.left == null && root.right == null) return root.val == targetsum;
if (root.left == null && root.right == null) return root.val == targetSum;
// 求两侧分支的路径和
return haspathsum(root.left, targetsum - root.val) || haspathsum(root.right, targetsum - root.val);
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
}
```
@@ -353,22 +351,22 @@ class solution {
迭代
```java
class solution {
public boolean haspathsum(treenode root, int targetsum) {
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null) return false;
stack<treenode> stack1 = new stack<>();
stack<integer> stack2 = new stack<>();
Stack<TreeNode> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
stack1.push(root);
stack2.push(root.val);
while(!stack1.isempty()) {
while(!stack1.isEmpty()) {
int size = stack1.size();
for(int i = 0; i < size; i++) {
treenode node = stack1.pop();
TreeNode node = stack1.pop();
int sum = stack2.pop();
// 如果该节点是叶子节点了同时该节点的路径数值等于sum那么就返回true
if(node.left == null && node.right == null && sum == targetsum) {
if(node.left == null && node.right == null && sum == targetSum) {
return true;
}
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
@@ -387,8 +385,9 @@ class solution {
}
}
```
```Java 統一迭代法
public boolean hasPathSum(TreeNode root, int targetSum) {
```Java
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
Stack<TreeNode> treeNodeStack = new Stack<>();
Stack<Integer> sumStack = new Stack<>();
@@ -422,38 +421,39 @@ class solution {
}
return false;
}
}
```
0113.路径总和-ii
```java
class solution {
public List<List<Integer>> pathsum(TreeNode root, int targetsum) {
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res; // 非空判断
List<Integer> path = new LinkedList<>();
preorderdfs(root, targetsum, res, path);
preOrderDfs(root, targetSum, res, path);
return res;
}
public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
public void preOrderDfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val);
// 遇到了叶子节点
if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) {
if (targetSum - root.val == 0) {
res.add(new ArrayList<>(path));
}
return; // 如果和不为 targetsum返回
}
if (root.left != null) {
preorderdfs(root.left, targetsum - root.val, res, path);
preOrderDfs(root.left, targetSum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) {
preorderdfs(root.right, targetsum - root.val, res, path);
preOrderDfs(root.right, targetSum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
}
@@ -1622,7 +1622,4 @@ public class Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 115.不同的子序列
@@ -72,7 +70,7 @@ dp[i][j]以i-1为结尾的s子序列中出现以j-1为结尾的t的个数为d
从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j] 是从上方和左上方推导而来,如图:,那么 dp[i][0] 和dp[0][j]是一定要初始化的。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221222165412.png)
![](https://file.kamacoder.com/pics/20221222165412.png)
每次当初始化的时候都要回顾一下dp[i][j]的定义,不要凭感觉初始化。
@@ -103,7 +101,7 @@ for (int j = 1; j <= t.size(); j++) dp[0][j] = 0; // 其实这行代码可以和
从递推公式dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; 和 dp[i][j] = dp[i - 1][j]; 中可以看出dp[i][j]都是根据左上方和正上方推出来的。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20221222165412.png)
![](https://file.kamacoder.com/pics/20221222165412.png)
所以遍历的时候一定是从上到下从左到右这样保证dp[i][j]可以根据之前计算出来的数值进行计算。
@@ -376,7 +374,3 @@ impl Solution {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 116. 填充每个节点的下一个右侧节点指针
@@ -28,7 +26,7 @@ struct Node {
* 你只能使用常量级额外空间。
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727143202.png)
![](https://file.kamacoder.com/pics/20210727143202.png)
## 思路
@@ -489,8 +487,4 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 121. 买卖股票的最佳时机
@@ -131,7 +129,7 @@ dp[0][1]表示第0天不持有股票不持有股票那么现金就是0
以示例1输入[7,1,5,3,6,4]为例dp数组状态如下
![121.买卖股票的最佳时机](https://code-thinking-1253855093.file.myqcloud.com/pics/20210224225642465.png)
![121.买卖股票的最佳时机](https://file.kamacoder.com/pics/20210224225642465.png)
dp[5][1]就是最终结果。
@@ -627,7 +625,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 122.买卖股票的最佳时机 II
@@ -68,7 +66,7 @@
如图:
![122.买卖股票的最佳时机II](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112917480858-20230310134659477.png)
![122.买卖股票的最佳时机II](https://file.kamacoder.com/pics/2020112917480858-20230310134659477.png)
一些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。
@@ -423,7 +421,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 122.买卖股票的最佳时机II
@@ -477,7 +475,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 123.买卖股票的最佳时机III
@@ -122,7 +120,7 @@ dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
以输入[1,2,3,4,5]为例
![123.买卖股票的最佳时机III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201228181724295-20230310134201291.png)
![123.买卖股票的最佳时机III](https://file.kamacoder.com/pics/20201228181724295-20230310134201291.png)
大家可以看到红色框为最后两次卖出的状态。
@@ -565,7 +563,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 127. 单词接龙
@@ -33,7 +31,7 @@
以示例1为例从这个图中可以看出 hit 到 cog的路线不止一条有三条一条是最短的长度为5两条长度为6。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210827175432.png)
![](https://file.kamacoder.com/pics/20210827175432.png)
本题只需要求出最短路径的长度就可以了,不用找出路径。
@@ -360,7 +358,3 @@ function diffonechar(word1: string, word2: string): boolean {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -383,7 +381,3 @@ int sumNumbers(struct TreeNode* root){
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 130. 被围绕的区域
@@ -10,7 +8,7 @@
给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220901104745.png)
![](https://file.kamacoder.com/pics/20220901104745.png)
* 输入board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
* 输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
@@ -30,11 +28,11 @@
步骤一:深搜或者广搜将地图周边的'O'全部改成'A',如图所示:
![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902102337.png)
![图一](https://file.kamacoder.com/pics/20220902102337.png)
步骤二:在遍历地图,将'O'全部改成'X'(地图中间的'O'改成了'X'),将'A'改回'O'(保留的地图周边的'O'),如图所示:
![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220902102831.png)
![图二](https://file.kamacoder.com/pics/20220902102831.png)
整体C++代码如下以下使用dfs实现其实遍历方式dfsbfs都是可以的。
@@ -793,7 +791,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 切割问题其实是一种组合问题!
@@ -1007,7 +1005,3 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -163,7 +161,7 @@ for (int i = s.size() - 1; i >= 0; i--) {
以输入:"aabc" 为例:
![132.分割回文串II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210124182218844.jpg)
![132.分割回文串II](https://file.kamacoder.com/pics/20210124182218844.jpg)
以上分析完毕,代码如下:
@@ -373,7 +371,3 @@ var minCut = function(s) {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 134. 加油站
@@ -146,7 +144,7 @@ i从0开始累加rest[i]和记为curSum一旦curSum小于零说明[0, i
如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230117165628.png)
![](https://file.kamacoder.com/pics/20230117165628.png)
那么为什么一旦[0i] 区间和为负数起始位置就可以是i+1呢i+1后面就不会出现更大的负数
@@ -154,7 +152,7 @@ i从0开始累加rest[i]和记为curSum一旦curSum小于零说明[0, i
那有没有可能 [0i] 区间 选某一个作为起点,累加到 i这里 curSum是不会小于零呢 如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230117170703.png)
![](https://file.kamacoder.com/pics/20230117170703.png)
如果 curSum<0 说明 区间和1 + 区间和2 < 0 那么 假设从上图中的位置开始计数curSum不会小于0的话就是 区间和2>0。
@@ -709,7 +707,3 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 135. 分发糖果
@@ -58,7 +56,7 @@ for (int i = 1; i < ratings.size(); i++) {
如图:
![135.分发糖果](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117114916878.png)
![135.分发糖果](https://file.kamacoder.com/pics/20201117114916878.png)
再确定左孩子大于右孩子的情况(从后向前遍历)
@@ -68,7 +66,7 @@ for (int i = 1; i < ratings.size(); i++) {
如果从前向后遍历rating[5]与rating[4]的比较 就不能用上 rating[5]与rating[6]的比较结果了 。如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20230202102044.png)
![](https://file.kamacoder.com/pics/20230202102044.png)
**所以确定左孩子大于右孩子的情况一定要从后向前遍历!**
@@ -84,7 +82,7 @@ for (int i = 1; i < ratings.size(); i++) {
如图:
![135.分发糖果1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201117115658791.png)
![135.分发糖果1](https://file.kamacoder.com/pics/20201117115658791.png)
所以该过程代码如下:
@@ -401,7 +399,3 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -182,7 +180,7 @@ dp[0]表示如果字符串为空的话,说明出现在字典里。
以输入: s = "leetcode", wordDict = ["leet", "code"]为例dp状态如图
![139.单词拆分](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202162652727.jpg)
![139.单词拆分](https://file.kamacoder.com/pics/20210202162652727.jpg)
dp[s.size()]就是最终结果
@@ -243,7 +241,7 @@ public:
使用用例s = "applepenapple", wordDict = ["apple", "pen"]对应的dp数组状态如下
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240809155103.png)
![](https://file.kamacoder.com/pics/20240809155103.png)
最后dp[s.size()] = 0 dp[13] = 0 而不是1因为先用 "apple" 去遍历的时候dp[8]并没有被赋值为1 还没用"pen"所以 dp[13]也不能变成1
@@ -565,7 +563,3 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 141. 环形链表
@@ -15,7 +13,7 @@
如果链表中存在环,则返回 true 。 否则,返回 false 。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727173600.png)
![](https://file.kamacoder.com/pics/20210727173600.png)
## 思路
@@ -160,7 +158,3 @@ function hasCycle(head: ListNode | null): boolean {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -22,7 +20,7 @@
**说明**:不允许修改给定的链表。
![循环链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20200816110112704.png)
![循环链表](https://file.kamacoder.com/pics/20200816110112704.png)
## 算法公开课
@@ -52,7 +50,7 @@
会发现最终都是这种情况, 如下图:
![142环形链表1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318162236720.png)
![142环形链表1](https://file.kamacoder.com/pics/20210318162236720.png)
fast和slow各自再走一步 fast和slow就相遇了
@@ -72,7 +70,7 @@ fast和slow各自再走一步 fast和slow就相遇了
环形入口节点到 fast指针与slow指针相遇节点 节点数为y。
从相遇节点 再到环形入口节点节点数为 z。 如图所示:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220925103433.png)
![](https://file.kamacoder.com/pics/20220925103433.png)
那么相遇时:
slow指针走过的节点数为: `x + y`
@@ -156,20 +154,20 @@ public:
即文章[链表:环找到了,那入口呢?](https://programmercarl.com/0142.环形链表II.html)中如下的地方:
![142环形链表5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210318165123581.png)
![142环形链表5](https://file.kamacoder.com/pics/20210318165123581.png)
首先slow进环的时候fast一定是先进环来了。
如果slow进环入口fast也在环入口那么把这个环展开成直线就是如下图的样子
![142环形链表3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816503266.png)
![142环形链表3](https://file.kamacoder.com/pics/2021031816503266.png)
可以看出如果slow 和 fast同时在环入口开始走一定会在环入口3相遇slow走了一圈fast走了两圈。
重点来了slow进环的时候fast一定是在环的任意一个位置如图
![142环形链表4](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031816515727.png)
![142环形链表4](https://file.kamacoder.com/pics/2021031816515727.png)
那么fast指针走到环入口3的时候已经走了k + n 个节点slow相应的应该走了(k + n) / 2 个节点。
@@ -465,7 +463,3 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -1,14 +1,12 @@
<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)
# 143.重排链表
[力扣题目链接](https://leetcode.cn/problems/reorder-list/submissions/)
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210726160122.png)
![](https://file.kamacoder.com/pics/20210726160122.png)
## 思路
@@ -689,7 +687,3 @@ void reorderList(struct ListNode* head){
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 这不仅仅是一道好题,也展现出计算机的思考方式
@@ -550,7 +548,3 @@ int evalRPN(char** tokens, int tokensSize) {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -1086,8 +1084,4 @@ public string ReverseWords(string s) {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -1,11 +1,5 @@
<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)
同:[链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html)
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 188.买卖股票的最佳时机IV
@@ -134,7 +132,7 @@ for (int j = 1; j < 2 * k; j += 2) {
以输入[1,2,3,4,5]k=2为例。
![188.买卖股票的最佳时机IV](https://code-thinking-1253855093.file.myqcloud.com/pics/20201229100358221.png)
![188.买卖股票的最佳时机IV](https://file.kamacoder.com/pics/20201229100358221.png)
最后一次卖出一定是利润最大的dp[prices.size() - 1][2 * k]即红色部分就是最后求解。
@@ -641,8 +639,4 @@ impl Solution {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 189. 旋转数组
@@ -212,7 +210,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 198.打家劫舍
@@ -89,7 +87,7 @@ for (int i = 2; i < nums.size(); i++) {
以示例二,输入[2,7,9,3,1]为例。
![198.打家劫舍](https://code-thinking-1253855093.file.myqcloud.com/pics/20210221170954115.jpg)
![198.打家劫舍](https://file.kamacoder.com/pics/20210221170954115.jpg)
红框dp[nums.size() - 1]为结果。
@@ -361,7 +359,3 @@ impl Solution {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 200. 岛屿数量
@@ -15,7 +13,7 @@
此外,你可以假设该网格的四条边均被水包围。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726093256.png)
![](https://file.kamacoder.com/pics/20220726093256.png)
提示:
@@ -30,7 +28,7 @@
也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图:
![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png)
![图一](https://file.kamacoder.com/pics/20220726094200.png)
这道题题目是 DFSBFS并查集基础题目。
@@ -50,7 +48,7 @@
如果从队列拿出节点,再去标记这个节点走过,就会发生下图所示的结果,会导致很多节点重复加入队列。
![图二](https://code-thinking-1253855093.file.myqcloud.com/pics/20220727100846.png)
![图二](https://file.kamacoder.com/pics/20220727100846.png)
超时写法 (从队列中取出节点再标记)
@@ -410,7 +408,3 @@ impl Solution {
```
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 200. 岛屿数量
@@ -14,7 +12,7 @@
此外,你可以假设该网格的四条边均被水包围。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726093256.png)
![](https://file.kamacoder.com/pics/20220726093256.png)
提示:
@@ -29,7 +27,7 @@
也就是说斜角度链接是不算了, 例如示例二,是三个岛屿,如图:
![图一](https://code-thinking-1253855093.file.myqcloud.com/pics/20220726094200.png)
![图一](https://file.kamacoder.com/pics/20220726094200.png)
这道题题目是 DFSBFS并查集基础题目。
@@ -463,7 +461,3 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -558,7 +556,3 @@ def next_value(n)
end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -36,11 +34,11 @@
这里以链表 1 4 2 4 来举例移除元素4。
![203_链表删除元素1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095351161.png)
![203_链表删除元素1](https://file.kamacoder.com/pics/20210316095351161.png)
如果使用CC++编程语言的话,不要忘了还要从内存中删除这两个移除的节点, 清理节点内存之后如图:
![203_链表删除元素2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095418280.png)
![203_链表删除元素2](https://file.kamacoder.com/pics/20210316095418280.png)
**当然如果使用java python的话就不用手动管理内存了。**
@@ -58,16 +56,16 @@
来看第一种操作:直接使用原来的链表来进行移除。
![203_链表删除元素3](https://code-thinking-1253855093.file.myqcloud.com/pics/2021031609544922.png)
![203_链表删除元素3](https://file.kamacoder.com/pics/2021031609544922.png)
移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。
所以头结点如何移除呢,其实只要将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。
![203_链表删除元素4](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095512470.png)
![203_链表删除元素4](https://file.kamacoder.com/pics/20210316095512470.png)
依然别忘将原头结点从内存中删掉。
![203_链表删除元素5](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095543775.png)
![203_链表删除元素5](https://file.kamacoder.com/pics/20210316095543775.png)
这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。
@@ -78,7 +76,7 @@
来看看如何设置一个虚拟头。依然还是在这个链表中移除元素1。
![203_链表删除元素6](https://code-thinking-1253855093.file.myqcloud.com/pics/20210316095619221.png)
![203_链表删除元素6](https://file.kamacoder.com/pics/20210316095619221.png)
这里来给链表添加一个虚拟头结点为新的头结点此时要移除这个旧头结点元素1。
@@ -808,7 +806,3 @@ end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 205. 同构字符串
@@ -180,7 +178,3 @@ function isIsomorphic(s: string, t: string): boolean {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。
@@ -29,7 +27,7 @@
其实只需要改变链表的next指针的指向直接将链表反转 ,而不用重新定义一个新的链表,如图所示:
![206_反转链表](https://code-thinking-1253855093.file.myqcloud.com/pics/20210218090901207.png)
![206_反转链表](https://file.kamacoder.com/pics/20210218090901207.png)
之前链表的头节点是元素1 反转之后头结点就是元素5 这里并没有添加或者删除节点仅仅是改变next指针的方向。
@@ -739,7 +737,3 @@ public ListNode reverseList(ListNode head) {
> 采用这种方法需要注意一点。就是当整个出栈循环结束以后cur正好指向原来链表的第一个结点而此时结点1中的next指向的是结点2因此最后还需要`cur.next = null`
![image-20230117195418626](https://raw.githubusercontent.com/liyuxuan7762/MyImageOSS/master/md_images/image-20230117195418626.png)
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是 广搜 可能是深搜。
@@ -59,7 +57,3 @@ public:
}
};
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 209.长度最小的子数组
@@ -106,7 +104,7 @@ public:
解题的关键在于 窗口的起始位置如何移动,如图所示:
![leetcode_209](https://code-thinking-1253855093.file.myqcloud.com/pics/20210312160441942.png)
![leetcode_209](https://file.kamacoder.com/pics/20210312160441942.png)
可以发现**滑动窗口的精妙之处在于根据当前子序列和大小的情况不断调节子序列的起始位置。从而将O(n^2)暴力解法降为O(n)。**
@@ -558,7 +556,3 @@ public class Solution {
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -1,3 +1,6 @@
* [做项目多个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)
```CPP
class Solution {

View File

@@ -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)
# 213.打家劫舍II
@@ -44,15 +42,15 @@
* 情况一:考虑不包含首尾元素
![213.打家劫舍II](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160748643-20230310134000692.jpg)
![213.打家劫舍II](https://file.kamacoder.com/pics/20210129160748643-20230310134000692.jpg)
* 情况二:考虑包含首元素,不包含尾元素
![213.打家劫舍II1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160821374-20230310134003961.jpg)
![213.打家劫舍II1](https://file.kamacoder.com/pics/20210129160821374-20230310134003961.jpg)
* 情况三:考虑包含尾元素,不包含首元素
![213.打家劫舍II2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210129160842491-20230310134008133.jpg)
![213.打家劫舍II2](https://file.kamacoder.com/pics/20210129160842491-20230310134008133.jpg)
**注意我这里用的是"考虑"**,例如情况三,虽然是考虑包含尾元素,但不一定要选尾部元素! 对于情况三取nums[1] 和 nums[3]就是最大的。
@@ -367,7 +365,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -47,7 +45,7 @@
选取过程如图:
![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975.png)
![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975.png)
图中可以看出只有最后取到集合13和为4 符合条件。
@@ -110,7 +108,7 @@ if (path.size() == k) {
本题和[77. 组合](https://programmercarl.com/0077.组合.html)区别之一就是集合固定的就是9个数[1,...,9]所以for循环固定i<=9
如图:
![216.组合总和III](https://code-thinking-1253855093.file.myqcloud.com/pics/20201123195717975-20230310113546003.png)
![216.组合总和III](https://file.kamacoder.com/pics/20201123195717975-20230310113546003.png)
处理过程就是 path收集每次选取的元素相当于树型结构里的边sum来统计path里元素的总和。
@@ -168,7 +166,7 @@ public:
这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。
如图:
![216.组合总和III1](https://code-thinking-1253855093.file.myqcloud.com/pics/2020112319580476.png)
![216.组合总和III1](https://file.kamacoder.com/pics/2020112319580476.png)
已选元素总和如果已经大于n图中数值为4那么往后遍历就没有意义了直接剪掉。
@@ -739,7 +737,3 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 222.完全二叉树的节点个数
@@ -155,7 +153,7 @@ public:
我来举一个典型的例子如题:
<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20200920221638903-20230310123444151.png' width=600> </img>
<img src='https://file.kamacoder.com/pics/20200920221638903-20230310123444151.png' width=600> </img>
完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。
@@ -164,10 +162,10 @@ public:
对于情况二分别递归左孩子和右孩子递归到某一深度一定会有左孩子或者右孩子为满二叉树然后依然可以按照情况1来计算。
完全二叉树(一)如图:
![222.完全二叉树的节点个数](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092543662.png)
![222.完全二叉树的节点个数](https://file.kamacoder.com/pics/20201124092543662.png)
完全二叉树(二)如图:
![222.完全二叉树的节点个数1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201124092634138.png)
![222.完全二叉树的节点个数1](https://file.kamacoder.com/pics/20201124092634138.png)
可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。
@@ -175,15 +173,15 @@ public:
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163554.png)
![](https://file.kamacoder.com/pics/20220829163554.png)
在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树,如图:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163709.png)
![](https://file.kamacoder.com/pics/20220829163709.png)
那有录友说了,这种情况,递归向左遍历的深度等于递归向右遍历的深度,但也不是满二叉树,如题:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220829163811.png)
![](https://file.kamacoder.com/pics/20220829163811.png)
如果这么想,大家就是对 完全二叉树理解有误区了,**以上这棵二叉树,它根本就不是一个完全二叉树**
@@ -894,7 +892,3 @@ public int CountNodes(TreeNode root)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -1367,7 +1365,3 @@ void myStackFree(MyStack* obj) {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 226.翻转二叉树
@@ -12,7 +10,7 @@
翻转一棵二叉树。
![226.翻转二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192644329.png)
![226.翻转二叉树](https://file.kamacoder.com/pics/20210203192644329.png)
这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell就是因为没在白板上写出翻转二叉树最后被Google拒绝了。真假不做判断全当一个乐子哈
@@ -37,7 +35,7 @@
如果要从整个树来看,翻转还真的挺复杂,整个树以中间分割线进行翻转,如图:
![226.翻转二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203192724351.png)
![226.翻转二叉树1](https://file.kamacoder.com/pics/20210203192724351.png)
可以发现想要翻转它,其实就把每一个节点的左右孩子交换一下就可以了。
@@ -1022,7 +1020,3 @@ public TreeNode InvertTree(TreeNode root) {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 工作上一定没人这么搞,但是考察对栈、队列理解程度的好题
@@ -691,7 +689,3 @@ impl MyQueue {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 234.回文链表
@@ -429,7 +427,3 @@ function reverseList(head: ListNode | null): ListNode | null {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 235. 二叉搜索树的最近公共祖先
@@ -16,7 +14,7 @@
例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5]
![235. 二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201018172243602.png)
![235. 二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20201018172243602.png)
示例 1:
@@ -54,7 +52,7 @@
如图我们从根节点搜索第一次遇到 cur节点是数值在[q, p]区间中 节点5此时可以说明 q p 一定分别存在于 节点 5的左子树和右子树中
![235.二叉搜索树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926164214.png)
![235.二叉搜索树的最近公共祖先](https://file.kamacoder.com/pics/20220926164214.png)
此时节点5是不是最近公共祖先 如果 从节点5继续向左遍历那么将错过成为p的祖先 如果从节点5继续向右遍历则错过成为q的祖先
@@ -66,7 +64,7 @@
如图所示p为节点6q为节点9
![235.二叉搜索树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/20220926165141.png)
![235.二叉搜索树的最近公共祖先2](https://file.kamacoder.com/pics/20220926165141.png)
可以看出直接按照指定的方向就可以找到节点8为最近公共祖先而且不需要遍历整棵树找到结果直接返回
@@ -548,7 +546,3 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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 @@
例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4]
![236. 二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/20201016173414722.png)
![236. 二叉树的最近公共祖先](https://file.kamacoder.com/pics/20201016173414722.png)
示例 1:
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
@@ -53,7 +51,7 @@
**首先最容易想到的一个情况如果找到一个节点发现左子树出现结点p右子树出现节点q或者 左子树出现结点q右子树出现节点p那么该节点就是节点p和q的最近公共祖先。** 即情况一:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922173502.png)
![](https://file.kamacoder.com/pics/20220922173502.png)
判断逻辑是 如果递归遍历遇到q就将q返回遇到p 就将p返回那么如果 左右子树的返回值都不为空说明此时的中节点一定是q 和p 的最近祖先。
@@ -63,7 +61,7 @@
**但是很多人容易忽略一个情况就是节点本身p(q)它拥有一个子孙节点q(p)。** 情况二:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220922173530.png)
![](https://file.kamacoder.com/pics/20220922173530.png)
其实情况一 和 情况二 代码实现过程都是一样的,也可以说,实现情况一的逻辑,顺便包含了情况二。
@@ -131,7 +129,7 @@ left与right的逻辑处理; // 中
如图:
![236.二叉树的最近公共祖先](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415105872.png)
![236.二叉树的最近公共祖先](https://file.kamacoder.com/pics/2021020415105872.png)
就像图中一样直接返回7。
@@ -164,7 +162,7 @@ TreeNode* right = lowestCommonAncestor(root->right, p, q);
如图:
![236.二叉树的最近公共祖先1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151125844.png)
![236.二叉树的最近公共祖先1](https://file.kamacoder.com/pics/20210204151125844.png)
图中节点10的左子树返回null右子树返回目标值7那么此时节点10的处理逻辑就是把右子树的返回值最近公共祖先7返回上去
@@ -185,7 +183,7 @@ else { // (left == NULL && right == NULL)
那么寻找最小公共祖先,完整流程图如下:
![236.二叉树的最近公共祖先2](https://code-thinking-1253855093.file.myqcloud.com/pics/202102041512582.png)
![236.二叉树的最近公共祖先2](https://file.kamacoder.com/pics/202102041512582.png)
**从图中,大家可以看到,我们是如何回溯遍历整棵二叉树,将结果返回给头结点的!**
@@ -491,7 +489,3 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
@@ -922,8 +920,4 @@ int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的
@@ -416,7 +414,3 @@ bool isAnagram(char* s, char* t) {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
> 以为只用了递归,其实还用了回溯
@@ -16,7 +14,7 @@
说明: 叶子节点是指没有子节点的节点。
示例:
![257.二叉树的所有路径1](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020415161576.png)
![257.二叉树的所有路径1](https://file.kamacoder.com/pics/2021020415161576.png)
## 算法公开课
@@ -30,7 +28,7 @@
前序遍历以及回溯的过程如图:
![257.二叉树的所有路径](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151702443.png)
![257.二叉树的所有路径](https://file.kamacoder.com/pics/20210204151702443.png)
我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。**
@@ -69,15 +67,15 @@ if (cur->left == NULL && cur->right == NULL) {
再来看一下终止处理的逻辑。
这里使用vector<int> 结构path来记录路径所以要把vector<int> 结构的path转为string格式再把这个string 放进 result里。
这里使用`vector<int>` 结构path来记录路径所以要把`vector<int>` 结构的path转为string格式再把这个string 放进 result里。
**那么为什么使用了vector<int> 结构来记录路径呢?** 因为在下面处理单层递归逻辑的时候要做回溯使用vector方便来做回溯。
**那么为什么使用了`vector<int>` 结构来记录路径呢?** 因为在下面处理单层递归逻辑的时候要做回溯使用vector方便来做回溯。
可能有的同学问了,我看有些人的代码也没有回溯啊。
**其实是有回溯的,只不过隐藏在函数调用时的参数赋值里**,下文我还会提到。
这里我们先使用vector<int>结构的path容器来记录路径那么终止处理逻辑如下
这里我们先使用`vector<int>`结构的path容器来记录路径那么终止处理逻辑如下
```CPP
if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点
@@ -317,7 +315,7 @@ public:
其实关键还在于 参数,使用的是 `string path`,这里并没有加上引用`&` 即本层递归中path + 该节点数值但该层递归结束上一层path的数值并不会受到任何影响。 如图所示:
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20220831173322.png)
![](https://file.kamacoder.com/pics/20220831173322.png)
节点4 的path在遍历到节点3path+3遍历节点3的递归结束之后返回节点4回溯的过程path并不会把3加上。
@@ -938,7 +936,3 @@ public void Traversal(TreeNode node, List<int> path, List<string> res)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 279.完全平方数
@@ -95,7 +93,7 @@ for (int i = 0; i <= n; i++) { // 遍历背包
已输入n为5例dp状态图如下
![279.完全平方数](https://code-thinking-1253855093.file.myqcloud.com/pics/20210202112617341.jpg)
![279.完全平方数](https://file.kamacoder.com/pics/20210202112617341.jpg)
dp[0] = 0
dp[1] = min(dp[0] + 1) = 1
@@ -479,7 +477,3 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 283. 移动零:动态规划:一样的套路,再求一次完全平方数
@@ -186,7 +184,3 @@ impl Solution {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 300.最长递增子序列
@@ -87,7 +85,7 @@ for (int i = 1; i < nums.size(); i++) {
输入:[0,1,0,3,2]dp数组的变化如下
![300.最长上升子序列](https://code-thinking-1253855093.file.myqcloud.com/pics/20210110170945618.jpg)
![300.最长上升子序列](https://file.kamacoder.com/pics/20210110170945618.jpg)
如果代码写出来但一直AC不了那么就把dp数组打印出来看看对不对
@@ -361,7 +359,3 @@ func lengthOfLIS(nums: Array<Int64>): Int64 {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@@ -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)
# 309.最佳买卖股票时机含冷冻期
@@ -49,7 +47,7 @@ dp[i][j]第i天状态为j所剩的最多现金为dp[i][j]。
* 状态三:今天卖出股票
* 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天!
![](https://code-thinking-1253855093.file.myqcloud.com/pics/518d5baaf33f4b2698064f8efb42edbf.png)
![](https://file.kamacoder.com/pics/518d5baaf33f4b2698064f8efb42edbf.png)
j的状态为
@@ -138,7 +136,7 @@ dp[i][3] = dp[i - 1][2];
以 [1,2,3,0,2] 为例dp数组如下
![309.最佳买卖股票时机含冷冻期](https://code-thinking-1253855093.file.myqcloud.com/pics/2021032317451040.png)
![309.最佳买卖股票时机含冷冻期](https://file.kamacoder.com/pics/2021032317451040.png)
最后结果是取 状态二,状态三,和状态四的最大值,不少同学会把状态四忘了,状态四是冷冻期,最后一天如果是冷冻期也可能是最大值。
@@ -603,8 +601,4 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

Some files were not shown because too many files have changed in this diff Show More