diff --git a/README.md b/README.md index 5fe165a7..d3b4fe66 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ > 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者) > 2. **正式出版** :[《代码随想录》](https://programmercarl.com/other/publish.html) 。 > 3. **PDF版本** :[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。 -> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。 +> 4. **算法公开课** :[《代码随想录》算法视频公开课](https://www.programmercarl.com/other/gongkaike.html) 。 > 5. **最强八股文** :[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。 > 6. **刷题顺序** :README已经将刷题顺序排好了,按照顺序一道一道刷就可以。 > 7. **学习社区** :一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html) 。 -> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。 +> 8. **提交代码** :本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://www.programmercarl.com/qita/join.html)了解提交代码的方式。 > 9. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境! @@ -51,19 +51,12 @@ ## 如何使用该刷题攻略 -电脑端还看不到留言,大家可以在公众号[「代码随想录」](https://img-blog.csdnimg.cn/20201124161234338.png),左下角有「刷题攻略」,这是手机版刷题攻略,看完就会发现有很多录友(代码随想录的朋友们)在文章下留言打卡,这份刷题顺序和题解已经陪伴了上万录友了,同时也说明文章的质量是经过上万人的考验! - -欢迎每一位学习算法的小伙伴加入到这个学习阵营来! - -**目前已经更新了,数组-> 链表-> 哈希表->字符串->栈与队列->树->回溯->贪心,八个专题了,正在讲解动态规划!** +按照先面的排列顺序,从数组开始刷起就可以了,顺序都安排好了,按顺序刷就好。 在刷题攻略中,每个专题开始都有理论基础篇,并不像是教科书般的理论介绍,而是从实战中归纳需要的基础知识。每个专题结束都有总结篇,最这个专题的归纳总结。 如果你是算法老手,这篇攻略也是复习的最佳资料,如果把每个系列对应的总结篇,快速过一遍,整个算法知识体系以及各种解法就重现脑海了。 - -目前「代码随想录」刷题攻略更新了:**200多篇文章,精讲了200道经典算法题目,共60w字的详细图解,大部分题目都搭配了20分钟左右的视频讲解**,视频质量很好,口碑很好,大家可以去看看,视频列表:[代码随想录视频讲解](https://www.bilibili.com/video/BV1fA4y1o715)。 - **这里每一篇题解,都是精品,值得仔细琢磨**。 我在题目讲解中统一使用C++,但你会发现下面几乎每篇题解都配有其他语言版本,Java、Python、Go、JavaScript等等,正是这些[热心小伙们](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)贡献的代码,当然我也会严格把控代码质量。 @@ -100,14 +93,11 @@ * [程序员应该用什么用具来写文档?](./problems/前序/程序员写文档工具.md) * 求职 + * [ACM模式练习网站,卡码网](https://kamacoder.com/) * [程序员的简历应该这么写!!(附简历模板)](./problems/前序/程序员简历.md) + * [【专业技能】应该这样写!](https://programmercarl.com/other/jianlizhuanye.html) + * [【项目经历】应该这样写!](https://programmercarl.com/other/jianlixiangmu.html) * [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md) - * [北京有这些互联网公司,你都知道么?](./problems/前序/北京互联网公司总结.md) - * [上海有这些互联网公司,你都知道么?](./problems/前序/上海互联网公司总结.md) - * [深圳有这些互联网公司,你都知道么?](./problems/前序/深圳互联网公司总结.md) - * [广州有这些互联网公司,你都知道么?](./problems/前序/广州互联网公司总结.md) - * [成都有这些互联网公司,你都知道么?](./problems/前序/成都互联网公司总结.md) - * [杭州有这些互联网公司,你都知道么?](./problems/前序/杭州互联网公司总结.md) * 算法性能分析 * [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md) @@ -506,7 +496,7 @@ # 关于作者 -大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发,CSDN博客专家。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 +大家好,我是程序员Carl,哈工大师兄,《代码随想录》作者,先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解,利用工作之余重新刷leetcode。 加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。 @@ -527,4 +517,3 @@

+
diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md
index b1987b87..614f6051 100644
--- a/problems/0005.最长回文子串.md
+++ b/problems/0005.最长回文子串.md
@@ -30,17 +30,17 @@
* 输出:"a"
-# 思路
+## 思路
本题和[647.回文子串](https://programmercarl.com/0647.回文子串.html) 差不多是一样的,但647.回文子串更基本一点,建议可以先做647.回文子串
-## 暴力解法
+### 暴力解法
两层for循环,遍历区间起始位置和终止位置,然后判断这个区间是不是回文。
时间复杂度:O(n^3)
-## 动态规划
+### 动态规划
动规五部曲:
@@ -208,7 +208,7 @@ public:
* 时间复杂度:O(n^2)
* 空间复杂度:O(n^2)
-## 双指针
+### 双指针
动态规划的空间复杂度是偏高的,我们再看一下双指针法。
@@ -258,9 +258,9 @@ public:
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```java
// 双指针 动态规划
@@ -327,7 +327,7 @@ class Solution {
}
```
-Python:
+### Python:
```python
class Solution:
@@ -377,7 +377,7 @@ class Solution:
return s[start:end]
```
-Go:
+### Go:
```go
func longestPalindrome(s string) string {
@@ -411,7 +411,7 @@ func longestPalindrome(s string) string {
```
-JavaScript:
+### JavaScript:
```js
//动态规划解法
@@ -527,7 +527,7 @@ var longestPalindrome = function(s) {
};
```
-C:
+### C:
动态规划:
```c
@@ -615,7 +615,7 @@ char * longestPalindrome(char * s){
}
```
-C#:
+### C#:
動態規則:
```c#
@@ -681,3 +681,4 @@ public class Solution {
+
diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md
index cf5e4520..b3ba1e5e 100644
--- a/problems/0017.电话号码的字母组合.md
+++ b/problems/0017.电话号码的字母组合.md
@@ -21,12 +21,12 @@
说明:尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[还得用回溯算法!| LeetCode:17.电话号码的字母组合](https://www.bilibili.com/video/BV1yV4y1V7Ug),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[还得用回溯算法!| LeetCode:17.电话号码的字母组合](https://www.bilibili.com/video/BV1yV4y1V7Ug),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
从示例上来说,输入"23",最直接的想法就是两层for循环遍历了吧,正好把组合的情况都输出了。
@@ -40,7 +40,7 @@
2. 两个字母就两个for循环,三个字符我就三个for循环,以此类推,然后发现代码根本写不出来
3. 输入1 * #按键等等异常情况
-## 数字和字母如何映射
+### 数字和字母如何映射
可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下:
@@ -59,7 +59,7 @@ const string letterMap[10] = {
};
```
-## 回溯法来解决n个for循环的问题
+### 回溯法来解决n个for循环的问题
对于回溯法还不了解的同学看这篇:[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)
@@ -134,9 +134,6 @@ for (int i = 0; i < letters.size(); i++) {
**但是要知道会有这些异常,如果是现场面试中,一定要考虑到!**
-
-## C++代码
-
关键地方都讲完了,按照[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中的回溯法模板,不难写出如下C++代码:
@@ -233,7 +230,7 @@ public:
所以大家可以按照版本一来写就可以了。
-# 总结
+## 总结
本篇将题目的三个要点一一列出,并重点强调了和前面讲解过的[77. 组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)的区别,本题是多个集合求组合,所以在回溯的搜索过程中,都有一些细节需要注意的。
@@ -241,10 +238,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
class Solution {
@@ -286,7 +283,7 @@ class Solution {
}
```
-## Python
+### Python
回溯
```python
class Solution:
@@ -435,7 +432,7 @@ class Solution:
-## Go
+### Go
主要在于递归中传递下一个数字
@@ -470,7 +467,7 @@ func dfs(digits string, start int) {
}
```
-## javaScript
+### JavaScript
```js
var letterCombinations = function(digits) {
@@ -497,7 +494,7 @@ var letterCombinations = function(digits) {
};
```
-## TypeScript
+### TypeScript
```typescript
function letterCombinations(digits: string): string[] {
@@ -531,7 +528,7 @@ function letterCombinations(digits: string): string[] {
};
```
-## Rust
+### Rust
```Rust
const map: [&str; 10] = [
@@ -563,7 +560,7 @@ impl Solution {
}
```
-## C
+### C
```c
char* path;
@@ -625,7 +622,7 @@ char ** letterCombinations(char * digits, int* returnSize){
}
```
-## Swift
+### Swift
```swift
func letterCombinations(_ digits: String) -> [String] {
@@ -666,7 +663,7 @@ func letterCombinations(_ digits: String) -> [String] {
}
```
-## Scala:
+### Scala
```scala
object Solution {
@@ -702,3 +699,4 @@ object Solution {
+
diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md
index ce9eccf0..40ee3a2e 100644
--- a/problems/0027.移除元素.md
+++ b/problems/0027.移除元素.md
@@ -152,10 +152,10 @@ public:
## 相关题目推荐
-* 26.删除排序数组中的重复项
-* 283.移动零
-* 844.比较含退格的字符串
-* 977.有序数组的平方
+* [26.删除排序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/)
+* [283.移动零](https://leetcode.cn/problems/move-zeroes/)
+* [844.比较含退格的字符串](https://leetcode.cn/problems/backspace-string-compare/)
+* [977.有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/)
## 其他语言版本
@@ -444,3 +444,4 @@ public class Solution {
+
diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md
index 34aa1086..3cfb673a 100644
--- a/problems/0031.下一个排列.md
+++ b/problems/0031.下一个排列.md
@@ -34,7 +34,7 @@
* 输出:[1]
-# 思路
+## 思路
一些同学可能手动写排列的顺序,都没有写对,那么写程序的话思路一定是有问题的了,我这里以1234为例子,把全排列都列出来。可以参考一下规律所在:
@@ -92,9 +92,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -159,7 +159,7 @@ class Solution {
}
```
-## Python
+### Python
>直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数
```python
class Solution:
@@ -191,7 +191,7 @@ class Solution:
"""
```
-## Go
+### Go
```go
//卡尔的解法
@@ -216,7 +216,7 @@ func reverse(a []int,begin,end int){
}
```
-## JavaScript
+### JavaScript
```js
//卡尔的解法(吐槽一下JavaScript的sort和其他语言的不太一样,只想到了拷贝数组去排序再替换原数组来实现nums的[i + 1, nums.length)升序排序)
@@ -272,3 +272,4 @@ var nextPermutation = function(nums) {
+
diff --git a/problems/0037.解数独.md b/problems/0037.解数独.md
index 6edd3c5b..1763063e 100644
--- a/problems/0037.解数独.md
+++ b/problems/0037.解数独.md
@@ -6,8 +6,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
- -如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) +> 如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg) # 37. 解数独 @@ -35,11 +34,9 @@ * 你可以假设给定的数独只有唯一解。 * 给定数独永远是 9x9 形式的。 -# 算法公开课 - -**《代码随想录》算法视频公开课:[回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独](https://www.bilibili.com/video/BV1TW4y1471V/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 - +## 算法公开课 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法二维递归?解数独不过如此!| LeetCode:37. 解数独](https://www.bilibili.com/video/BV1TW4y1471V/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -764,3 +761,4 @@ object Solution {
+
diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md
index 4d9466c3..cdf33c58 100644
--- a/problems/0039.组合总和.md
+++ b/problems/0039.组合总和.md
@@ -39,11 +39,11 @@ candidates 中的数字可以无限制重复被选取。
[3,5]
]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[Leetcode:39. 组合总和讲解](https://www.bilibili.com/video/BV1KT4y1M7HJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
题目中的**无限制重复被选取,吓得我赶紧想想 出现0 可咋办**,然后看到下面提示:1 <= candidates[i] <= 200,我就放心了。
@@ -57,7 +57,7 @@ candidates 中的数字可以无限制重复被选取。
而在[77.组合](https://programmercarl.com/0077.组合.html)和[216.组合总和III](https://programmercarl.com/0216.组合总和III.html) 中都可以知道要递归K层,因为要取k个元素的组合。
-## 回溯三部曲
+### 回溯三部曲
* 递归函数参数
@@ -156,7 +156,7 @@ public:
};
```
-## 剪枝优化
+### 剪枝优化
在这个树形结构中:
@@ -217,7 +217,7 @@ public:
* 时间复杂度: O(n * 2^n),注意这只是复杂度的上界,因为剪枝的存在,真实的时间复杂度远小于此
* 空间复杂度: O(target)
-# 总结
+## 总结
本题和我们之前讲过的[77.组合](https://programmercarl.com/0077.组合.html)、[216.组合总和III](https://programmercarl.com/0216.组合总和III.html)有两点不同:
@@ -238,10 +238,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
// 剪枝优化
@@ -271,7 +271,7 @@ class Solution {
}
```
-## Python
+### Python
回溯(版本一)
@@ -311,7 +311,7 @@ class Solution:
for i in range(startIndex, len(candidates)):
if total + candidates[i] > target:
- break
+ continue
total += candidates[i]
path.append(candidates[i])
self.backtracking(candidates, target, total, i, path, result)
@@ -370,7 +370,7 @@ class Solution:
```
-## Go
+### Go
主要在于递归中传递下一个数字
@@ -404,7 +404,7 @@ func dfs(candidates []int, start int, target int) {
}
```
-## JavaScript
+### JavaScript
```js
var combinationSum = function(candidates, target) {
@@ -430,7 +430,7 @@ var combinationSum = function(candidates, target) {
};
```
-## TypeScript
+### TypeScript
```typescript
function combinationSum(candidates: number[], target: number): number[][] {
@@ -456,7 +456,7 @@ function combinationSum(candidates: number[], target: number): number[][] {
};
```
-## Rust
+### Rust
```Rust
impl Solution {
@@ -485,7 +485,7 @@ impl Solution {
}
```
-## C
+### C
```c
int* path;
@@ -541,7 +541,7 @@ int** combinationSum(int* candidates, int candidatesSize, int target, int* retur
}
```
-## Swift
+### Swift
```swift
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
@@ -570,7 +570,7 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
}
```
-## Scala
+### Scala
```scala
object Solution {
@@ -604,3 +604,4 @@ object Solution {
+
diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md
index 9094020e..33e4a46f 100644
--- a/problems/0040.组合总和II.md
+++ b/problems/0040.组合总和II.md
@@ -41,13 +41,11 @@ candidates 中的每个数字在每个组合中只能使用一次。
]
```
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法中的去重,树层去重树枝去重,你弄清楚了没?| LeetCode:40.组合总和II](https://www.bilibili.com/video/BV12V4y1V73A),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
-
-# 思路
+## 思路
这道题目和[39.组合总和](https://programmercarl.com/0039.组合总和.html)如下区别:
@@ -86,7 +84,7 @@ candidates 中的每个数字在每个组合中只能使用一次。
可以看到图中,每个节点相对于 [39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)我多加了used数组,这个used数组下面会重点介绍。
-## 回溯三部曲
+### 回溯三部曲
* **递归函数参数**
@@ -217,7 +215,7 @@ public:
* 时间复杂度: O(n * 2^n)
* 空间复杂度: O(n)
-## 补充
+### 补充
这里直接用startIndex来去重也是可以的, 就不用used数组了。
@@ -257,7 +255,7 @@ public:
```
-# 总结
+## 总结
本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。
@@ -265,14 +263,10 @@ public:
所以Carl有必要把去重的这块彻彻底底的给大家讲清楚,**就连“树层去重”和“树枝去重”都是我自创的词汇,希望对大家理解有帮助!**
+## 其他语言版本
-
-
-# 其他语言版本
-
-
-## Java
+### Java
**使用标记数组**
```Java
class Solution {
@@ -355,7 +349,7 @@ class Solution {
}
```
-## Python
+### Python
回溯
```python
class Solution:
@@ -442,7 +436,7 @@ class Solution:
self.combinationSumHelper(candidates, target - candidates[i], i + 1, path, results)
path.pop()
```
-## Go
+### Go
主要在于如何在回溯中去重
**使用used数组**
@@ -518,7 +512,7 @@ func dfs(candidates []int, start int, target int) {
}
}
```
-## javaScript
+### JavaScript
```js
/**
@@ -588,7 +582,7 @@ var combinationSum2 = function(candidates, target) {
};
```
-## TypeScript
+### TypeScript
```typescript
function combinationSum2(candidates: number[], target: number): number[][] {
@@ -619,7 +613,7 @@ function combinationSum2(candidates: number[], target: number): number[][] {
};
```
-## Rust
+### Rust
```Rust
impl Solution {
@@ -654,7 +648,7 @@ impl Solution {
}
```
-## C
+### C
```c
int* path;
@@ -716,7 +710,7 @@ int** combinationSum2(int* candidates, int candidatesSize, int target, int* retu
}
```
-## Swift
+### Swift
```swift
func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
@@ -749,7 +743,7 @@ func combinationSum2(_ candidates: [Int], _ target: Int) -> [[Int]] {
```
-## Scala
+### Scala
```scala
object Solution {
@@ -784,3 +778,4 @@ object Solution {
+
diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md
index db66095d..1f1a543b 100644
--- a/problems/0042.接雨水.md
+++ b/problems/0042.接雨水.md
@@ -29,7 +29,7 @@
* 输出:9
-# 思路
+## 思路
接雨水问题在面试中还是常见题目的,有必要好好讲一讲。
@@ -39,7 +39,7 @@
* 动态规划
* 单调栈
-## 暴力解法
+### 暴力解法
本题暴力解法也是也是使用双指针。
@@ -137,7 +137,7 @@ public:
力扣后面修改了后台测试数据,所以以上暴力解法超时了。
-## 双指针优化
+### 双指针优化
在暴力解法中,我们可以看到只要记录左边柱子的最高高度 和 右边柱子的最高高度,就可以计算当前位置的雨水面积,这就是通过列来计算。
@@ -184,7 +184,7 @@ public:
};
```
-## 单调栈解法
+### 单调栈解法
关于单调栈的理论基础,单调栈适合解决什么问题,单调栈的工作过程,大家可以先看这题讲解 [739. 每日温度](https://programmercarl.com/0739.每日温度.html)。
@@ -194,7 +194,7 @@ public:
而接雨水这道题目,我们正需要寻找一个元素,右边最大元素以及左边最大元素,来计算雨水面积。
-### 准备工作
+#### 准备工作
那么本题使用单调栈有如下几个问题:
@@ -248,7 +248,7 @@ stack
+
diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md
index 2f0349b2..02c8e486 100644
--- a/problems/0045.跳跃游戏II.md
+++ b/problems/0045.跳跃游戏II.md
@@ -25,9 +25,9 @@
说明:
假设你总是可以到达数组的最后一个位置。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,最少跳几步还得看覆盖范围 | LeetCode: 45.跳跃游戏 II](https://www.bilibili.com/video/BV1Y24y1r7XZ),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -53,7 +53,7 @@
**图中覆盖范围的意义在于,只要红色的区域,最多两步一定可以到!(不用管具体怎么跳,反正一定可以跳到)**
-## 方法一
+### 方法一
从图中可以看出来,就是移动下标达到了当前覆盖的最远距离下标时,步数就要加一,来增加覆盖距离。最后的步数就是最少步数。
@@ -90,7 +90,7 @@ public:
* 空间复杂度: O(1)
-## 方法二
+### 方法二
依然是贪心,思路和方法一差不多,代码可以简洁一些。
@@ -469,3 +469,4 @@ impl Solution {
+
diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md
index de1af642..1f5263a7 100644
--- a/problems/0046.全排列.md
+++ b/problems/0046.全排列.md
@@ -24,9 +24,9 @@
]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合与排列的区别,回溯算法求解的时候,有何不同?| LeetCode:46.全排列](https://www.bilibili.com/video/BV19v4y1S79W/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -491,3 +491,4 @@ object Solution {
+
diff --git a/problems/0047.全排列II.md b/problems/0047.全排列II.md
index afede33a..4fed8a5c 100644
--- a/problems/0047.全排列II.md
+++ b/problems/0047.全排列II.md
@@ -31,9 +31,9 @@
* 1 <= nums.length <= 8
* -10 <= nums[i] <= 10
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II](https://www.bilibili.com/video/BV1R84y1i7Tm/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法求解全排列,如何去重?| LeetCode:47.全排列 II](https://www.bilibili.com/video/BV1R84y1i7Tm/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -58,7 +58,7 @@
在[46.全排列](https://programmercarl.com/0046.全排列.html)中已经详细讲解了排列问题的写法,在[40.组合总和II](https://programmercarl.com/0040.组合总和II.html) 、[90.子集II](https://programmercarl.com/0090.子集II.html)中详细讲解了去重的写法,所以这次我就不用回溯三部曲分析了,直接给出代码,如下:
-## C++代码
+
```CPP
class Solution {
@@ -170,7 +170,7 @@ if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == true) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
-```
+```
其实并不行,一定要加上 `used[i - 1] == false`或者`used[i - 1] == true`,因为 used[i - 1] 要一直是 true 或者一直是false 才可以,而不是 一会是true 一会又是false。 所以这个条件要写上。
@@ -179,7 +179,7 @@ if (i > 0 && nums[i] == nums[i - 1]) {
## 其他语言版本
-### java
+### Java
```java
class Solution {
@@ -221,7 +221,7 @@ class Solution {
}
```
-### python
+Python
```python
class Solution:
@@ -526,3 +526,4 @@ object Solution {
+
diff --git a/problems/0051.N皇后.md b/problems/0051.N皇后.md
index 13cdafb8..6bc4fa78 100644
--- a/problems/0051.N皇后.md
+++ b/problems/0051.N皇后.md
@@ -28,9 +28,9 @@ n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,
* 输入:n = 1
* 输出:[["Q"]]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后](https://www.bilibili.com/video/BV1Rd4y1c7Bq/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这就是传说中的N皇后? 回溯算法安排!| LeetCode:51.N皇后](https://www.bilibili.com/video/BV1Rd4y1c7Bq/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -864,3 +864,4 @@ object Solution {
+
diff --git a/problems/0052.N皇后II.md b/problems/0052.N皇后II.md
index 90b920ba..29c2b588 100644
--- a/problems/0052.N皇后II.md
+++ b/problems/0052.N皇后II.md
@@ -43,13 +43,11 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
".Q.."]
]
-# 思路
+## 思路
详看:[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
-# C++代码
-
```CPP
class Solution {
private:
@@ -100,8 +98,9 @@ public:
};
```
-# 其他语言补充
-JavaScript
+## 其他语言补充
+### JavaScript
+
```javascript
var totalNQueens = function(n) {
let count = 0;
@@ -146,7 +145,7 @@ var totalNQueens = function(n) {
};
```
-TypeScript:
+### TypeScript
```typescript
// 0-该格为空,1-该格有皇后
@@ -199,7 +198,7 @@ function checkValid(chess: GridStatus[][], i: number, j: number, n: number): boo
}
```
-C
+### C
```c
//path[i]为在i行,path[i]列上存在皇后
@@ -258,7 +257,8 @@ int totalNQueens(int n){
return answer;
}
```
-Java
+### Java
+
```java
class Solution {
int count = 0;
diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md
index fe4e4ed3..639c54bc 100644
--- a/problems/0053.最大子序和.md
+++ b/problems/0053.最大子序和.md
@@ -16,11 +16,13 @@
- 输出: 6
- 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法的巧妙需要慢慢体会!LeetCode:53. 最大子序和](https://www.bilibili.com/video/BV1aY4y1Z7ya),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-## 暴力解法
+## 思路
+
+### 暴力解法
暴力解法的思路,第一层 for 就是设置起始位置,第二层 for 循环遍历数组寻找最大值
@@ -48,7 +50,7 @@ public:
以上暴力的解法 C++勉强可以过,其他语言就不确定了。
-## 贪心解法
+### 贪心解法
**贪心贪的是哪里呢?**
@@ -104,7 +106,7 @@ public:
当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。
-## 常见误区
+### 常见误区
误区一:
@@ -122,7 +124,7 @@ public:
其实并不会,因为还有一个变量 result 一直在更新 最大的连续和,只要有更大的连续和出现,result 就更新了,那么 result 已经把 4 更新了,后面 连续和变成 3,也不会对最后结果有影响。
-## 动态规划
+### 动态规划
当然本题还可以用动态规划来做,在代码随想录动态规划章节我会详细介绍,如果大家想在想看,可以直接跳转:[动态规划版本详解](https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html#%E6%80%9D%E8%B7%AF)
diff --git a/problems/0053.最大子序和(动态规划).md b/problems/0053.最大子序和(动态规划).md
index 6f3b3686..f1b64709 100644
--- a/problems/0053.最大子序和(动态规划).md
+++ b/problems/0053.最大子序和(动态规划).md
@@ -17,7 +17,7 @@
## 算法公开课
-**《代码随想录》算法视频公开课:[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和](https://www.bilibili.com/video/BV19V4y1F7b5),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -97,8 +97,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
/**
* 1.dp[i]代表当前下标对应的最大值
@@ -140,7 +140,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
@@ -153,7 +154,8 @@ class Solution:
return result
```
-Go:
+### Go:
+
```Go
// solution
// 1, dp
@@ -184,7 +186,7 @@ func max(a,b int) int{
}
```
-JavaScript:
+### JavaScript:
```javascript
const maxSubArray = nums => {
@@ -203,8 +205,7 @@ const maxSubArray = nums => {
};
```
-
-Scala:
+### Scala:
```scala
object Solution {
@@ -221,7 +222,7 @@ object Solution {
}
```
-TypeScript:
+### TypeScript:
```typescript
function maxSubArray(nums: number[]): number {
@@ -244,3 +245,4 @@ function maxSubArray(nums: number[]): number {
+
diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md
index a38e8237..d855f1a1 100644
--- a/problems/0054.螺旋矩阵.md
+++ b/problems/0054.螺旋矩阵.md
@@ -6,7 +6,7 @@
-## 54.螺旋矩阵
+# 54.螺旋矩阵
[力扣题目链接](https://leetcode.cn/problems/spiral-matrix/)
diff --git a/problems/0055.跳跃游戏.md b/problems/0055.跳跃游戏.md
index e54c2034..bedb09ab 100644
--- a/problems/0055.跳跃游戏.md
+++ b/problems/0055.跳跃游戏.md
@@ -26,9 +26,9 @@
- 输出: false
- 解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,怎么跳跃不重要,关键在覆盖范围 | LeetCode:55.跳跃游戏](https://www.bilibili.com/video/BV1VG4y1X7kB),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md
index 8705f840..95781b1a 100644
--- a/problems/0056.合并区间.md
+++ b/problems/0056.合并区间.md
@@ -22,9 +22,9 @@
* 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
* 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,合并区间有细节!LeetCode:56.合并区间](https://www.bilibili.com/video/BV1wx4y157nD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -341,3 +341,4 @@ impl Solution {
+
diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md
index f03fcdad..78d9385a 100644
--- a/problems/0059.螺旋矩阵II.md
+++ b/problems/0059.螺旋矩阵II.md
@@ -688,6 +688,58 @@ public class Solution {
}
```
+### Ruby:
+```ruby
+def generate_matrix(n)
+ result = Array.new(n) { Array.new(n, 0) }
+ #循环次数
+ loop_times = 0
+ #步长
+ step = n - 1
+ val = 1
+
+
+ while loop_times < n / 2
+ #模拟从左向右
+ for i in 0..step - 1
+ #行数不变,列数变
+ result[loop_times][i+loop_times] = val
+ val += 1
+ end
+
+ #模拟从上到下
+ for i in 0..step - 1
+ #列数不变,行数变
+ result[i+loop_times][n-loop_times-1] = val
+ val += 1
+ end
+
+ #模拟从右到左
+ for i in 0..step - 1
+ #行数不变,列数变
+ result[n-loop_times-1][n-loop_times-i-1] = val
+ val += 1
+ end
+
+ #模拟从下到上
+ for i in 0..step - 1
+ #列数不变,行数变
+ result[n-loop_times-i-1][loop_times] = val
+ val += 1
+ end
+
+ loop_times += 1
+ step -= 2
+ end
+
+ #如果是奇数,则填充最后一个元素
+ result[n/2][n/2] = n**2 if n % 2
+
+ return result
+
+end
+```
+
diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md
index 5111e30e..985c7575 100644
--- a/problems/0062.不同路径.md
+++ b/problems/0062.不同路径.md
@@ -50,9 +50,9 @@
* 1 <= m, n <= 100
* 题目数据保证答案小于等于 2 * 10^9
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划中如何初始化很重要!| LeetCode:62.不同路径](https://www.bilibili.com/video/BV1ve4y1x7Eu/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md
index cb305b41..3d243a7a 100644
--- a/problems/0063.不同路径II.md
+++ b/problems/0063.不同路径II.md
@@ -46,9 +46,9 @@
* 1 <= m, n <= 100
* obstacleGrid[i][j] 为 0 或 1
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,这次遇到障碍了| LeetCode:63. 不同路径 II](https://www.bilibili.com/video/BV1Ld4y1k7c6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md
index 1b24e491..1a1f7e31 100644
--- a/problems/0070.爬楼梯.md
+++ b/problems/0070.爬楼梯.md
@@ -29,9 +29,9 @@
* 1 阶 + 2 阶
* 2 阶 + 1 阶
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透动态规划-爬楼梯|LeetCode:70.爬楼梯)](https://www.bilibili.com/video/BV17h411h7UH),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -522,3 +522,4 @@ impl Solution {
+
diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md
index 8c85985f..4ca7a371 100644
--- a/problems/0070.爬楼梯完全背包版本.md
+++ b/problems/0070.爬楼梯完全背包版本.md
@@ -127,8 +127,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public int climbStairs(int n) {
@@ -148,7 +148,7 @@ class Solution {
}
```
-Python3:
+### Python3:
```python
@@ -166,8 +166,8 @@ class Solution:
return dp[n]
```
+### Go:
-Go:
```go
func climbStairs(n int) int {
//定义
@@ -189,7 +189,8 @@ func climbStairs(n int) int {
}
```
-JavaScript:
+### JavaScript:
+
```javascript
var climbStairs = function(n) {
const dp = new Array(n + 1).fill(0);
@@ -206,7 +207,7 @@ var climbStairs = function(n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function climbStairs(n: number): number {
@@ -226,7 +227,7 @@ function climbStairs(n: number): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -250,4 +251,3 @@ impl Solution {
-
diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md
index 703e8913..1ed9a860 100644
--- a/problems/0072.编辑距离.md
+++ b/problems/0072.编辑距离.md
@@ -40,8 +40,8 @@ exection -> execution (插入 'u')
* 0 <= word1.length, word2.length <= 500
* word1 和 word2 由小写英文字母组成
-# 算法公开课
-**《代码随想录》算法视频公开课:[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+## 算法公开课
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划终极绝杀! LeetCode:72.编辑距离](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -227,8 +227,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
public int minDistance(String word1, String word2) {
int m = word1.length();
@@ -256,7 +256,8 @@ public int minDistance(String word1, String word2) {
}
```
-Python:
+### Python:
+
```python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
@@ -274,7 +275,8 @@ class Solution:
return dp[-1][-1]
```
-Go:
+### Go:
+
```Go
func minDistance(word1 string, word2 string) int {
m, n := len(word1), len(word2)
@@ -310,8 +312,8 @@ func Min(args ...int) int {
}
```
+### Javascript:
-Javascript:
```javascript
const minDistance = (word1, word2) => {
let dp = Array.from(Array(word1.length + 1), () => Array(word2.length+1).fill(0));
@@ -338,7 +340,7 @@ const minDistance = (word1, word2) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function minDistance(word1: string, word2: string): number {
@@ -373,7 +375,7 @@ function minDistance(word1: string, word2: string): number {
};
```
-C:
+### C:
```c
@@ -405,3 +407,4 @@ int minDistance(char * word1, char * word2){
+
diff --git a/problems/0077.组合.md b/problems/0077.组合.md
index 444e15ce..8d448739 100644
--- a/problems/0077.组合.md
+++ b/problems/0077.组合.md
@@ -5,10 +5,6 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
- - - - # 第77题. 组合 [力扣题目链接](https://leetcode.cn/problems/combinations/ ) @@ -27,13 +23,12 @@ [1,4], ] -# 算法公开课 +## 算法公开课 + +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -**《代码随想录》算法视频公开课:[带你学透回溯算法-组合问题(对应力扣题目:77.组合)](https://www.bilibili.com/video/BV1ti4y1L7cv),[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 - - -# 思路 +## 思路 本题是回溯法的经典题目。 @@ -108,7 +103,7 @@ for (int i = 1; i <= n; i++) { 在[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)中我们提到了回溯法三部曲,那么我们按照回溯法三部曲开始正式讲解代码了。 -## 回溯法三部曲 +### 回溯法三部曲 * 递归函数的返回值以及参数 @@ -345,13 +340,35 @@ public: - - ## 其他语言版本 -### Java: +### Java: +未剪枝优化 +```java +class Solution { + List
+
diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md
index 3926d006..9577d65f 100644
--- a/problems/0077.组合优化.md
+++ b/problems/0077.组合优化.md
@@ -7,8 +7,11 @@
# 77.组合优化
+## 算法公开课
-**《代码随想录》算法视频公开课:[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[组合问题的剪枝操作](https://www.bilibili.com/video/BV1wi4y157er),相信结合视频在看本篇题解,更有助于大家对本题的理解。**
+
+## 思路
在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们通过回溯搜索法,解决了n个数中求k个数的组合问题。
@@ -46,7 +49,7 @@ public:
};
```
-# 剪枝优化
+## 剪枝优化
我们说过,回溯法虽然是暴力搜索,但也有时候可以有点剪枝优化一下的。
@@ -135,7 +138,7 @@ public:
-# 总结
+## 总结
本篇我们针对求组合问题的回溯法代码做了剪枝优化,这个优化如果不画图的话,其实不好理解,也不好讲清楚。
@@ -143,14 +146,10 @@ public:
**就酱,学到了就帮Carl转发一下吧,让更多的同学知道这里!**
-
-
-
-
## 其他语言版本
+### Java
-Java:
```java
class Solution {
List
+
diff --git a/problems/0078.子集.md b/problems/0078.子集.md
index 21009f6a..5f3654de 100644
--- a/problems/0078.子集.md
+++ b/problems/0078.子集.md
@@ -27,12 +27,12 @@
[]
]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集](https://www.bilibili.com/video/BV1U84y1q7Ci),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,树上节点都是目标集和! | LeetCode:78.子集](https://www.bilibili.com/video/BV1U84y1q7Ci),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
求子集问题和[77.组合](https://programmercarl.com/0077.组合.html)和[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)又不一样了。
@@ -52,7 +52,7 @@
从图中红线部分,可以看出**遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合**。
-## 回溯三部曲
+### 回溯三部曲
* 递归函数参数
@@ -102,8 +102,6 @@ for (int i = startIndex; i < nums.size(); i++) {
}
```
-## C++代码
-
根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板:
```
@@ -158,7 +156,7 @@ public:
并不会,因为每次递归的下一层就是从i+1开始的。
-# 总结
+## 总结
相信大家经过了
* 组合问题:
@@ -178,10 +176,10 @@ public:
**而组合问题、分割问题是收集树形结构中叶子节点的结果**。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
List
+
diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md
index f9a83508..9b76229a 100644
--- a/problems/0084.柱状图中最大的矩形.md
+++ b/problems/0084.柱状图中最大的矩形.md
@@ -20,7 +20,7 @@
* 1 <= heights.length <=10^5
* 0 <= heights[i] <= 10^4
-# 思路
+## 思路
本题和[42. 接雨水](https://programmercarl.com/0042.接雨水.html),是遥相呼应的两道题目,建议都要仔细做一做,原理上有很多相同的地方,但细节上又有差异,更可以加深对单调栈的理解!
@@ -28,7 +28,7 @@
我们先来看一下暴力解法的解法:
-## 暴力解法
+### 暴力解法
```CPP
class Solution {
@@ -55,7 +55,7 @@ public:
如上代码并不能通过leetcode,超时了,因为时间复杂度是$O(n^2)$。
-## 双指针解法
+### 双指针解法
本题双指针的写法整体思路和[42. 接雨水](https://programmercarl.com/0042.接雨水.html)是一致的,但要比[42. 接雨水](https://programmercarl.com/0042.接雨水.html)难一些。
@@ -98,7 +98,7 @@ public:
};
```
-## 单调栈
+### 单调栈
本地单调栈的解法和接雨水的题目是遥相呼应的。
@@ -169,7 +169,7 @@ public:
}
};
-```
+```
细心的录友会发现,我在 height数组上后,都加了一个元素0, 为什么这么做呢?
@@ -229,7 +229,7 @@ public:
## 其他语言版本
-Java:
+### Java:
暴力解法:
```java
@@ -335,7 +335,7 @@ class Solution {
}
```
-Python3:
+### Python3:
```python
@@ -468,7 +468,7 @@ class Solution:
```
-Go:
+### Go:
> 单调栈
@@ -506,7 +506,8 @@ func largestRectangleArea(heights []int) int {
```
-JavaScript:
+### JavaScript:
+
```javascript
//双指针 js中运行速度最快
var largestRectangleArea = function(heights) {
@@ -581,7 +582,7 @@ var largestRectangleArea = function(heights) {
return maxArea;
};
```
-TypeScript:
+### TypeScript:
> 暴力法(会超时):
@@ -669,9 +670,119 @@ function largestRectangleArea(heights: number[]): number {
};
```
+### Rust:
+
+双指针预处理
+```rust
+
+impl Solution {
+ pub fn largest_rectangle_area(v: Vec
+
diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md
index 3238ee52..13080cd9 100644
--- a/problems/0090.子集II.md
+++ b/problems/0090.子集II.md
@@ -4,7 +4,6 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
- # 90.子集II [力扣题目链接](https://leetcode.cn/problems/subsets-ii/) @@ -25,9 +24,9 @@ [] ] -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -240,7 +239,7 @@ class Solution { -#### Python3 +### Python3 回溯 利用used数组去重 ```python @@ -646,3 +645,4 @@ object Solution {
+
diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md
index 55e57dde..59cd92da 100644
--- a/problems/0093.复原IP地址.md
+++ b/problems/0093.复原IP地址.md
@@ -40,16 +40,12 @@
* 0 <= s.length <= 3000
* s 仅由数字组成
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
-# 算法公开课
-
-**《代码随想录》算法视频公开课:[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。
@@ -63,7 +59,7 @@

-## 回溯三部曲
+### 回溯三部曲
* 递归参数
@@ -134,7 +130,7 @@ for (int i = startIndex; i < s.size(); i++) {
}
```
-## 判断子串是否合法
+### 判断子串是否合法
最后就是在写一个判断段位是否是有效段位了。
@@ -169,8 +165,6 @@ bool isValid(const string& s, int start, int end) {
}
```
-## C++代码
-
根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板:
@@ -247,7 +241,7 @@ public:
* 时间复杂度: O(3^4),IP地址最多包含4个数字,每个数字最多有3种可能的分割方式,则搜索树的最大深度为4,每个节点最多有3个子节点。
* 空间复杂度: O(n)
-# 总结
+## 总结
在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。
@@ -259,9 +253,9 @@ public:
-# 其他语言版本
+## 其他语言版本
-## java
+### Java
```java
class Solution {
@@ -402,7 +396,7 @@ class Solution {
```
-## python
+### Python
回溯(版本一)
```python
@@ -478,9 +472,7 @@ class Solution:
```
-
-
-## Go
+### Go
```go
var (
@@ -517,7 +509,7 @@ func dfs(s string, start int) {
}
```
-## JavaScript
+### JavaScript
```js
/**
@@ -547,7 +539,7 @@ var restoreIpAddresses = function(s) {
};
```
-## TypeScript
+### TypeScript
```typescript
function isValidIpSegment(str: string): boolean {
@@ -586,7 +578,7 @@ function restoreIpAddresses(s: string): string[] {
};
```
-## Rust
+### Rust
```Rust
impl Solution {
@@ -643,7 +635,7 @@ impl Solution {
}
```
-## C
+### C
```c
//记录结果
char** result;
@@ -719,7 +711,7 @@ char ** restoreIpAddresses(char * s, int* returnSize){
}
```
-## Swift
+### Swift
```swift
// 判断区间段是否合法
@@ -766,7 +758,7 @@ func restoreIpAddresses(_ s: String) -> [String] {
}
```
-## Scala
+### Scala
```scala
object Solution {
@@ -813,3 +805,4 @@ object Solution {
+
diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md
index 368a5747..8d58cc5a 100644
--- a/problems/0096.不同的二叉搜索树.md
+++ b/problems/0096.不同的二叉搜索树.md
@@ -16,9 +16,9 @@

-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md
index 95b657a5..a48ec065 100644
--- a/problems/0098.验证二叉搜索树.md
+++ b/problems/0098.验证二叉搜索树.md
@@ -20,18 +20,18 @@

-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。
有了这个特性,**验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。**
-## 递归法
+### 递归法
可以递归中序遍历将二叉搜索树转变成一个数组,代码如下:
@@ -211,7 +211,7 @@ public:
最后这份代码看上去整洁一些,思路也清晰。
-## 迭代法
+### 迭代法
可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)
@@ -245,7 +245,7 @@ public:
在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。
-# 总结
+## 总结
这道题目是一个简单题,但对于没接触过的同学还是有难度的。
@@ -254,10 +254,10 @@ public:
只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
//使用統一迭代法
@@ -369,7 +369,7 @@ class Solution {
}
```
-## Python
+### Python
递归法(版本一)利用中序递增性质,转换成数组
```python
@@ -479,7 +479,7 @@ class Solution:
```
-## Go
+### Go
```Go
func isValidBST(root *TreeNode) bool {
@@ -526,7 +526,7 @@ func isValidBST(root *TreeNode) bool {
}
```
-## JavaScript
+### JavaScript
辅助数组解决
@@ -595,7 +595,7 @@ var isValidBST = function (root) {
};
```
-## TypeScript
+### TypeScript
> 辅助数组解决:
@@ -637,7 +637,7 @@ function isValidBST(root: TreeNode | null): boolean {
};
```
-## Scala
+### Scala
辅助数组解决:
```scala
@@ -682,7 +682,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -735,3 +735,4 @@ impl Solution {
+
diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md
index 96acacf6..56a6c884 100644
--- a/problems/0100.相同的树.md
+++ b/problems/0100.相同的树.md
@@ -19,7 +19,7 @@

-# 思路
+## 思路
在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。
@@ -115,7 +115,7 @@ public:
当然我可以把如上代码整理如下:
-## 递归
+### 递归
```CPP
class Solution {
@@ -134,7 +134,7 @@ public:
};
```
-## 迭代法
+### 迭代法
```CPP
class Solution {
@@ -166,9 +166,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```java
// 递归法
@@ -205,7 +205,8 @@ class Solution {
}
}
```
-Python:
+### Python:
+
```python
# 递归法
class Solution:
@@ -236,7 +237,8 @@ class Solution:
que.append(rightNode.right)
return True
```
-Go:
+### Go:
+
> 递归法
```go
func isSameTree(p *TreeNode, q *TreeNode) bool {
@@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
}
```
-JavaScript:
+### JavaScript:
> 递归法
@@ -296,7 +298,7 @@ var isSameTree = (p, q) => {
};
```
-TypeScript:
+### TypeScript:
> 递归法-先序遍历
@@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
+
diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md
index a0bab999..0144fc5c 100644
--- a/problems/0106.从中序与后序遍历序列构造二叉树.md
+++ b/problems/0106.从中序与后序遍历序列构造二叉树.md
@@ -29,9 +29,9 @@

-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder);
完整代码如下:
-### C++完整代码
-
```CPP
class Solution {
private:
@@ -281,8 +279,6 @@ public:
下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割)
-### C++优化版本
-
```CPP
class Solution {
private:
@@ -400,8 +396,9 @@ public:
};
```
+## 相关题目推荐
-# 105.从前序与中序遍历序列构造二叉树
+### 105.从前序与中序遍历序列构造二叉树
[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
@@ -418,7 +415,7 @@ public:

-## 思路
+### 思路
本题和106是一样的道理。
@@ -547,7 +544,7 @@ public:
};
```
-# 思考题
+## 思考题
前序和中序可以唯一确定一棵二叉树。
@@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
所以前序和后序不能唯一确定一棵二叉树!
-# 总结
+## 总结
之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。
@@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
106.从中序与后序遍历序列构造二叉树
@@ -688,7 +685,7 @@ class Solution {
}
```
-## Python
+### Python
105.从前序与中序遍历序列构造二叉树
@@ -754,7 +751,7 @@ class Solution:
return root
```
-## Go
+### Go
106 从中序与后序遍历序列构造二叉树
@@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode {
```
-
-
-## JavaScript
+### JavaScript
```javascript
var buildTree = function(inorder, postorder) {
@@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) {
};
```
-## TypeScript
+### TypeScript
> 106.从中序与后序遍历序列构造二叉树
@@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
};
```
-## C
+### C
106 从中序与后序遍历序列构造二叉树
@@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
}
```
-## Swift
+### Swift
105 从前序与中序遍历序列构造二叉树
@@ -1140,7 +1135,7 @@ class Solution_0106 {
}
```
-## Scala
+### Scala
106 从中序与后序遍历序列构造二叉树
@@ -1188,7 +1183,7 @@ object Solution {
}
```
-## rust
+### Rust
106 从中序与后序遍历序列构造二叉树
@@ -1238,3 +1233,4 @@ impl Solution {
+
diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md
index 056ef3e2..e699005e 100644
--- a/problems/0108.将有序数组转换为二叉搜索树.md
+++ b/problems/0108.将有序数组转换为二叉搜索树.md
@@ -20,11 +20,11 @@

-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
做这道题目之前大家可以了解一下这几道:
@@ -71,7 +71,7 @@
**这也是题目中强调答案不是唯一的原因。 理解这一点,这道题目算是理解到位了**。
-## 递归
+### 递归
递归三部曲:
@@ -155,7 +155,7 @@ public:
**注意:在调用traversal的时候传入的left和right为什么是0和nums.size() - 1,因为定义的区间为左闭右闭**。
-## 迭代法
+### 迭代法
迭代法可以通过三个队列来模拟,一个队列放遍历的节点,一个队列放左区间下标,一个队列放右区间下标。
@@ -203,7 +203,7 @@ public:
};
```
-# 总结
+## 总结
**在[二叉树:构造二叉树登场!](https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html) 和 [二叉树:构造一棵最大的二叉树](https://programmercarl.com/0654.最大二叉树.html)之后,我们顺理成章的应该构造一下二叉搜索树了,一不小心还是一棵平衡二叉搜索树**。
@@ -216,10 +216,10 @@ public:
最后依然给出迭代的方法,其实就是模拟取中间元素,然后不断分割去构造二叉树的过程。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
递归: 左闭右开 [left,right)
```Java
@@ -315,7 +315,7 @@ class Solution {
}
```
-## Python
+### Python
递归法
```python
class Solution:
@@ -377,7 +377,7 @@ class Solution:
```
-## Go
+### Go
递归(隐含回溯)
@@ -396,7 +396,7 @@ func sortedArrayToBST(nums []int) *TreeNode {
}
```
-## JavaScript
+### JavaScript
递归
```javascript
@@ -453,7 +453,7 @@ var sortedArrayToBST = function(nums) {
return root;
};
```
-## TypeScript
+### TypeScript
```typescript
function sortedArrayToBST(nums: number[]): TreeNode | null {
@@ -469,7 +469,7 @@ function sortedArrayToBST(nums: number[]): TreeNode | null {
};
```
-## C
+### C
递归
```c
@@ -490,7 +490,7 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
}
```
-## Scala
+### Scala
递归:
@@ -511,7 +511,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -536,3 +536,4 @@ impl Solution {
+
diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md
index 39285a3b..be03f719 100644
--- a/problems/0112.路径总和.md
+++ b/problems/0112.路径总和.md
@@ -21,9 +21,9 @@
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
-## 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -32,8 +32,8 @@
那么接下来我通过详细讲解如下两道题,来回答这个问题:
-* 112.路径总和
-* 113.路径总和ii
+* [112.路径总和](https://leetcode.cn/problems/path-sum/)
+* [113.路径总和ii](https://leetcode.cn/problems/path-sum-ii/)
这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。
@@ -218,7 +218,9 @@ public:
如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。
-# 113. 路径总和ii
+## 相关题目推荐
+
+### 113. 路径总和ii
[力扣题目链接](https://leetcode.cn/problems/path-sum-ii/)
@@ -232,7 +234,7 @@ public:

-## 思路
+### 思路
113.路径总和ii要遍历整个树,找到所有路径,**所以递归函数不要返回值!**
@@ -289,7 +291,7 @@ public:
至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。
-## 总结
+### 总结
本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。
@@ -300,11 +302,11 @@ public:
-# 其他语言版本
+## 其他语言版本
-## java
+### Java
-### 0112.路径总和
+0112.路径总和
```java
class solution {
@@ -422,7 +424,7 @@ class solution {
}
```
-### 0113.路径总和-ii
+0113.路径总和-ii
```java
class solution {
@@ -529,9 +531,9 @@ class Solution {
}
```
-## python
+### Python
-### 0112.路径总和
+0112.路径总和
(版本一) 递归
```python
@@ -618,7 +620,7 @@ class Solution:
-### 0113.路径总和-ii
+0113.路径总和-ii
(版本一) 递归
```python
@@ -719,9 +721,9 @@ class Solution:
```
-## go
+### Go
-### 112. 路径总和
+112. 路径总和
```go
//递归法
@@ -746,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool {
}
```
-### 113. 路径总和 II
+113. 路径总和 II
```go
/**
@@ -786,9 +788,9 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) {
}
```
-## javascript
+### Javascript
-### 0112.路径总和
+0112.路径总和
**递归**
@@ -852,7 +854,7 @@ let hasPathSum = function(root, targetSum) {
};
```
-### 0113.路径总和-ii
+0113.路径总和-ii
**递归**
@@ -950,9 +952,9 @@ let pathSum = function(root, targetSum) {
};
```
-## TypeScript
+### TypeScript
-### 0112.路径总和
+0112.路径总和
**递归法:**
@@ -1034,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
};
```
-### 0112.路径总和 ii
+0112.路径总和 ii
**递归法:**
@@ -1070,9 +1072,9 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] {
};
```
-## Swift
+### Swift
-### 0112.路径总和
+0112.路径总和
**递归**
@@ -1141,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool {
}
```
-### 0113.路径总和 II
+0113.路径总和 II
**递归**
@@ -1192,10 +1194,11 @@ func traversal(_ cur: TreeNode?, count: Int) {
}
```
-## C
+### C
-> 0112.路径总和
-> 递归法:
+0112.路径总和
+
+递归法:
```c
bool hasPathSum(struct TreeNode* root, int targetSum){
@@ -1252,7 +1255,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){
}
```
-> 0113.路径总和 II
+0113.路径总和 II
```c
int** ret;
@@ -1317,9 +1320,9 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur
}
```
-## Scala
+### Scala
-### 0112.路径总和
+0112.路径总和
**递归:**
@@ -1369,7 +1372,7 @@ object Solution {
}
```
-### 0113.路径总和 II
+0113.路径总和 II
**递归:**
@@ -1405,9 +1408,9 @@ object Solution {
}
```
-## rust
+### Rust
-### 112.路径总和.md
+0112.路径总和
递归:
@@ -1461,7 +1464,7 @@ impl Solution {
}
```
-### 113.路径总和-ii
+0113.路径总和-ii
```rust
impl Solution {
diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md
index 91f35291..0d2477bd 100644
--- a/problems/0115.不同的子序列.md
+++ b/problems/0115.不同的子序列.md
@@ -157,8 +157,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public int numDistinct(String s, String t) {
@@ -182,7 +182,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
class Solution:
def numDistinct(self, s: str, t: str) -> int:
@@ -200,7 +201,8 @@ class Solution:
return dp[-1][-1]
```
-Python3:
+### Python3:
+
```python
class SolutionDP2:
"""
@@ -234,7 +236,8 @@ class SolutionDP2:
return dp[-1]
```
-Go:
+### Go:
+
```go
func numDistinct(s string, t string) int {
dp:= make([][]int,len(s)+1)
@@ -259,8 +262,8 @@ func numDistinct(s string, t string) int {
}
```
+### Javascript:
-Javascript:
```javascript
const numDistinct = (s, t) => {
let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0));
@@ -283,7 +286,7 @@ const numDistinct = (s, t) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function numDistinct(s: string, t: string): number {
@@ -311,7 +314,7 @@ function numDistinct(s: string, t: string): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -368,7 +371,9 @@ impl Solution {
}
```
+
@@ -244,3 +245,4 @@ class Solution:
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md
index 7ace0723..0c073db5 100644
--- a/problems/0509.斐波那契数.md
+++ b/problems/0509.斐波那契数.md
@@ -32,9 +32,9 @@ F(n) = F(n - 1) + F(n - 2),其中 n > 1
* 0 <= n <= 30
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[手把手带你入门动态规划 | leetcode:509.斐波那契数](https://www.bilibili.com/video/BV1f5411K7mo),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -278,7 +278,7 @@ class Solution:
return self.fib(n - 1) + self.fib(n - 2)
```
-### Go:
+### Go
```Go
func fib(n int) int {
if n < 2 {
@@ -479,3 +479,4 @@ public class Solution
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益! 参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md
index 31bb6822..003ef75a 100644
--- a/problems/0116.填充每个节点的下一个右侧节点指针.md
+++ b/problems/0116.填充每个节点的下一个右侧节点指针.md
@@ -30,7 +30,7 @@ struct Node {

-# 思路
+## 思路
注意题目提示内容,:
* 你只能使用常量级额外空间。
@@ -38,7 +38,7 @@ struct Node {
基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。
-## 递归
+### 递归
一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。
@@ -83,7 +83,7 @@ public:
};
```
-## 迭代(层序遍历)
+### 迭代(层序遍历)
本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。
@@ -114,9 +114,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
// 递归法
@@ -169,7 +169,7 @@ class Solution {
}
```
-## Python
+### Python
```python
# 递归法
@@ -210,7 +210,7 @@ class Solution:
nodePre.next = None # 本层最后一个节点指向None
return root
```
-## Go
+### Go
```go
// 迭代法
func connect(root *Node) *Node {
@@ -259,7 +259,7 @@ func connect(root *Node) *Node {
}
```
-## JavaScript
+### JavaScript
```js
const connect = root => {
@@ -287,7 +287,7 @@ const connect = root => {
};
```
-## TypeScript
+### TypeScript
(注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’)
@@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null {
+
diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md
index 06305156..cbdf40e8 100644
--- a/problems/0121.买卖股票的最佳时机.md
+++ b/problems/0121.买卖股票的最佳时机.md
@@ -24,11 +24,9 @@
* 输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
-# 算法公开课
-
-**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
+## 算法公开课
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -202,7 +200,7 @@ public:
## 其他语言版本
-Java:
+### Java:
> 贪心法:
@@ -294,8 +292,7 @@ class Solution {
```
-
-Python:
+### Python:
> 贪心法:
```python
@@ -351,7 +348,8 @@ class Solution:
return dp1
```
-Go:
+### Go:
+
> 贪心法:
```Go
func maxProfit(prices []int) int {
@@ -418,7 +416,7 @@ func max(a, b int) int {
}
```
-JavaScript:
+### JavaScript:
> 动态规划
@@ -454,7 +452,7 @@ var maxProfit = function(prices) {
};
```
-TypeScript:
+### TypeScript:
> 贪心法
@@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number {
};
```
-C#:
+### C#:
> 贪心法
@@ -533,7 +531,7 @@ public class Solution
}
```
-Rust:
+### Rust:
> 贪心
diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md
index 89c654fa..2c2ab225 100644
--- a/problems/0122.买卖股票的最佳时机II.md
+++ b/problems/0122.买卖股票的最佳时机II.md
@@ -37,9 +37,9 @@
- 1 <= prices.length <= 3 \* 10 ^ 4
- 0 <= prices[i] <= 10 ^ 4
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -316,7 +316,7 @@ function maxProfit(prices: number[]): number {
}
```
-### Rust
+### Rust:
贪心:
@@ -389,7 +389,7 @@ int maxProfit(int* prices, int pricesSize){
}
```
-### Scala
+### Scala:
贪心:
@@ -411,3 +411,4 @@ object Solution {
+
diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md
index 02f8d287..6e08b57c 100644
--- a/problems/0122.买卖股票的最佳时机II(动态规划).md
+++ b/problems/0122.买卖股票的最佳时机II(动态规划).md
@@ -34,9 +34,9 @@
* 1 <= prices.length <= 3 * 10 ^ 4
* 0 <= prices[i] <= 10 ^ 4
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -133,8 +133,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
// 动态规划
class Solution
@@ -191,7 +191,7 @@ class Solution {
}
```
-Python:
+### Python:
> 版本一:
```python
@@ -221,7 +221,8 @@ class Solution:
return dp[(length-1) % 2][1]
```
-Go:
+### Go:
+
```go
// 买卖股票的最佳时机Ⅱ 动态规划
// 时间复杂度:O(n) 空间复杂度:O(n)
@@ -250,7 +251,8 @@ func max(a, b int) int {
}
```
-Javascript:
+### JavaScript:
+
```javascript
// 方法一:动态规划(dp 数组)
const maxProfit = (prices) => {
@@ -290,7 +292,7 @@ const maxProfit = (prices) => {
}
```
-TypeScript:
+### TypeScript:
> 动态规划
@@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number {
};
```
-C#:
+### C#:
> 贪心法
@@ -363,7 +365,7 @@ public class Solution
}
```
-Rust:
+### Rust:
> 贪心
@@ -414,4 +416,3 @@ impl Solution {
-
diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md
index a646b7d5..72dd9042 100644
--- a/problems/0123.买卖股票的最佳时机III.md
+++ b/problems/0123.买卖股票的最佳时机III.md
@@ -39,9 +39,9 @@
* 1 <= prices.length <= 10^5
* 0 <= prices[i] <= 10^5
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -221,7 +221,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
// 版本一
@@ -277,7 +277,7 @@ class Solution {
}
```
-Python:
+### Python:
> 版本一:
```python
@@ -314,7 +314,7 @@ class Solution:
return dp[4]
```
-Go:
+### Go:
```go
func maxProfit(prices []int) int {
@@ -344,7 +344,7 @@ func max(a, b int) int {
}
```
-JavaScript:
+### JavaScript:
> 版本一:
@@ -383,7 +383,7 @@ const maxProfit = prices => {
};
```
-TypeScript:
+### TypeScript:
> 版本一
@@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number {
};
```
-Rust:
+### Rust:
> 版本一
@@ -465,3 +465,4 @@ impl Solution {
+
diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md
index 20ad5182..97bc66d0 100644
--- a/problems/0127.单词接龙.md
+++ b/problems/0127.单词接龙.md
@@ -29,7 +29,7 @@
* 解释:endWord "cog" 不在字典中,所以无法进行转换。
-# 思路
+## 思路
以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。
@@ -97,9 +97,9 @@ public:
当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
public int ladderLength(String beginWord, String endWord, List
-
diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md
index 445e108a..ebb36071 100644
--- a/problems/0129.求根到叶子节点数字之和.md
+++ b/problems/0129.求根到叶子节点数字之和.md
@@ -10,7 +10,7 @@
[力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/)
-# 思路
+## 思路
本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。
@@ -24,7 +24,7 @@
那么先按递归三部曲来分析:
-## 递归三部曲
+### 递归三部曲
如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html)
@@ -116,7 +116,7 @@ path.pop_back(); // 回溯
```
**把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。
-## 整体C++代码
+整体C++代码
关键逻辑分析完了,整体C++代码如下:
@@ -162,16 +162,16 @@ public:
};
```
-# 总结
+## 总结
过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。
**我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!**
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```java
class Solution {
@@ -219,7 +219,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
@@ -246,7 +247,7 @@ class Solution:
backtrace(root)
return res
```
-Go:
+### Go:
```go
func sumNumbers(root *TreeNode) int {
@@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) {
-JavaScript:
+### JavaScript:
+
```javascript
var sumNumbers = function(root) {
const listToInt = path => {
@@ -315,7 +317,7 @@ var sumNumbers = function(root) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function sumNumbers(root: TreeNode | null): number {
@@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number {
};
```
-C:
+### C:
```c
//sum记录总和
@@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){
+
diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md
index 92fed58a..ca73e9f7 100644
--- a/problems/0131.分割回文串.md
+++ b/problems/0131.分割回文串.md
@@ -23,12 +23,12 @@
["a","a","b"]
]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
本题这涉及到两个关键问题:
@@ -58,7 +58,7 @@
此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。
-## 回溯三部曲
+### 回溯三部曲
* 递归函数参数
@@ -124,7 +124,7 @@ for (int i = startIndex; i < s.size(); i++) {
**注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1**。
-## 判断回文子串
+### 判断回文子串
最后我们看一下回文子串要如何判断了,判断一个字符串是否是回文。
@@ -147,8 +147,6 @@ for (int i = startIndex; i < s.size(); i++) {
此时关键代码已经讲解完毕,整体代码如下(详细注释了)
-## C++整体代码
-
根据Carl给出的回溯算法模板:
```CPP
@@ -212,7 +210,7 @@ public:
* 时间复杂度: O(n * 2^n)
* 空间复杂度: O(n^2)
-# 优化
+## 优化
上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在:
@@ -272,7 +270,7 @@ public:
```
-# 总结
+## 总结
这道题目在leetcode上是中等,但可以说是hard的题目了,但是代码其实就是按照模板的样子来的。
@@ -306,10 +304,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
class Solution {
List> lists = new ArrayList<>();
@@ -351,7 +349,7 @@ class Solution {
}
```
-## Python
+### Python
回溯 基本版
```python
class Solution:
@@ -473,7 +471,7 @@ class Solution:
return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2))
```
-## Go
+### Go
```go
var (
path []string // 放已经回文的子串
@@ -512,7 +510,7 @@ func isPalindrome(s string) bool {
}
```
-## javaScript
+### JavaScript
```js
/**
@@ -545,7 +543,7 @@ var partition = function(s) {
};
```
-## TypeScript
+### TypeScript
```typescript
function partition(s: string): string[][] {
@@ -582,7 +580,7 @@ function partition(s: string): string[][] {
};
```
-## C
+### C
```c
char** path;
@@ -679,7 +677,7 @@ char*** partition(char* s, int* returnSize, int** returnColumnSizes){
}
```
-## Swift
+### Swift
```swift
func partition(_ s: String) -> [[String]] {
@@ -719,7 +717,7 @@ func partition(_ s: String) -> [[String]] {
}
```
-## Rust
+### Rust
**回溯+函数判断回文串**
```Rust
@@ -808,7 +806,7 @@ impl Solution {
```
-## Scala
+### Scala
```scala
object Solution {
@@ -855,3 +853,4 @@ object Solution {
+
diff --git a/problems/0132.分割回文串II.md b/problems/0132.分割回文串II.md
index 9b164dfb..eb91a189 100644
--- a/problems/0132.分割回文串II.md
+++ b/problems/0132.分割回文串II.md
@@ -34,7 +34,7 @@
* 1 <= s.length <= 2000
* s 仅由小写英文字母组成
-# 思路
+## 思路
我们在讲解回溯法系列的时候,讲过了这道题目[回溯算法:131.分割回文串](https://programmercarl.com/0131.分割回文串.html)。
@@ -201,9 +201,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -257,7 +257,7 @@ class Solution {
}
```
-## Python
+### Python
```python
class Solution:
@@ -286,7 +286,7 @@ class Solution:
return dp[-1]
```
-## Go
+### Go
```go
func minCut(s string) int {
@@ -330,7 +330,7 @@ func min(i, j int) int {
}
```
-## JavaScript
+### JavaScript
```js
var minCut = function(s) {
@@ -376,3 +376,4 @@ var minCut = function(s) {
+
diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md
index ad9acfbc..2f9539e8 100644
--- a/problems/0134.加油站.md
+++ b/problems/0134.加油站.md
@@ -45,12 +45,14 @@
* 解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
-## 暴力方法
+### 暴力方法
暴力的方法很明显就是O(n^2)的,遍历每一个加油站为起点的情况,模拟一圈。
@@ -85,7 +87,7 @@ public:
* 空间复杂度:O(1)
-## 贪心算法(方法一)
+### 贪心算法(方法一)
直接从全局进行贪心选择,情况如下:
@@ -134,7 +136,7 @@ public:
但不管怎么说,解法毕竟还是巧妙的,不用过于执着于其名字称呼。
-## 贪心算法(方法二)
+### 贪心算法(方法二)
可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量rest[i]相加一定是大于等于零的。
@@ -633,3 +635,4 @@ object Solution {
+
diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md
index cf3ccc8e..d130bd68 100644
--- a/problems/0135.分发糖果.md
+++ b/problems/0135.分发糖果.md
@@ -28,9 +28,9 @@
* 输出: 4
* 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -234,7 +234,7 @@ func findMax(num1 int, num2 int) int {
}
```
-### Javascript:
+### Javascript
```Javascript
var candy = function(ratings) {
let candys = new Array(ratings.length).fill(1)
@@ -376,3 +376,4 @@ object Solution {
+
diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md
index 0d88ba36..d93288ae 100644
--- a/problems/0139.单词拆分.md
+++ b/problems/0139.单词拆分.md
@@ -33,9 +33,9 @@
* 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
* 输出: false
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -123,7 +123,7 @@ public:
**这个代码就可以AC了,当然回溯算法不是本题的主菜,背包才是!**
-## 背包问题
+### 背包问题
单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。
@@ -239,7 +239,7 @@ public:
}
};
-```
+```
使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下:
@@ -259,8 +259,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public boolean wordBreak(String s, List
+
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md
index 19ccb725..0c1a526f 100644
--- a/problems/0151.翻转字符串里的单词.md
+++ b/problems/0151.翻转字符串里的单词.md
@@ -433,7 +433,7 @@ class Solution {
}
```
-### python:
+### Python:
(版本一)先删除空白,然后整个反转,最后单词反转。
**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)**
@@ -547,26 +547,28 @@ func reverseWords(s string) string {
b = b[:slowIndex]
}
//2.反转整个字符串
- reverse(&b, 0, len(b)-1)
+ reverse(b)
//3.反转单个单词 i单词开始位置,j单词结束位置
i := 0
for i < len(b) {
j := i
for ; j < len(b) && b[j] != ' '; j++ {
}
- reverse(&b, i, j-1)
+ reverse(b[i:j])
i = j
i++
}
return string(b)
}
-func reverse(b *[]byte, left, right int) {
- for left < right {
- (*b)[left], (*b)[right] = (*b)[right], (*b)[left]
- left++
- right--
- }
+func reverse(b []byte) {
+ left := 0
+ right := len(b) - 1
+ for left < right {
+ b[left], b[right] = b[right], b[left]
+ left++
+ right--
+ }
}
```
@@ -974,4 +976,3 @@ char * reverseWords(char * s){
-
diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md
index 773a910a..e4c5c484 100644
--- a/problems/0188.买卖股票的最佳时机IV.md
+++ b/problems/0188.买卖股票的最佳时机IV.md
@@ -31,9 +31,9 @@
* 0 <= prices.length <= 1000
* 0 <= prices[i] <= 1000
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -173,7 +173,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
// 版本一: 三维 dp数组
@@ -295,7 +295,7 @@ class Solution {
}
```
-Python:
+### Python:
版本一
@@ -329,7 +329,7 @@ class Solution:
dp[j] = max(dp[j],dp[j-1]+prices[i])
return dp[2*k]
```
-Go:
+### Go:
版本一:
@@ -404,7 +404,7 @@ func max188(a, b int) int {
}
```
-Javascript:
+### JavaScript:
```javascript
// 方法一:动态规划
@@ -454,7 +454,7 @@ var maxProfit = function(k, prices) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function maxProfit(k: number, prices: number[]): number {
@@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md
index 35819694..d60612e9 100644
--- a/problems/0189.旋转数组.md
+++ b/problems/0189.旋转数组.md
@@ -33,7 +33,7 @@
向右旋转 2 步: [3,99,-1,-100]。
-# 思路
+## 思路
这道题目在字符串里其实很常见,我把字符串反转相关的题目列一下:
@@ -83,9 +83,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -106,7 +106,7 @@ class Solution {
}
```
-## Python
+### Python
方法一:局部翻转 + 整体翻转
```python
@@ -139,7 +139,7 @@ class Solution:
# 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。
```
-## Go
+### Go
```go
func rotate(nums []int, k int) {
@@ -157,7 +157,7 @@ func reverse(nums []int){
}
```
-## JavaScript
+### JavaScript
```js
var rotate = function (nums, k) {
@@ -178,7 +178,7 @@ var rotate = function (nums, k) {
};
```
-## TypeScript
+### TypeScript
```typescript
function rotate(nums: number[], k: number): void {
@@ -205,3 +205,4 @@ function reverseByRange(nums: number[], left: number, right: number): void {
+
diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md
index 80902559..a7bc4c99 100644
--- a/problems/0198.打家劫舍.md
+++ b/problems/0198.打家劫舍.md
@@ -31,9 +31,9 @@
* 0 <= nums.length <= 100
* 0 <= nums[i] <= 400
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -121,8 +121,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
// 动态规划
class Solution {
@@ -194,7 +194,7 @@ class Solution {
}
```
-Python:
+### Python:
1维DP
```python
@@ -255,7 +255,8 @@ class Solution:
```
-Go:
+### Go:
+
```Go
func rob(nums []int) int {
n := len(nums)
@@ -275,7 +276,7 @@ func max(a, b int) int {
}
```
-JavaScript:
+### JavaScript:
```javascript
const rob = nums => {
@@ -291,7 +292,7 @@ const rob = nums => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function rob(nums: number[]): number {
@@ -314,7 +315,7 @@ function rob(nums: number[]): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -338,4 +339,3 @@ impl Solution {
-
diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md
index c20fe4f1..5b9d90aa 100644
--- a/problems/0200.岛屿数量.广搜版.md
+++ b/problems/0200.岛屿数量.广搜版.md
@@ -197,7 +197,7 @@ class Solution {
}
```
-## 其他语言版本
+
### Python
BFS solution
```python
@@ -237,6 +237,7 @@ class Solution:
continue
q.append((next_i, next_j))
visited[next_i][next_j] = True
+
```
```
+
diff --git a/problems/0200.岛屿数量.深搜版.md b/problems/0200.岛屿数量.深搜版.md
index c30ace19..f610e323 100644
--- a/problems/0200.岛屿数量.深搜版.md
+++ b/problems/0200.岛屿数量.深搜版.md
@@ -218,6 +218,67 @@ class Solution {
}
}
```
+
+Python:
+
+```python
+# 版本一
+class Solution:
+ def numIslands(self, grid: List[List[str]]) -> int:
+ m, n = len(grid), len(grid[0])
+ visited = [[False] * n for _ in range(m)]
+ dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向
+ result = 0
+
+ def dfs(x, y):
+ for d in dirs:
+ nextx = x + d[0]
+ nexty = y + d[1]
+ if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过
+ continue
+ if not visited[nextx][nexty] and grid[nextx][nexty] == '1': # 没有访问过的同时是陆地的
+ visited[nextx][nexty] = True
+ dfs(nextx, nexty)
+
+ for i in range(m):
+ for j in range(n):
+ if not visited[i][j] and grid[i][j] == '1':
+ visited[i][j] = True
+ result += 1 # 遇到没访问过的陆地,+1
+ dfs(i, j) # 将与其链接的陆地都标记上 true
+
+ return result
+```
+
+```python
+# 版本二
+class Solution:
+ def numIslands(self, grid: List[List[str]]) -> int:
+ m, n = len(grid), len(grid[0])
+ visited = [[False] * n for _ in range(m)]
+ dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 四个方向
+ result = 0
+
+ def dfs(x, y):
+ if visited[x][y] or grid[x][y] == '0':
+ return # 终止条件:访问过的节点 或者 遇到海水
+ visited[x][y] = True
+ for d in dirs:
+ nextx = x + d[0]
+ nexty = y + d[1]
+ if nextx < 0 or nextx >= m or nexty < 0 or nexty >= n: # 越界了,直接跳过
+ continue
+ dfs(nextx, nexty)
+
+ for i in range(m):
+ for j in range(n):
+ if not visited[i][j] and grid[i][j] == '1':
+ result += 1 # 遇到没访问过的陆地,+1
+ dfs(i, j) # 将与其链接的陆地都标记上 true
+
+ return result
+```
+
diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md
index a507638c..e07ab746 100644
--- a/problems/0205.同构字符串.md
+++ b/problems/0205.同构字符串.md
@@ -29,7 +29,7 @@
提示:可以假设 s 和 t 长度相同。
-# 思路
+## 思路
字符串没有说都是小写字母之类的,所以用数组不合适了,用map来做映射。
@@ -61,9 +61,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -87,7 +87,7 @@ class Solution {
}
```
-## Python
+### Python
```python
class Solution:
@@ -110,7 +110,7 @@ class Solution:
return True
```
-## Go
+### Go
```go
func isIsomorphic(s string, t string) bool {
@@ -132,7 +132,7 @@ func isIsomorphic(s string, t string) bool {
}
```
-## JavaScript
+### JavaScript
```js
var isIsomorphic = function(s, t) {
@@ -156,7 +156,7 @@ var isIsomorphic = function(s, t) {
};
```
-## TypeScript
+### TypeScript
```typescript
function isIsomorphic(s: string, t: string): boolean {
@@ -183,3 +183,4 @@ function isIsomorphic(s: string, t: string): boolean {
+
diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md
index ee62b574..cd9d596d 100644
--- a/problems/0213.打家劫舍II.md
+++ b/problems/0213.打家劫舍II.md
@@ -31,9 +31,9 @@
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 1000
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -104,8 +104,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public int rob(int[] nums) {
@@ -129,7 +129,7 @@ class Solution {
}
```
-Python:
+### Python:
```Python
class Solution:
@@ -219,7 +219,7 @@ class Solution:
```
-Go:
+### Go:
```go
// 打家劫舍Ⅱ 动态规划
@@ -257,7 +257,8 @@ func max(a, b int) int {
}
```
-javascipt:
+### JavaScript:
+
```javascript
var rob = function(nums) {
const n = nums.length
@@ -279,7 +280,7 @@ const robRange = (nums, start, end) => {
return dp[end]
}
```
-TypeScript:
+### TypeScript:
```typescript
function rob(nums: number[]): number {
@@ -301,7 +302,7 @@ function robRange(nums: number[], start: number, end: number): number {
}
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -336,3 +337,4 @@ impl Solution {
+
diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md
index 319b2eba..4de7dc58 100644
--- a/problems/0216.组合总和III.md
+++ b/problems/0216.组合总和III.md
@@ -7,8 +7,6 @@
-
-
> 别看本篇选的是组合总和III,而不是组合总和,本题和上一篇77.组合相比难度刚刚好!
# 216.组合总和III
@@ -30,12 +28,12 @@
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
本题就是在[1,2,3,4,5,6,7,8,9]这个集合中找到和为n的k个数的组合。
@@ -54,7 +52,7 @@
图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。
-## 回溯三部曲
+### 回溯三部曲
* **确定递归函数参数**
@@ -165,7 +163,7 @@ public:
};
```
-## 剪枝
+### 剪枝
这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。
@@ -238,7 +236,7 @@ public:
* 时间复杂度: O(n * 2^n)
* 空间复杂度: O(n)
-# 总结
+## 总结
开篇就介绍了本题与[77.组合](https://programmercarl.com/0077.组合.html)的区别,相对来说加了元素总和的限制,如果做完[77.组合](https://programmercarl.com/0077.组合.html)再做本题在合适不过。
@@ -249,10 +247,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
模板方法
@@ -358,7 +356,7 @@ class Solution {
}
```
-## Python
+### Python
```py
class Solution:
@@ -383,7 +381,7 @@ class Solution:
```
-## Go
+### Go
回溯+减枝
@@ -418,7 +416,7 @@ func dfs(k, n int, start int, sum int) {
}
```
-## javaScript
+### JavaScript
```js
/**
@@ -455,7 +453,7 @@ var combinationSum3 = function(k, n) {
};
```
-## TypeScript
+### TypeScript
```typescript
function combinationSum3(k: number, n: number): number[][] {
@@ -479,7 +477,7 @@ function combinationSum3(k: number, n: number): number[][] {
};
```
-## Rust
+### Rust
```Rust
impl Solution {
@@ -516,7 +514,7 @@ impl Solution {
}
```
-## C
+### C
```c
int* path;
@@ -575,7 +573,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){
}
```
-## Swift
+### Swift
```swift
func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
@@ -607,7 +605,7 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
}
```
-## Scala
+### Scala
```scala
object Solution {
diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md
index 18b397e3..fef942fc 100644
--- a/problems/0234.回文链表.md
+++ b/problems/0234.回文链表.md
@@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null {
+
diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md
index 9777bb0b..2b8af060 100644
--- a/problems/0235.二叉搜索树的最近公共祖先.md
+++ b/problems/0235.二叉搜索树的最近公共祖先.md
@@ -36,11 +36,11 @@
* 所有节点的值都是唯一的。
* p、q 为不同节点且均存在于给定的二叉搜索树中。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。
@@ -71,7 +71,7 @@
可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回!
-## 递归法
+### 递归法
递归三部曲如下:
@@ -203,7 +203,7 @@ public:
};
```
-## 迭代法
+### 迭代法
对于二叉搜索树的迭代法,大家应该在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)就了解了。
@@ -229,7 +229,7 @@ public:
灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕?
-# 总结
+## 总结
对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。
@@ -238,10 +238,10 @@ public:
最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
递归法:
```java
@@ -273,7 +273,7 @@ class Solution {
```
-## Python
+### Python
递归法(版本一)
```python
@@ -326,7 +326,7 @@ class Solution:
```
-## Go
+### Go
递归法:
```go
@@ -350,7 +350,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
```
-## JavaScript
+### JavaScript
递归法:
```javascript
@@ -391,7 +391,7 @@ var lowestCommonAncestor = function(root, p, q) {
};
```
-## TypeScript
+### TypeScript
> 递归法:
@@ -422,7 +422,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
-## Scala
+### Scala
递归:
@@ -453,7 +453,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -519,3 +519,4 @@ impl Solution {
+
diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md
index 0ebd5566..9db7409e 100644
--- a/problems/0236.二叉树的最近公共祖先.md
+++ b/problems/0236.二叉树的最近公共祖先.md
@@ -34,12 +34,12 @@
* 所有节点的值都是唯一的。
* p、q 为不同节点且均存在于给定的二叉树中。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
遇到这个题目首先想的是要是能自底向上查找就好了,这样就可以找到公共祖先了。
@@ -226,7 +226,7 @@ public:
};
```
-# 总结
+## 总结
这道题目刷过的同学未必真正了解这里面回溯的过程,以及结果是如何一层一层传上去的。
@@ -243,10 +243,10 @@ public:
本题没有给出迭代法,因为迭代法不适合模拟回溯的过程。理解递归的解法就够了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
class Solution {
@@ -273,7 +273,7 @@ class Solution {
```
-## Python
+### Python
递归法(版本一)
```python
class Solution:
@@ -312,7 +312,7 @@ class Solution:
return left
```
-## Go
+### Go
```Go
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
@@ -343,7 +343,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
}
```
-## JavaScript
+### JavaScript
```javascript
var lowestCommonAncestor = function(root, p, q) {
@@ -370,7 +370,7 @@ var lowestCommonAncestor = function(root, p, q) {
};
```
-## TypeScript
+### TypeScript
```typescript
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
@@ -384,7 +384,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
-## Scala
+### Scala
```scala
object Solution {
@@ -404,7 +404,7 @@ object Solution {
}
```
-## rust
+### Rust
```rust
impl Solution {
@@ -436,3 +436,4 @@ impl Solution {
+
diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md
index 70ab8649..a0c138ee 100644
--- a/problems/0279.完全平方数.md
+++ b/problems/0279.完全平方数.md
@@ -28,9 +28,9 @@
提示:
* 1 <= n <= 10^4
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -106,8 +106,6 @@ dp[5] = min(dp[4] + 1, dp[1] + 1) = 2
最后的dp[n]为最终结果。
-## C++代码
-
以上动规五部曲分析完毕C++代码如下:
```CPP
@@ -165,8 +163,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
// 版本一,先遍历物品, 再遍历背包
@@ -219,7 +217,7 @@ class Solution {
}
```
-Python:
+### Python:
先遍历物品, 再遍历背包
```python
@@ -276,7 +274,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
// 版本一,先遍历物品, 再遍历背包
func numSquares1(n int) int {
@@ -327,7 +326,8 @@ func min(a, b int) int {
}
```
-Javascript:
+### Javascript:
+
```Javascript
// 先遍历物品,再遍历背包
var numSquares1 = function(n) {
@@ -357,7 +357,7 @@ var numSquares2 = function(n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
// 先遍历物品
@@ -389,7 +389,7 @@ function numSquares(n: number): number {
};
```
-Rust:
+### Rust:
```rust
// 先遍历背包
@@ -439,3 +439,4 @@ impl Solution {
+
diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md
index 22d6428c..42232cc0 100644
--- a/problems/0283.移动零.md
+++ b/problems/0283.移动零.md
@@ -4,9 +4,7 @@
+
diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md
index c58c3bf6..677c72c6 100644
--- a/problems/0300.最长上升子序列.md
+++ b/problems/0300.最长上升子序列.md
@@ -33,7 +33,7 @@
## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划之子序列问题,元素不连续!| LeetCode:300.最长递增子序列](https://www.bilibili.com/video/BV1ng411J7xP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -124,30 +124,28 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
+ int res = 0;
Arrays.fill(dp, 1);
- for (int i = 0; i < dp.length; i++) {
+ for (int i = 1; i < dp.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
+ res = Math.max(res, dp[i]);
}
}
- int res = 0;
- for (int i = 0; i < dp.length; i++) {
- res = Math.max(res, dp[i]);
- }
return res;
}
}
```
-Python:
+### Python:
DP
```python
@@ -189,7 +187,8 @@ class Solution:
return len(tails) # 返回递增子序列的长度
```
-Go:
+### Go:
+
```go
// 动态规划求解
func lengthOfLIS(nums []int) int {
@@ -248,7 +247,8 @@ func lengthOfLIS(nums []int ) int {
}
```
-Javascript
+### Javascript:
+
```javascript
const lengthOfLIS = (nums) => {
let dp = Array(nums.length).fill(1);
@@ -267,7 +267,7 @@ const lengthOfLIS = (nums) => {
};
```
-TypeScript
+### TypeScript:
```typescript
function lengthOfLIS(nums: number[]): number {
@@ -288,10 +288,11 @@ function lengthOfLIS(nums: number[]): number {
};
```
-Rust:
+### Rust:
+
```rust
pub fn length_of_lis(nums: Vec
+
diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md
index cd71136b..f4093b67 100644
--- a/problems/0309.最佳买卖股票时机含冷冻期.md
+++ b/problems/0309.最佳买卖股票时机含冷冻期.md
@@ -20,9 +20,9 @@
* 输出: 3
* 解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次有冷冻期!| LeetCode:309.买卖股票的最佳时机含冷冻期](https://www.bilibili.com/video/BV1rP4y1D7ku),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -174,7 +174,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
class Solution {
@@ -273,8 +273,9 @@ class Solution {
}
```
-Python:
+### Python:
版本一
+
```python
from typing import List
@@ -319,7 +320,8 @@ class Solution:
return max(dp[-1][1], dp[-1][2])
```
-Go:
+### Go:
+
```go
// 最佳买卖股票时机含冷冻期 动态规划
// 时间复杂度O(n) 空间复杂度O(n)
@@ -355,7 +357,7 @@ func max(a, b int) int {
}
```
-Javascript:
+### Javascript:
```javascript
const maxProfit = (prices) => {
@@ -397,7 +399,7 @@ const maxProfit = (prices) => {
};
```
-TypeScript:
+### TypeScript:
> 版本一,与本文思路一致
@@ -455,7 +457,7 @@ function maxProfit(prices: number[]): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -484,3 +486,4 @@ impl Solution {
+
diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md
index 1f3f4df2..f32fd13e 100644
--- a/problems/0322.零钱兑换.md
+++ b/problems/0322.零钱兑换.md
@@ -39,9 +39,9 @@
* 1 <= coins[i] <= 2^31 - 1
* 0 <= amount <= 10^4
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
@@ -110,7 +110,6 @@ dp[0] = 0;
dp[amount]为最终结果。
-## C++代码
以上分析完毕,C++ 代码如下:
```CPP
@@ -187,8 +186,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public int coinChange(int[] coins, int amount) {
@@ -215,7 +214,7 @@ class Solution {
}
```
-Python:
+### Python:
先遍历物品 后遍历背包
@@ -288,7 +287,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
// 版本一, 先遍历物品,再遍历背包
func coinChange1(coins []int, amount int) int {
@@ -352,7 +352,7 @@ func min(a, b int) int {
```
-Rust:
+### Rust:
```rust
// 遍历物品
@@ -398,7 +398,8 @@ impl Solution {
}
```
-Javascript:
+### Javascript:
+
```javascript
// 遍历物品
const coinChange = (coins, amount) => {
@@ -435,7 +436,7 @@ var coinChange = function(coins, amount) {
}
```
-TypeScript:
+### TypeScript:
```typescript
// 遍历物品
@@ -473,3 +474,4 @@ function coinChange(coins: number[], amount: number): number {
+
diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md
index fa6414e9..2795e313 100644
--- a/problems/0332.重新安排行程.md
+++ b/problems/0332.重新安排行程.md
@@ -29,9 +29,11 @@
* 输出:["JFK","ATL","JFK","SFO","ATL","SFO"]
* 解释:另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"]。但是它自然排序更大更靠后。
-## 思路
+## 算法公开课
-**如果对回溯算法基础还不了解的话,我还特意录制了一期视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) ,相信结合视频再看本篇题解,更有助于大家对本题的理解。**
+
+## 思路
这道题目还是很难的,之前我们用回溯法解决了如下问题:[组合问题](https://programmercarl.com/0077.组合.html),[分割问题](https://programmercarl.com/0093.复原IP地址.html),[子集问题](https://programmercarl.com/0078.子集.html),[排列问题](https://programmercarl.com/0046.全排列.html)。
@@ -53,7 +55,7 @@
针对以上问题我来逐一解答!
-## 如何理解死循环
+### 如何理解死循环
对于死循环,我来举一个有重复机场的例子:
@@ -61,7 +63,7 @@
为什么要举这个例子呢,就是告诉大家,出发机场和到达机场也会重复的,**如果在解题的过程中没有对集合元素处理好,就会死循环。**
-## 该记录映射关系
+### 该记录映射关系
有多种解法,字母序靠前排在前面,让很多同学望而退步,如何该记录映射关系呢 ?
@@ -90,7 +92,7 @@ unordered_map
+
diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md
index 3ff8dedb..cba82f6c 100644
--- a/problems/0343.整数拆分.md
+++ b/problems/0343.整数拆分.md
@@ -22,9 +22,9 @@
* 说明: 你可以假设 n 不小于 2 且不大于 58。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,本题关键在于理解递推公式!| LeetCode:343. 整数拆分](https://www.bilibili.com/video/BV1Mg411q7YJ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -473,3 +473,4 @@ object Solution {
+
diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md
index 08de23ae..943dfe39 100644
--- a/problems/0376.摆动序列.md
+++ b/problems/0376.摆动序列.md
@@ -33,11 +33,13 @@
- 输入: [1,2,3,4,5,6,7,8,9]
- 输出: 2
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找摆动有细节!| LeetCode:376.摆动序列](https://www.bilibili.com/video/BV17M411b7NS),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-## 思路 1(贪心解法)
+## 思路
+
+### 思路 1(贪心解法)
本题要求通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。
@@ -69,7 +71,7 @@
2. 情况二:数组首尾两端
3. 情况三:单调坡中有平坡
-### 情况一:上下坡中有平坡
+#### 情况一:上下坡中有平坡
例如 [1,2,2,2,1]这样的数组,如图:
@@ -87,7 +89,7 @@
所以我们记录峰值的条件应该是: `(preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)`,为什么这里允许 prediff == 0 ,就是为了 上面我说的这种情况。
-### 情况二:数组首尾两端
+#### 情况二:数组首尾两端
所以本题统计峰值的时候,数组最左面和最右面如何统计呢?
@@ -142,7 +144,7 @@ public:
所以此时我们要讨论情况三!
-### 情况三:单调坡度有平坡
+#### 情况三:单调坡度有平坡
在版本一中,我们忽略了一种情况,即 如果在一个单调坡度上有平坡,例如[1,2,2,2,3,4],如图:
@@ -187,7 +189,7 @@ public:

-## 思路 2(动态规划)
+### 思路 2(动态规划)
考虑用动态规划的思想来解决这个问题。
@@ -696,4 +698,3 @@ object Solution {
-
diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md
index bd43d526..d9699c54 100644
--- a/problems/0377.组合总和Ⅳ.md
+++ b/problems/0377.组合总和Ⅳ.md
@@ -31,9 +31,9 @@
因此输出为 7。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -154,8 +154,7 @@ C++测试用例有两个数相加超过int的数据,所以需要在if里加上
## 其他语言版本
-
-Java:
+### Java:
```Java
class Solution {
@@ -174,7 +173,7 @@ class Solution {
}
```
-Python:
+### Python:
卡哥版
@@ -207,7 +206,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
func combinationSum4(nums []int, target int) int {
//定义dp数组
@@ -226,7 +226,8 @@ func combinationSum4(nums []int, target int) int {
}
```
-Javascript:
+### Javascript:
+
```javascript
const combinationSum4 = (nums, target) => {
@@ -245,7 +246,7 @@ const combinationSum4 = (nums, target) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function combinationSum4(nums: number[], target: number): number {
@@ -264,7 +265,7 @@ function combinationSum4(nums: number[], target: number): number {
};
```
-Rust
+### Rust:
```Rust
impl Solution {
@@ -289,3 +290,4 @@ impl Solution {
+
diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md
index c10114c0..12d3fa48 100644
--- a/problems/0392.判断子序列.md
+++ b/problems/0392.判断子序列.md
@@ -28,9 +28,9 @@
两个字符串都只由小写字符组成。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,用相似思路解决复杂问题 | LeetCode:392.判断子序列](https://www.bilibili.com/video/BV1tv4y1B7ym/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -149,8 +149,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public boolean isSubsequence(String s, String t) {
@@ -174,7 +174,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
@@ -190,7 +191,7 @@ class Solution:
return False
```
-JavaScript:
+### JavaScript:
```javascript
const isSubsequence = (s, t) => {
@@ -213,7 +214,7 @@ const isSubsequence = (s, t) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function isSubsequence(s: string, t: string): boolean {
@@ -237,7 +238,7 @@ function isSubsequence(s: string, t: string): boolean {
};
```
-Go:
+### Go:
```go
func isSubsequence(s string, t string) bool {
@@ -258,11 +259,52 @@ func isSubsequence(s string, t string) bool {
}
```
+Rust:
+```rust
+impl Solution {
+ pub fn is_subsequence(s: String, t: String) -> bool {
+ let mut dp = vec![vec![0; t.len() + 1]; s.len() + 1];
+ for (i, char_s) in s.chars().enumerate() {
+ for (j, char_t) in t.chars().enumerate() {
+ if char_s == char_t {
+ dp[i + 1][j + 1] = dp[i][j] + 1;
+ continue;
+ }
+ dp[i + 1][j + 1] = dp[i + 1][j]
+ }
+ }
+ dp[s.len()][t.len()] == s.len()
+ }
+}
+```
+> 滚动数组
+```rust
+impl Solution {
+ pub fn is_subsequence(s: String, t: String) -> bool {
+ let mut dp = vec![0; t.len() + 1];
+ let (s, t) = (s.as_bytes(), t.as_bytes());
+ for &byte_s in s {
+ let mut pre = 0;
+ for j in 0..t.len() {
+ let temp = dp[j + 1];
+ if byte_s == t[j] {
+ dp[j + 1] = pre + 1;
+ } else {
+ dp[j + 1] = dp[j];
+ }
+ pre = temp;
+ }
+ }
+ dp[t.len()] == s.len()
+ }
+}
+```
+
diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md
index aa2868df..c1ad602d 100644
--- a/problems/0404.左叶子之和.md
+++ b/problems/0404.左叶子之和.md
@@ -16,9 +16,9 @@

-## 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -48,7 +48,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL)
}
```
-## 递归法
+### 递归法
递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。
@@ -131,11 +131,11 @@ public:
return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};
-```
+```
精简之后的代码其实看不出来用的是什么遍历方式了,对于算法初学者以上根据第一个版本来学习。
-## 迭代法
+### 迭代法
本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:迭代法统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中的写法,可以写出一个前序遍历的迭代法。
@@ -177,7 +177,7 @@ public:
## 其他语言版本
-### Java
+### Java:
**递归**
@@ -246,7 +246,7 @@ class Solution {
```
-### Python
+### Python:
递归
```python
# Definition for a binary tree node.
@@ -316,7 +316,7 @@ class Solution:
```
-### Go
+### Go:
**递归法**
@@ -368,7 +368,7 @@ func sumOfLeftLeaves(root *TreeNode) int {
```
-### JavaScript
+### JavaScript:
**递归法**
@@ -417,7 +417,7 @@ var sumOfLeftLeaves = function(root) {
};
```
-### TypeScript
+### TypeScript:
> 递归法
@@ -462,7 +462,7 @@ function sumOfLeftLeaves(root: TreeNode | null): number {
};
```
-### Swift
+### Swift:
**递归法**
```swift
@@ -511,7 +511,7 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int {
}
```
-### C
+### C:
递归法:
```c
int sumOfLeftLeaves(struct TreeNode* root){
@@ -561,7 +561,7 @@ int sumOfLeftLeaves(struct TreeNode* root){
}
```
-### Scala
+### Scala:
**递归:**
```scala
@@ -600,7 +600,7 @@ object Solution {
}
```
-### Rust
+### Rust:
**递归**
@@ -656,3 +656,4 @@ impl Solution {
+
diff --git a/problems/0406.根据身高重建队列.md b/problems/0406.根据身高重建队列.md
index 60d1fcab..9cd78fac 100644
--- a/problems/0406.根据身高重建队列.md
+++ b/problems/0406.根据身高重建队列.md
@@ -37,9 +37,9 @@
题目数据确保队列可以被重建
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列](https://www.bilibili.com/video/BV1EA411675Y),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -402,3 +402,4 @@ object Solution {
+
diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md
index a886b99a..2b2be103 100644
--- a/problems/0416.分割等和子集.md
+++ b/problems/0416.分割等和子集.md
@@ -4,7 +4,7 @@
+
diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md
index 3d73598d..18e5cb4c 100644
--- a/problems/0450.删除二叉搜索树中的节点.md
+++ b/problems/0450.删除二叉搜索树中的节点.md
@@ -24,15 +24,15 @@

-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。
-## 递归
+### 递归
递归三部曲:
@@ -161,7 +161,7 @@ public:
};
```
-## 普通二叉树的删除方式
+### 普通二叉树的删除方式
这里我在介绍一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。
@@ -198,7 +198,7 @@ public:
这个代码是简短一些,思路也巧妙,但是不太好想,实操性不强,推荐第一种写法!
-## 迭代法
+### 迭代法
删除节点的迭代法还是复杂一些的,但其本质我在递归法里都介绍了,最关键就是删除节点的操作(动画模拟的过程)
@@ -246,7 +246,7 @@ public:
};
```
-# 总结
+## 总结
读完本篇,大家会发现二叉搜索树删除节点比增加节点复杂的多。
@@ -264,10 +264,38 @@ public:
迭代法其实不太容易写出来,所以如果是初学者的话,彻底掌握第一种递归写法就够了。
-# 其他语言版本
+## 其他语言版本
+
+
+### Java
+```java
+// 解法1(最好理解的版本)
+class Solution {
+ public TreeNode deleteNode(TreeNode root, int key) {
+ if (root == null) return root;
+ if (root.val == key) {
+ if (root.left == null) {
+ return root.right;
+ } else if (root.right == null) {
+ return root.left;
+ } else {
+ TreeNode cur = root.right;
+ while (cur.left != null) {
+ cur = cur.left;
+ }
+ cur.left = root.left;
+ root = root.right;
+ return root;
+ }
+ }
+ if (root.val > key) root.left = deleteNode(root.left, key);
+ if (root.val < key) root.right = deleteNode(root.right, key);
+ return root;
+ }
+}
+```
-## Java
```java
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
@@ -296,34 +324,59 @@ class Solution {
}
}
```
+递归法
```java
-// 解法2
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
- if (root == null) return root;
- if (root.val == key) {
- if (root.left == null) {
- return root.right;
- } else if (root.right == null) {
- return root.left;
- } else {
- TreeNode cur = root.right;
- while (cur.left != null) {
- cur = cur.left;
- }
- cur.left = root.left;
- root = root.right;
- return root;
+ if (root == null){
+ return null;
+ }
+ //寻找对应的对应的前面的节点,以及他的前一个节点
+ TreeNode cur = root;
+ TreeNode pre = null;
+ while (cur != null){
+ if (cur.val < key){
+ pre = cur;
+ cur = cur.right;
+ } else if (cur.val > key) {
+ pre = cur;
+ cur = cur.left;
+ }else {
+ break;
}
}
- if (root.val > key) root.left = deleteNode(root.left, key);
- if (root.val < key) root.right = deleteNode(root.right, key);
+ if (pre == null){
+ return deleteOneNode(cur);
+ }
+ if (pre.left !=null && pre.left.val == key){
+ pre.left = deleteOneNode(cur);
+ }
+ if (pre.right !=null && pre.right.val == key){
+ pre.right = deleteOneNode(cur);
+ }
return root;
}
+
+ public TreeNode deleteOneNode(TreeNode node){
+ if (node == null){
+ return null;
+ }
+ if (node.right == null){
+ return node.left;
+ }
+ TreeNode cur = node.right;
+ while (cur.left !=null){
+ cur = cur.left;
+ }
+ cur.left = node.left;
+ return node.right;
+ }
}
```
-## Python
+
+### Python
+
递归法(版本一)
```python
class Solution:
@@ -411,7 +464,7 @@ class Solution:
return root
```
-## Go
+### Go
```Go
// 递归版本
func deleteNode(root *TreeNode, key int) *TreeNode {
@@ -497,7 +550,7 @@ func deleteNode(root *TreeNode, key int) *TreeNode {
}
```
-## JavaScript
+### JavaScript
递归
@@ -588,7 +641,7 @@ var deleteNode = function (root, key) {
}
```
-## TypeScript
+### TypeScript
> 递归法:
@@ -652,7 +705,7 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
};
```
-## Scala
+### Scala
```scala
object Solution {
@@ -682,7 +735,7 @@ object Solution {
}
```
-## rust
+### Rust
```rust
impl Solution {
@@ -720,3 +773,4 @@ impl Solution {
+
diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md
index ff476f41..90cd7085 100644
--- a/problems/0452.用最少数量的箭引爆气球.md
+++ b/problems/0452.用最少数量的箭引爆气球.md
@@ -42,9 +42,9 @@
* points[i].length == 2
* -2^31 <= xstart < xend <= 2^31 - 1
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,判断重叠区间问题 | LeetCode:452.用最少数量的箭引爆气球](https://www.bilibili.com/video/BV1SA41167xe),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md
index ce1987ef..c9c1a852 100644
--- a/problems/0455.分发饼干.md
+++ b/problems/0455.分发饼干.md
@@ -30,9 +30,9 @@
- 0 <= s.length <= 3 \* 10^4
- 1 <= g[i], s[j] <= 2^31 - 1
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,你想先喂哪个小孩?| LeetCode:455.分发饼干](https://www.bilibili.com/video/BV1MM411b7cq),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0463.岛屿的周长.md b/problems/0463.岛屿的周长.md
index 18f1d01e..14fa98dc 100644
--- a/problems/0463.岛屿的周长.md
+++ b/problems/0463.岛屿的周长.md
@@ -90,7 +90,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
// 解法一
@@ -191,8 +191,8 @@ class Solution {
```
-Python:
-### 解法1:
+### Python:
+
扫描每个cell,如果当前位置为岛屿 grid[i][j] == 1, 从当前位置判断四边方向,如果边界或者是水域,证明有边界存在,res矩阵的对应cell加一。
```python
@@ -228,7 +228,8 @@ class Solution:
```
-Go:
+### Go:
+
```go
func islandPerimeter(grid [][]int) int {
m, n := len(grid), len(grid[0])
@@ -249,7 +250,8 @@ func islandPerimeter(grid [][]int) int {
}
```
-JavaScript:
+### JavaScript:
+
```javascript
//解法一
var islandPerimeter = function(grid) {
@@ -305,3 +307,4 @@ var islandPerimeter = function(grid) {
+
diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md
index 7c1206ef..8f6197ac 100644
--- a/problems/0474.一和零.md
+++ b/problems/0474.一和零.md
@@ -34,9 +34,9 @@
* strs[i] 仅由 '0' 和 '1' 组成
* 1 <= m, n <= 100
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满这个背包最多用多少个物品?| LeetCode:474.一和零](https://www.bilibili.com/video/BV1rW4y1x7ZQ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -538,3 +538,4 @@ impl Solution {
+
diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md
index 4b21008e..e2011372 100644
--- a/problems/0491.递增子序列.md
+++ b/problems/0491.递增子序列.md
@@ -23,9 +23,9 @@
* 数组中的整数范围是 [-100,100]。
* 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列](https://www.bilibili.com/video/BV1EG4y1h78v/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法精讲,树层去重与树枝去重 | LeetCode:491.递增子序列](https://www.bilibili.com/video/BV1EG4y1h78v/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -619,4 +619,3 @@ object Solution {
-
diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md
index 1902d5ed..4a4e966c 100644
--- a/problems/0494.目标和.md
+++ b/problems/0494.目标和.md
@@ -37,9 +37,9 @@
* 初始的数组的和不会超过 1000 。
* 保证返回的最终结果能被 32 位整数存下。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?| LeetCode:494.目标和](https://www.bilibili.com/video/BV1o8411j73x/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -67,7 +67,7 @@ target是固定的,sum是固定的,left就可以求出来。
此时问题就是在集合nums中找出和为left的组合。
-## 回溯算法
+### 回溯算法
在回溯算法系列中,一起学过这道题目[回溯算法:39. 组合总和](https://programmercarl.com/0039.组合总和.html)的录友应该感觉很熟悉,这不就是组合总和问题么?
@@ -118,7 +118,7 @@ public:
也可以使用记忆化回溯,但这里我就不在回溯上下功夫了,直接看动规吧
-## 动态规划
+### 动态规划
如何转化为01背包问题呢。
@@ -519,8 +519,6 @@ const findTargetSumWays = (nums, target) => {
### TypeScript
-TypeScript:
-
```ts
function findTargetSumWays(nums: number[], target: number): number {
// 把数组分成两个组合left, right.left + right = sum, left - right = target.
@@ -590,3 +588,4 @@ impl Solution {
+
diff --git a/problems/0496.下一个更大元素I.md b/problems/0496.下一个更大元素I.md
index 31c3ce43..6bcafafb 100644
--- a/problems/0496.下一个更大元素I.md
+++ b/problems/0496.下一个更大元素I.md
@@ -37,7 +37,7 @@ nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位
* nums1和nums2中所有整数 互不相同
* nums1 中的所有整数同样出现在 nums2 中
-# 思路
+## 思路
做本题之前,建议先做一下[739. 每日温度](https://programmercarl.com/0739.每日温度.html)
@@ -191,7 +191,8 @@ public:
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
## 其他语言版本
-Java
+### Java
+
```java
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
@@ -248,7 +249,8 @@ class Solution {
}
}
```
-Python3:
+### Python3
+
```python
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
@@ -269,7 +271,7 @@ class Solution:
return result
```
-Go:
+### Go
> 未精简版本
```go
@@ -335,7 +337,7 @@ func nextGreaterElement(nums1 []int, nums2 []int) []int {
}
```
-JavaScript:
+### JavaScript
```JS
var nextGreaterElement = function (nums1, nums2) {
@@ -358,7 +360,7 @@ var nextGreaterElement = function (nums1, nums2) {
};
```
-TypeScript:
+### TypeScript
```typescript
function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
@@ -387,9 +389,36 @@ function nextGreaterElement(nums1: number[], nums2: number[]): number[] {
};
```
+### Rust
+
+```rust
+impl Solution {
+ pub fn next_greater_element(nums1: Vec
+
diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md
index 1da32343..efbabc4a 100644
--- a/problems/0501.二叉搜索树中的众数.md
+++ b/problems/0501.二叉搜索树中的众数.md
@@ -33,20 +33,20 @@
进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
这道题目呢,递归法我从两个维度来讲。
首先如果不是二叉搜索树的话,应该怎么解题,是二叉搜索树,又应该如何解题,两种方式做一个比较,可以加深大家对二叉树的理解。
-## 递归法
+### 递归法
-### 如果不是二叉搜索树
+#### 如果不是二叉搜索树
如果不是二叉搜索树,最直观的方法一定是把这个树都遍历了,用map统计频率,把频率排个序,最后取前面高频的元素的集合。
@@ -140,7 +140,7 @@ public:
**所以如果本题没有说是二叉搜索树的话,那么就按照上面的思路写!**
-### 是二叉搜索树
+#### 是二叉搜索树
**既然是搜索树,它中序遍历就是有序的**。
@@ -271,7 +271,7 @@ public:
```
-## 迭代法
+### 迭代法
只要把中序遍历转成迭代,中间节点的处理逻辑完全一样。
@@ -326,7 +326,7 @@ public:
};
```
-# 总结
+## 总结
本题在递归法中,我给出了如果是普通二叉树,应该怎么求众数。
@@ -345,10 +345,10 @@ public:
> **需要强调的是 leetcode上的耗时统计是非常不准确的,看个大概就行,一样的代码耗时可以差百分之50以上**,所以leetcode的耗时统计别太当回事,知道理论上的效率优劣就行了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
暴力法
@@ -472,7 +472,7 @@ class Solution {
}
}
```
-統一迭代法
+统一迭代法
```Java
class Solution {
public int[] findMode(TreeNode root) {
@@ -526,7 +526,7 @@ class Solution {
```
-## Python
+### Python
递归法(版本一)利用字典
@@ -640,7 +640,7 @@ class Solution:
return result
```
-## Go
+### Go
计数法,不使用额外空间,利用二叉树性质,中序遍历
```go
@@ -676,7 +676,7 @@ func findMode(root *TreeNode) []int {
}
```
-## JavaScript
+### JavaScript
使用额外空间map的方法
```javascript
@@ -753,7 +753,7 @@ var findMode = function(root) {
};
```
-## TypeScript
+### TypeScript
> 辅助Map法
@@ -852,7 +852,7 @@ function findMode(root: TreeNode | null): number[] {
};
```
-## Scala
+### Scala
暴力:
```scala
@@ -923,7 +923,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -1015,3 +1015,4 @@ pub fn find_mode(root: Option
+
diff --git a/problems/0503.下一个更大元素II.md b/problems/0503.下一个更大元素II.md
index 3fd4b3b6..d211a680 100644
--- a/problems/0503.下一个更大元素II.md
+++ b/problems/0503.下一个更大元素II.md
@@ -22,7 +22,7 @@
* -10^9 <= nums[i] <= 10^9
-# 思路
+## 思路
做本题之前建议先做[739. 每日温度](https://programmercarl.com/0739.每日温度.html) 和 [496.下一个更大元素 I](https://programmercarl.com/0496.下一个更大元素I.html)。
@@ -138,7 +138,8 @@ public:
## 其他语言版本
-Java:
+### Java:
+
```Java
class Solution {
public int[] nextGreaterElements(int[] nums) {
@@ -162,7 +163,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
# 方法 1:
class Solution:
@@ -196,7 +198,8 @@ class Solution:
stack.append(i)
return ans
```
-Go:
+### Go:
+
```go
func nextGreaterElements(nums []int) []int {
length := len(nums)
@@ -218,7 +221,7 @@ func nextGreaterElements(nums []int) []int {
}
```
-JavaScript:
+### JavaScript:
```JS
/**
@@ -242,7 +245,7 @@ var nextGreaterElements = function (nums) {
return res;
};
```
-TypeScript:
+### TypeScript:
```typescript
function nextGreaterElements(nums: number[]): number[] {
@@ -266,6 +269,25 @@ function nextGreaterElements(nums: number[]): number[] {
};
```
+### Rust:
+
+```rust
+impl Solution {
+ pub fn next_greater_elements(nums: Vec
+
diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md
index 743b0df9..7ef934cc 100644
--- a/problems/0513.找树左下角的值.md
+++ b/problems/0513.找树左下角的值.md
@@ -20,9 +20,9 @@

-## 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -614,7 +614,7 @@ object Solution {
}
```
-### rust
+### Rust
**层序遍历**
@@ -689,3 +689,4 @@ impl Solution {
+
diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md
index fcdd57b0..80927583 100644
--- a/problems/0516.最长回文子序列.md
+++ b/problems/0516.最长回文子序列.md
@@ -152,8 +152,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```java
public class Solution {
@@ -175,8 +174,7 @@ public class Solution {
}
```
-
-Python:
+### Python:
```python
class Solution:
@@ -193,7 +191,7 @@ class Solution:
return dp[0][-1]
```
-Go:
+### Go:
```Go
func longestPalindromeSubseq(s string) int {
@@ -222,7 +220,7 @@ func longestPalindromeSubseq(s string) int {
}
```
-Javascript:
+### Javascript:
```javascript
const longestPalindromeSubseq = (s) => {
@@ -247,7 +245,7 @@ const longestPalindromeSubseq = (s) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function longestPalindromeSubseq(s: string): number {
@@ -281,3 +279,4 @@ function longestPalindromeSubseq(s: string): number {
+
diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md
index 8da35114..7c9f0fce 100644
--- a/problems/0518.零钱兑换II.md
+++ b/problems/0518.零钱兑换II.md
@@ -41,9 +41,9 @@
* 硬币种类不超过 500 种
* 结果符合 32 位符号整数
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
@@ -202,8 +202,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public int change(int amount, int[] coins) {
@@ -242,7 +242,7 @@ class Solution {
}
```
-Python:
+### Python:
```python
@@ -260,7 +260,8 @@ class Solution:
-Go:
+### Go:
+
```go
func change(amount int, coins []int) int {
// 定义dp数组
@@ -280,7 +281,8 @@ func change(amount int, coins []int) int {
}
```
-Rust:
+### Rust:
+
```rust
impl Solution {
pub fn change(amount: i32, coins: Vec
-
diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md
index 3e4391d6..56911858 100644
--- a/problems/0530.二叉搜索树的最小绝对差.md
+++ b/problems/0530.二叉搜索树的最小绝对差.md
@@ -19,12 +19,12 @@
提示:树中至少有 2 个节点。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。
@@ -32,7 +32,7 @@
遇到在二叉搜索树上求什么最值啊,差值之类的,就把它想成在一个有序数组上求最值,求差值,这样就简单多了。
-## 递归
+### 递归
那么二叉搜索树采用中序遍历,其实就是一个有序数组。
@@ -102,7 +102,7 @@ public:
是不是看上去也并不复杂!
-## 迭代
+### 迭代
看过这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式的写法就不能统一一下么?](https://programmercarl.com/二叉树的统一迭代法.html)文章之后,不难写出两种中序遍历的迭代法。
@@ -135,7 +135,7 @@ public:
};
```
-# 总结
+## 总结
**遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。**
@@ -145,10 +145,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
递归
```java
@@ -235,7 +235,7 @@ class Solution {
}
}
```
-## Python
+### Python
递归法(版本一)利用中序递增,结合数组
```python
@@ -313,7 +313,7 @@ class Solution:
```
-## Go:
+### Go
中序遍历,然后计算最小差值
```go
@@ -340,7 +340,7 @@ func getMinimumDifference(root *TreeNode) int {
}
```
-## JavaScript
+### JavaScript
递归 先转换为有序数组
```javascript
/**
@@ -415,7 +415,7 @@ var getMinimumDifference = function(root) {
}
```
-## TypeScript
+### TypeScript
> 辅助数组解决
@@ -482,7 +482,7 @@ function getMinimumDifference(root: TreeNode | null): number {
};
```
-## Scala
+### Scala
构建二叉树的有序数组:
@@ -561,7 +561,7 @@ object Solution {
}
```
-## rust
+### Rust
构建二叉树的有序数组:
@@ -652,3 +652,4 @@ impl Solution {
+
diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md
index 781763a4..c403c98f 100644
--- a/problems/0538.把二叉搜索树转换为累加树.md
+++ b/problems/0538.把二叉搜索树转换为累加树.md
@@ -44,11 +44,11 @@
* 树中的所有值 互不相同 。
* 给定的树为二叉搜索树。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后再遍历其他节点累加?怎么一想这么麻烦呢。
@@ -64,7 +64,7 @@
那么知道如何遍历这个二叉树,也就迎刃而解了,**从树中可以看出累加的顺序是右中左,所以我们需要反中序遍历这个二叉树,然后顺序累加就可以了**。
-## 递归
+### 递归
遍历顺序如图所示:
@@ -131,7 +131,7 @@ public:
};
```
-## 迭代法
+### 迭代法
迭代法其实就是中序模板题了,在[二叉树:前中后序迭代法](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序统一方式迭代法](https://programmercarl.com/二叉树的统一迭代法.html)可以选一种自己习惯的写法。
@@ -166,17 +166,17 @@ public:
};
```
-# 总结
+## 总结
经历了前面各种二叉树增删改查的洗礼之后,这道题目应该比较简单了。
**好了,二叉树已经接近尾声了,接下来就是要对二叉树来一个大总结了**。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
**递归**
```Java
@@ -237,7 +237,7 @@ class Solution {
}
```
-## Python
+### Python
递归法(版本一)
```python
# Definition for a binary tree node.
@@ -318,7 +318,7 @@ class Solution:
self.traversal(root)
return root
-```
+```
迭代法(版本二)
```python
class Solution:
@@ -338,9 +338,9 @@ class Solution:
pre = cur.val
cur =cur.left
return root
-```
+```
-## Go
+### Go
弄一个sum暂存其和值
```go
@@ -362,7 +362,7 @@ func traversal(cur *TreeNode) {
}
```
-## JavaScript
+### JavaScript
递归
```javascript
@@ -401,7 +401,7 @@ var convertBST = function (root) {
};
```
-##C
+### C
递归
```c
@@ -422,7 +422,7 @@ struct TreeNode* convertBST(struct TreeNode* root){
}
```
-## TypeScript
+### TypeScript
> 递归法
@@ -462,7 +462,7 @@ function convertBST(root: TreeNode | null): TreeNode | null {
};
```
-## Scala
+### Scala
```scala
object Solution {
@@ -481,7 +481,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -535,3 +535,4 @@ impl Solution {
+
diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md
index 48d15b0b..505a4e33 100644
--- a/problems/0583.两个字符串的删除操作.md
+++ b/problems/0583.两个字符串的删除操作.md
@@ -17,9 +17,9 @@
* 解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划之子序列,还是为了编辑距离做铺垫 | LeetCode:583.两个字符串的删除操(https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[LeetCode:583.两个字符串的删除操](https://www.bilibili.com/video/BV1we4y157wB/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -143,8 +143,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
// dp数组中存储word1和word2最长相同子序列的长度
class Solution {
@@ -215,8 +215,8 @@ class Solution {
}
```
+### Python:
-Python:
```python
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
@@ -234,7 +234,8 @@ class Solution:
return dp[-1][-1]
```
-Go:
+### Go:
+
```go
func minDistance(word1 string, word2 string) int {
dp := make([][]int, len(word1)+1)
@@ -267,7 +268,8 @@ func min(a, b int) int {
return b
}
```
-Javascript:
+### Javascript:
+
```javascript
// 方法一
var minDistance = (word1, word2) => {
@@ -309,7 +311,7 @@ var minDistance = function (word1, word2) {
};
```
-TypeScript:
+### TypeScript:
> dp版本一:
diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md
index 18a245c4..18839a26 100644
--- a/problems/0617.合并二叉树.md
+++ b/problems/0617.合并二叉树.md
@@ -19,9 +19,9 @@
注意: 合并必须从两个树的根节点开始。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -164,7 +164,7 @@ public:
};
```
-## 迭代法
+### 迭代法
使用迭代法,如何同时处理两棵树呢?
@@ -716,7 +716,7 @@ object Solution {
}
```
-### rust
+### Rust
递归:
diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md
index 084b9f74..fdf83736 100644
--- a/problems/0647.回文子串.md
+++ b/problems/0647.回文子串.md
@@ -26,11 +26,13 @@
提示:输入的字符串长度不会超过 1000 。
-## 暴力解法
+## 思路
+
+### 暴力解法
两层for循环,遍历区间起始位置和终止位置,然后还需要一层遍历判断这个区间是不是回文。所以时间复杂度:O(n^3)
-## 动态规划
+### 动态规划
动规五部曲:
@@ -187,7 +189,7 @@ public:
* 时间复杂度:O(n^2)
* 空间复杂度:O(n^2)
-## 双指针法
+### 双指针法
动态规划的空间复杂度是偏高的,我们再看一下双指针法。
@@ -231,7 +233,7 @@ public:
## 其他语言版本
-Java:
+### Java:
动态规划:
@@ -337,7 +339,7 @@ class Solution {
}
```
-Python:
+### Python:
> 动态规划:
```python
@@ -390,7 +392,8 @@ class Solution:
return res
```
-Go:
+### Go:
+
```Go
func countSubstrings(s string) int {
res:=0
@@ -416,7 +419,8 @@ func countSubstrings(s string) int {
}
```
-Javascript
+### Javascript:
+
> 动态规划
```javascript
const countSubstrings = (s) => {
@@ -462,7 +466,7 @@ const countSubstrings = (s) => {
}
```
-TypeScript:
+### TypeScript:
> 动态规划:
@@ -524,3 +528,4 @@ function expandRange(s: string, left: number, right: number): number {
+
diff --git a/problems/0649.Dota2参议院.md b/problems/0649.Dota2参议院.md
index a1420a66..db6b43df 100644
--- a/problems/0649.Dota2参议院.md
+++ b/problems/0649.Dota2参议院.md
@@ -42,7 +42,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一
因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利。
-# 思路
+## 思路
这道题 题意太绕了,我举一个更形象的例子给大家捋顺一下。
@@ -70,7 +70,7 @@ Dota2 参议院由来自两派的参议员组成。现在参议院希望对一
如果对贪心算法理论基础还不了解的话,可以看看这篇:[关于贪心算法,你该了解这些!](https://programmercarl.com/贪心算法理论基础.html) ,相信看完之后对贪心就有基本的了解了。
-# 代码实现
+## 代码实现
实现代码,在每一轮循环的过程中,去过模拟优先消灭身后的对手,其实是比较麻烦的。
@@ -111,9 +111,9 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -145,7 +145,7 @@ class Solution {
}
```
-## Python
+### Python
```python
class Solution:
@@ -173,7 +173,7 @@ class Solution:
return "Radiant" if R else "Dire"
```
-## Go
+### Go
```go
@@ -214,7 +214,7 @@ func predictPartyVictory(senateStr string) string {
}
```
-## JavaScript
+### JavaScript
```js
var predictPartyVictory = function(senateStr) {
@@ -244,7 +244,7 @@ var predictPartyVictory = function(senateStr) {
};
```
-## TypeScript
+### TypeScript
```typescript
function predictPartyVictory(senate: string): string {
@@ -287,3 +287,4 @@ function predictPartyVictory(senate: string): string {
+
diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md
index 33a9176e..77d7980f 100644
--- a/problems/0654.最大二叉树.md
+++ b/problems/0654.最大二叉树.md
@@ -25,9 +25,9 @@
给定的数组的大小在 [1, 1000] 之间。
-## 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -310,7 +310,7 @@ class Solution:
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
return self.traversal(nums, 0, len(nums))
- ```
+```
(版本三) 使用切片
@@ -587,3 +587,4 @@ impl Solution {
+
diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md
index ab4bf152..f0d33391 100644
--- a/problems/0657.机器人能否返回原点.md
+++ b/problems/0657.机器人能否返回原点.md
@@ -29,7 +29,7 @@
-# 思路
+## 思路
这道题目还是挺简单的,大家不要想复杂了,一波哈希法又一波图论算法啥的,哈哈。
@@ -64,9 +64,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
// 时间复杂度:O(n)
@@ -86,7 +86,7 @@ class Solution {
}
```
-## Python
+### Python
```python
# 时间复杂度:O(n)
@@ -107,7 +107,7 @@ class Solution:
return x == 0 and y == 0
```
-## Go
+### Go
```go
func judgeCircle(moves string) bool {
@@ -131,7 +131,7 @@ func judgeCircle(moves string) bool {
}
```
-## JavaScript
+### JavaScript
```js
// 时间复杂度:O(n)
@@ -150,7 +150,7 @@ var judgeCircle = function(moves) {
```
-## TypeScript
+### TypeScript
```ts
var judgeCircle = function (moves) {
@@ -185,3 +185,4 @@ var judgeCircle = function (moves) {
+
diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md
index 85922a1d..2cfbfefc 100644
--- a/problems/0669.修剪二叉搜索树.md
+++ b/problems/0669.修剪二叉搜索树.md
@@ -20,17 +20,17 @@

-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
相信看到这道题目大家都感觉是一道简单题(事实上leetcode上也标明是简单)。
但还真的不简单!
-## 递归法
+### 递归法
直接想法就是:递归处理,然后遇到 `root->val < low || root->val > high` 的时候直接return NULL,一波修改,赶紧利落。
@@ -188,7 +188,7 @@ public:
只看代码,其实不太好理解节点是如何移除的,这一块大家可以自己再模拟模拟!
-## 迭代法
+### 迭代法
因为二叉搜索树的有序性,不需要使用栈模拟递归的过程。
@@ -233,7 +233,7 @@ public:
};
```
-# 总结
+## 总结
修剪二叉搜索树其实并不难,但在递归法中大家可看出我费了很大的功夫来讲解如何删除节点的,这个思路其实是比较绕的。
@@ -243,10 +243,10 @@ public:
本题我依然给出递归法和迭代法,初学者掌握递归就可以了,如果想进一步学习,就把迭代法也写一写。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
**递归**
@@ -311,7 +311,7 @@ class Solution {
````
-## Python
+### Python
递归法(版本一)
```python
@@ -381,7 +381,7 @@ class Solution:
```
-## Go
+### Go
```go
// 递归
@@ -436,7 +436,7 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode {
```
-## JavaScript版本
+### JavaScript
迭代:
@@ -492,7 +492,7 @@ var trimBST = function (root,low,high) {
}
```
-## TypeScript
+### TypeScript
> 递归法
@@ -540,7 +540,7 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n
};
```
-## Scala
+### Scala
递归法:
@@ -557,7 +557,7 @@ object Solution {
}
```
-## rust
+### Rust
// 递归
@@ -590,3 +590,4 @@ impl Solution {
+
diff --git a/problems/0673.最长递增子序列的个数.md b/problems/0673.最长递增子序列的个数.md
index da80a4e0..0277f249 100644
--- a/problems/0673.最长递增子序列的个数.md
+++ b/problems/0673.最长递增子序列的个数.md
@@ -24,7 +24,7 @@
* 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
-# 思路
+## 思路
这道题可以说是 [300.最长上升子序列](https://programmercarl.com/0300.最长上升子序列.html) 的进阶版本
@@ -221,9 +221,9 @@ public:
还有O(nlog n)的解法,使用树状数组,今天有点忙就先不写了,感兴趣的同学可以自行学习一下,这里有我之前写的树状数组系列博客:https://blog.csdn.net/youngyangyang04/category_871105.html (十年前的陈年老文了)
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -257,7 +257,7 @@ class Solution {
}
```
-## Python
+### Python
```python
class Solution:
@@ -286,7 +286,7 @@ class Solution:
return result;
```
-## Go
+### Go
```go
@@ -332,7 +332,7 @@ func findNumberOfLIS(nums []int) int {
}
```
-## JavaScript
+### JavaScript
```js
var findNumberOfLIS = function(nums) {
@@ -364,3 +364,4 @@ var findNumberOfLIS = function(nums) {
+
diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md
index 8cc270ec..485e321c 100644
--- a/problems/0674.最长连续递增序列.md
+++ b/problems/0674.最长连续递增序列.md
@@ -29,7 +29,7 @@
## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,重点在于连续!| LeetCode:674.最长连续递增序列](https://www.bilibili.com/video/BV1bD4y1778v),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -157,8 +157,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
> 动态规划:
```java
@@ -207,7 +206,7 @@ public static int findLengthOfLCIS(int[] nums) {
}
```
-Python:
+### Python:
DP
```python
@@ -261,7 +260,8 @@ class Solution:
return result
```
-Go:
+### Go:
+
> 动态规划:
```go
func findLengthOfLCIS(nums []int) int {
@@ -302,7 +302,9 @@ func findLengthOfLCIS(nums []int) int {
}
```
-Rust:
+
+### Rust:
+>动态规划
```rust
pub fn find_length_of_lcis(nums: Vec
+
diff --git a/problems/0684.冗余连接.md b/problems/0684.冗余连接.md
index c4d62d9b..8124cc7e 100644
--- a/problems/0684.冗余连接.md
+++ b/problems/0684.冗余连接.md
@@ -25,7 +25,7 @@
* edges 中无重复元素
* 给定的图是连通的
-# 思路
+## 思路
这道题目也是并查集基础题目。
@@ -150,9 +150,9 @@ public:
可以看出,主函数的代码很少,就判断一下边的两个节点在不在同一个集合就可以了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -205,7 +205,7 @@ class Solution {
}
```
-## Python
+### Python
```python
@@ -256,7 +256,7 @@ class Solution:
return []
```
-## Go
+### Go
```go
@@ -312,7 +312,7 @@ func findRedundantConnection(edges [][]int) []int {
}
```
-## JavaScript
+### JavaScript
```js
const n = 1005;
@@ -365,3 +365,4 @@ var findRedundantConnection = function(edges) {
+
diff --git a/problems/0685.冗余连接II.md b/problems/0685.冗余连接II.md
index 8c56afdc..31b2ad24 100644
--- a/problems/0685.冗余连接II.md
+++ b/problems/0685.冗余连接II.md
@@ -213,9 +213,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
@@ -335,7 +335,7 @@ class Solution {
}
```
-## Python
+### Python
```python
@@ -426,7 +426,7 @@ class Solution:
return self.getRemoveEdge(edges)
```
-## Go
+### Go
```go
@@ -527,7 +527,7 @@ func findRedundantDirectedConnection(edges [][]int) []int {
```
-## JavaScript
+### JavaScript
```js
const N = 1010; // 如题:二维数组大小的在3到1000范围内
@@ -623,3 +623,4 @@ var findRedundantDirectedConnection = function(edges) {
+
diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md
index 37a601bc..186f044c 100644
--- a/problems/0695.岛屿的最大面积.md
+++ b/problems/0695.岛屿的最大面积.md
@@ -22,7 +22,7 @@
* 输出:6
* 解释:答案不应该是 11 ,因为岛屿只能包含水平或垂直这四个方向上的 1 。
-# 思路
+## 思路
注意题目中每座岛屿只能由**水平方向和/或竖直方向上**相邻的陆地连接形成。
@@ -38,7 +38,7 @@
* [DFS理论基础](https://programmercarl.com/图论深搜理论基础.html)
* [BFS理论基础](https://programmercarl.com/图论广搜理论基础.html)
-## DFS
+### DFS
很多同学,写dfs其实也是凭感觉来,有的时候dfs函数中写终止条件才能过,有的时候 dfs函数不写终止添加也能过!
@@ -134,7 +134,7 @@ public:
以上两种写法的区别,我在题解: [DFS,BDF 你没注意的细节都给你列出来了!LeetCode:200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/solution/by-carlsun-2-n72a/)做了详细介绍。
-## BFS
+### BFS
关于广度优先搜索,如果大家还不了解的话,看这里:[广度优先搜索精讲](https://programmercarl.com/图论广搜理论基础.html)
@@ -188,9 +188,9 @@ public:
```
-# 其它语言版本
-## Java
-### DFS
+## 其它语言版本
+### Java
+#### DFS
```java
// DFS
class Solution {
@@ -236,7 +236,7 @@ class Solution {
```
-### BFS
+#### BFS
```java
//BFS
class Solution {
@@ -290,7 +290,7 @@ class Solution {
}
}
```
-### DFS 優化(遇到島嶼後,就把他淹沒)
+#### DFS 優化(遇到島嶼後,就把他淹沒)
```java
//这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 “淹没” 操作,即将岛屿所占的地方置为 0。
public int maxAreaOfIsland(int[][] grid) {
@@ -319,8 +319,8 @@ public int dfs(int[][] grid,int i,int j){
}
```
-## Python
-### BFS
+### Python
+#### BFS
```python
class Solution:
def __init__(self):
@@ -359,7 +359,7 @@ class Solution:
self.count += 1
queue.append((new_x, new_y))
```
-### DFS
+#### DFS
```python
class Solution:
def __init__(self):
@@ -394,3 +394,4 @@ class Solution:
+
diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md
index 13064f97..bc21bab8 100644
--- a/problems/0700.二叉搜索树中的搜索.md
+++ b/problems/0700.二叉搜索树中的搜索.md
@@ -18,9 +18,9 @@
在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -415,7 +415,7 @@ object Solution {
}
```
-### rust
+### Rust
递归:
@@ -469,3 +469,4 @@ impl Solution {
+
diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md
index fc4351ba..511d161c 100644
--- a/problems/0701.二叉搜索树中的插入操作.md
+++ b/problems/0701.二叉搜索树中的插入操作.md
@@ -23,11 +23,11 @@
* -10^8 <= val <= 10^8
* 新值和原始二叉搜索树中的任意节点值都不同
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。
@@ -43,7 +43,7 @@
接下来就是遍历二叉搜索树的过程了。
-## 递归
+### 递归
递归三部曲:
@@ -165,7 +165,7 @@ public:
**网上千篇一律的代码,可能会误导大家认为通过递归函数返回节点 这样的写法是天经地义,其实这里是有优化的!**
-## 迭代
+### 迭代
再来看看迭代法,对二叉搜索树迭代写法不熟悉,可以看这篇:[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)
@@ -198,7 +198,7 @@ public:
};
```
-# 总结
+## 总结
首先在二叉搜索树中的插入操作,大家不用恐惧其重构搜索树,其实根本不用重构。
@@ -207,9 +207,9 @@ public:
最后依然给出了迭代的方法,迭代的方法就需要记录当前遍历节点的父节点了,这个和没有返回值的递归函数实现的代码逻辑是一样的。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -254,7 +254,7 @@ class Solution {
}
```
-----
-## Python
+### Python
递归法(版本一)
```python
@@ -371,7 +371,7 @@ class Solution:
```
-----
-## Go
+### Go
递归法
@@ -417,7 +417,7 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
}
```
-----
-## JavaScript
+### JavaScript
有返回值的递归写法
@@ -530,7 +530,7 @@ var insertIntoBST = function (root, val) {
};
```
-## TypeScript
+### TypeScript
> 递归-有返回值
@@ -595,7 +595,7 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
```
-## Scala
+### Scala
递归:
@@ -632,7 +632,7 @@ object Solution {
}
```
-### rust
+### Rust
迭代:
@@ -697,3 +697,4 @@ impl Solution {
+
diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md
index 52abf578..ba5e538c 100644
--- a/problems/0704.二分查找.md
+++ b/problems/0704.二分查找.md
@@ -150,8 +150,8 @@ public:
* [35.搜索插入位置](https://programmercarl.com/0035.搜索插入位置.html)
* [34.在排序数组中查找元素的第一个和最后一个位置](https://programmercarl.com/0034.%E5%9C%A8%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%85%83%E7%B4%A0%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%92%8C%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%BD%AE.html)
-* 69.x 的平方根
-* 367.有效的完全平方数
+* [69.x 的平方根](https://leetcode.cn/problems/sqrtx/)
+* [367.有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/)
@@ -760,9 +760,58 @@ object Solution {
}
}
```
+**Dart:**
+
+```dart
+(版本一)左闭右闭区间
+class Solution {
+ int search(List
+
diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md
index 52b2be3b..db398649 100644
--- a/problems/0714.买卖股票的最佳时机含手续费.md
+++ b/problems/0714.买卖股票的最佳时机含手续费.md
@@ -39,7 +39,7 @@
本题相对于[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html),多添加了一个条件就是手续费。
-## 贪心算法
+### 贪心算法
在[贪心算法:122.买卖股票的最佳时机II](https://programmercarl.com/0122.买卖股票的最佳时机II.html)中使用贪心策略不用关心具体什么时候买卖,只要收集每天的正利润,最后稳稳的就是最大利润了。
@@ -93,7 +93,7 @@ public:
大家也可以发现,情况三,那块代码是可以删掉的,我是为了让代码表达清晰,所以没有精简。
-## 动态规划
+### 动态规划
我在公众号「代码随想录」里将在下一个系列详细讲解动态规划,所以本题解先给出我的C++代码(带详细注释),感兴趣的同学可以自己先学习一下。
@@ -364,3 +364,4 @@ object Solution {
+
diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md
index 042de947..7e8e3d7c 100644
--- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md
+++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md
@@ -32,9 +32,9 @@
* 0 < prices[i] < 50000.
* 0 <= fee < 50000.
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,这次含手续费!| LeetCode:714.买卖股票的最佳时机含手续费](https://www.bilibili.com/video/BV1z44y1Z7UR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -97,8 +97,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
/**
* 卖出时支付手续费
@@ -173,9 +173,9 @@ class Solution {
}
}
```
-```
+### python
+
-Python:
```python
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
@@ -188,7 +188,8 @@ class Solution:
return max(dp[-1][0], dp[-1][1])
```
-Go:
+### Go:
+
```go
// 买卖股票的最佳时机含手续费 动态规划
// 时间复杂度O(n) 空间复杂度O(n)
@@ -211,7 +212,8 @@ func max(a, b int) int {
}
```
-Javascript:
+### Javascript:
+
```javascript
const maxProfit = (prices,fee) => {
let dp = Array.from(Array(prices.length), () => Array(2).fill(0));
@@ -224,7 +226,7 @@ const maxProfit = (prices,fee) => {
}
```
-TypeScript:
+### TypeScript:
```typescript
function maxProfit(prices: number[], fee: number): number {
@@ -245,8 +247,9 @@ function maxProfit(prices: number[], fee: number): number {
};
```
-Rust:
+### Rust:
**贪心**
+
```Rust
impl Solution {
pub fn max_profit(prices: Vec
+
diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md
index d88ebbb0..c2215cf6 100644
--- a/problems/0738.单调递增的数字.md
+++ b/problems/0738.单调递增的数字.md
@@ -26,12 +26,14 @@
说明: N 是在 [0, 10^9] 范围内的一个整数。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,思路不难想,但代码不好写!LeetCode:738.单调自增的数字](https://www.bilibili.com/video/BV1Kv4y1x7tP),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
-## 暴力解法
+### 暴力解法
题意很简单,那么首先想的就是暴力解法了,来我替大家暴力一波,结果自然是超时!
@@ -62,7 +64,7 @@ public:
* 时间复杂度:O(n × m) m为n的数字长度
* 空间复杂度:O(1)
-## 贪心算法
+### 贪心算法
题目要求小于等于N的最大单调递增的整数,那么拿一个两位的数字来举例。
@@ -120,7 +122,7 @@ public:
## 其他语言版本
-### Java:
+### Java
```java
版本1
class Solution {
@@ -163,7 +165,7 @@ class Solution {
```
-### Python:
+### Python
暴力
```python
class Solution:
@@ -395,3 +397,4 @@ impl Solution {
+
diff --git a/problems/0739.每日温度.md b/problems/0739.每日温度.md
index 749dc972..fc1a8063 100644
--- a/problems/0739.每日温度.md
+++ b/problems/0739.每日温度.md
@@ -211,8 +211,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```java
class Solution {
@@ -270,7 +269,8 @@ class Solution {
}
```
-Python:
+### Python:
+
> 未精简版本
```python
@@ -307,7 +307,7 @@ class Solution:
return answer
```
-Go:
+### Go:
> 暴力法
@@ -384,7 +384,7 @@ func dailyTemperatures(num []int) []int {
}
```
-JavaScript:
+### JavaScript:
```javascript
// 版本一
@@ -429,7 +429,7 @@ var dailyTemperatures = function(temperatures) {
};
```
-TypeScript:
+### TypeScript:
> 精简版:
@@ -455,10 +455,31 @@ function dailyTemperatures(temperatures: number[]): number[] {
};
```
+### Rust:
+```rust
+impl Solution {
+ /// 单调栈的本质是以空间换时间,记录之前已访问过的非递增子序列下标
+ pub fn daily_temperatures(temperatures: Vec
+
diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md
index 9eaaa4e9..6fb518c3 100644
--- a/problems/0746.使用最小花费爬楼梯.md
+++ b/problems/0746.使用最小花费爬楼梯.md
@@ -37,9 +37,9 @@
* cost[i] 将会是一个整型数据,范围为 [0, 999] 。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯](https://www.bilibili.com/video/BV16G411c7yZ/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-----------
@@ -523,3 +523,4 @@ public class Solution
+
diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md
index 158f76d6..41314456 100644
--- a/problems/0763.划分字母区间.md
+++ b/problems/0763.划分字母区间.md
@@ -24,9 +24,9 @@
* S的长度在[1, 500]之间。
* S只包含小写字母 'a' 到 'z' 。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,寻找最远的出现位置! LeetCode:763.划分字母区间](https://www.bilibili.com/video/BV18G4y1K7d5),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -409,3 +409,4 @@ impl Solution {
+
diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md
index 2ea4ae47..ec8288c6 100644
--- a/problems/0797.所有可能的路径.md
+++ b/problems/0797.所有可能的路径.md
@@ -149,7 +149,7 @@ public:
```
-# 总结
+## 总结
本题是比较基础的深度优先搜索模板题,这种有向图路径问题,最合适使用深搜,当然本题也可以使用广搜,但广搜相对来说就麻烦了一些,需要记录一下路径。
@@ -159,7 +159,7 @@ public:
## 其他语言版本
-Java
+### Java
```Java
// 深度优先遍历
@@ -190,7 +190,8 @@ class Solution {
}
```
-Python
+### Python
+
```python
class Solution:
def __init__(self):
@@ -216,9 +217,9 @@ class Solution:
self.path.pop() # 回溯
```
-### Go
+
+
-
diff --git a/problems/0827.最大人工岛.md b/problems/0827.最大人工岛.md
index 9112fabd..d7879825 100644
--- a/problems/0827.最大人工岛.md
+++ b/problems/0827.最大人工岛.md
@@ -29,7 +29,7 @@
* 输出: 4
* 解释: 没有0可以让我们变成1,面积依然为 4。
-# 思路
+## 思路
本题的一个暴力想法,应该是遍历地图尝试 将每一个 0 改成1,然后去搜索地图中的最大的岛屿面积。
@@ -219,9 +219,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
class Solution {
@@ -286,4 +286,3 @@ class Solution {
-
diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md
index ac56f6f9..c7f52202 100644
--- a/problems/0844.比较含退格的字符串.md
+++ b/problems/0844.比较含退格的字符串.md
@@ -38,7 +38,7 @@
本文将给出 空间复杂度O(n)的栈模拟方法 以及空间复杂度是O(1)的双指针方法。
-## 普通方法(使用栈的思路)
+### 普通方法(使用栈的思路)
这道题目一看就是要使用栈的节奏,这种匹配(消除)问题也是栈的擅长所在,跟着一起刷题的同学应该知道,在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html),我就已经提过了一次使用栈来做类似的事情了。
@@ -100,7 +100,7 @@ public:
* 时间复杂度:O(n + m)
* 空间复杂度:O(n + m)
-## 优化方法(从后向前双指针)
+### 优化方法(从后向前双指针)
当然还可以有使用 O(1) 的空间复杂度来解决该问题。
@@ -289,7 +289,7 @@ class Solution {
}
```
-### python
+### Python
```python
class Solution:
@@ -591,3 +591,4 @@ impl Solution {
+
diff --git a/problems/0860.柠檬水找零.md b/problems/0860.柠檬水找零.md
index 5046df12..50a0c31a 100644
--- a/problems/0860.柠檬水找零.md
+++ b/problems/0860.柠檬水找零.md
@@ -50,9 +50,9 @@
* 0 <= bills.length <= 10000
* bills[i] 不是 5 就是 10 或是 20
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零](https://www.bilibili.com/video/BV12x4y1j7DD),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -403,3 +403,4 @@ object Solution {
+
diff --git a/problems/0922.按奇偶排序数组II.md b/problems/0922.按奇偶排序数组II.md
index c4654f16..72be8fa7 100644
--- a/problems/0922.按奇偶排序数组II.md
+++ b/problems/0922.按奇偶排序数组II.md
@@ -147,8 +147,6 @@ class Solution {
}
```
-### java
-
```java
//方法一:采用额外的数组空间
class Solution {
@@ -384,3 +382,4 @@ function sortArrayByParityII(nums: number[]): number[] {
+
diff --git a/problems/0941.有效的山脉数组.md b/problems/0941.有效的山脉数组.md
index f43a2108..48c29eb4 100644
--- a/problems/0941.有效的山脉数组.md
+++ b/problems/0941.有效的山脉数组.md
@@ -33,7 +33,7 @@
* 输出:true
-# 思路
+## 思路
判断是山峰,主要就是要严格的保存左边到中间,和右边到中间是递增的。
@@ -71,9 +71,9 @@ public:
如果想系统学一学双指针的话, 可以看一下这篇[双指针法:总结篇!](https://programmercarl.com/双指针总结.html)
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -101,7 +101,7 @@ class Solution {
}
```
-## Python3
+### Python3
```python
class Solution:
@@ -118,7 +118,7 @@ class Solution:
```
-## Go
+### Go
```go
func validMountainArray(arr []int) bool {
@@ -142,7 +142,7 @@ func validMountainArray(arr []int) bool {
}
```
-## JavaScript
+### JavaScript
```js
var validMountainArray = function(arr) {
@@ -157,7 +157,7 @@ var validMountainArray = function(arr) {
};
```
-## TypeScript
+### TypeScript
```typescript
function validMountainArray(arr: number[]): boolean {
@@ -177,7 +177,7 @@ function validMountainArray(arr: number[]): boolean {
};
```
-## C#
+### C#
```csharp
public class Solution {
@@ -201,3 +201,4 @@ public class Solution {
+
diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md
index e2ba8ebf..be04bd47 100644
--- a/problems/0968.监控二叉树.md
+++ b/problems/0968.监控二叉树.md
@@ -38,9 +38,9 @@
* 给定树的节点数的范围是 [1, 1000]。
* 每个节点的值都是 0。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,二叉树与贪心的结合,有点难...... LeetCode:968.监督二叉树](https://www.bilibili.com/video/BV1SA411U75i),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -732,3 +732,4 @@ impl Solution {
+
diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md
index a53148b3..1138d7fc 100644
--- a/problems/1002.查找常用字符.md
+++ b/problems/1002.查找常用字符.md
@@ -30,7 +30,7 @@ words[i] 由小写英文字母组成
-# 思路
+## 思路
这道题意一起就有点绕,不是那么容易懂,其实就是26个小写字符中有字符 在所有字符串里都出现的话,就输出,重复的也算。
@@ -140,7 +140,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```Java
class Solution {
@@ -174,7 +174,8 @@ class Solution {
}
}
```
-Python
+### Python
+
```python
class Solution:
def commonChars(self, words: List[str]) -> List[str]:
@@ -218,7 +219,8 @@ class Solution:
return l
```
-javaScript
+### JavaScript
+
```js
var commonChars = function (words) {
let res = []
@@ -285,7 +287,8 @@ var commonChars = function(words) {
}
```
-TypeScript
+### TypeScript
+
```ts
console.time("test")
let str: string = ""
@@ -321,7 +324,8 @@ TypeScript
return str.split("")
```
-GO
+### GO
+
```golang
func commonChars(words []string) []string {
length:=len(words)
@@ -357,7 +361,8 @@ func min(a,b int)int{
}
```
-Swift:
+### Swift:
+
```swift
func commonChars(_ words: [String]) -> [String] {
var res = [String]()
@@ -397,7 +402,8 @@ func commonChars(_ words: [String]) -> [String] {
}
```
-C:
+### C:
+
```c
//若两个哈希表定义为char数组(每个单词的最大长度不会超过100,因此可以用char表示),可以提高时间和空间效率
void updateHashTable(int* hashTableOne, int* hashTableTwo) {
@@ -449,7 +455,8 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){
return ret;
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
def commonChars(words: Array[String]): List[String] = {
@@ -483,7 +490,7 @@ object Solution {
}
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -522,3 +529,4 @@ impl Solution {
+
diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md
index 4cf69d6f..bed11c7a 100644
--- a/problems/1005.K次取反后最大化的数组和.md
+++ b/problems/1005.K次取反后最大化的数组和.md
@@ -34,9 +34,9 @@
* 1 <= K <= 10000
* -100 <= A[i] <= 100
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,这不就是常识?还能叫贪心?LeetCode:1005.K次取反后最大化的数组和](https://www.bilibili.com/video/BV138411G7LY),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md
index 7142d75c..ff9e5dc4 100644
--- a/problems/1035.不相交的线.md
+++ b/problems/1035.不相交的线.md
@@ -19,7 +19,7 @@
## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线](https://www.bilibili.com/video/BV1h84y1x7MP),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -82,8 +82,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public int maxUncrossedLines(int[] nums1, int[] nums2) {
@@ -106,7 +106,8 @@ Java:
}
```
-Python:
+### Python:
+
```python
class Solution:
def maxUncrossedLines(self, A: List[int], B: List[int]) -> int:
@@ -120,8 +121,7 @@ class Solution:
return dp[-1][-1]
```
-
-Golang:
+### Go:
```go
func maxUncrossedLines(A []int, B []int) int {
@@ -152,28 +152,51 @@ func max(a, b int) int {
}
```
-Rust:
+### Rust:
```rust
-pub fn max_uncrossed_lines(nums1: Vec
+
diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md
index 932029ab..cc661317 100644
--- a/problems/1049.最后一块石头的重量II.md
+++ b/problems/1049.最后一块石头的重量II.md
@@ -35,9 +35,9 @@
* 1 <= stones.length <= 30
* 1 <= stones[i] <= 1000
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[这个背包最多能装多少?LeetCode:1049.最后一块石头的重量II](https://www.bilibili.com/video/BV14M411C7oV/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -341,7 +341,7 @@ func max(a, b int) int {
}
```
-### JavaScript
+### JavaScript:
```javascript
/**
@@ -364,7 +364,7 @@ var lastStoneWeightII = function (stones) {
};
```
-### C
+### C:
```c
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
@@ -413,7 +413,7 @@ function lastStoneWeightII(stones: number[]): number {
};
```
-### Scala
+### Scala:
滚动数组:
```scala
@@ -455,7 +455,7 @@ object Solution {
}
```
-### Rust
+### Rust:
```rust
impl Solution {
@@ -477,3 +477,4 @@ impl Solution {
+
diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md
index 68269b87..f33391c3 100644
--- a/problems/1143.最长公共子序列.md
+++ b/problems/1143.最长公共子序列.md
@@ -39,7 +39,7 @@
## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划子序列问题经典题目 | LeetCode:1143.最长公共子序列](https://www.bilibili.com/video/BV1ye4y1L7CQ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -136,7 +136,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
/*
@@ -207,8 +207,9 @@ class Solution {
}
```
-Python:
+### Python:
2维DP
+
```python
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
@@ -252,7 +253,8 @@ class Solution:
```
-Go:
+### Go:
+
```Go
func longestCommonSubsequence(text1 string, text2 string) int {
t1 := len(text1)
@@ -283,7 +285,8 @@ func max(a,b int)int {
```
-Javascript:
+### JavaScript:
+
```javascript
const longestCommonSubsequence = (text1, text2) => {
let dp = Array.from(Array(text1.length+1), () => Array(text2.length+1).fill(0));
@@ -302,7 +305,7 @@ const longestCommonSubsequence = (text1, text2) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function longestCommonSubsequence(text1: string, text2: string): number {
@@ -326,24 +329,50 @@ function longestCommonSubsequence(text1: string, text2: string): number {
};
```
-Rust:
+### Rust
+
```rust
-pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
- let (n, m) = (text1.len(), text2.len());
- let (s1, s2) = (text1.as_bytes(), text2.as_bytes());
- let mut dp = vec![0; m + 1];
- let mut last = vec![0; m + 1];
- for i in 1..=n {
- dp.swap_with_slice(&mut last);
- for j in 1..=m {
- dp[j] = if s1[i - 1] == s2[j - 1] {
- last[j - 1] + 1
- } else {
- last[j].max(dp[j - 1])
- };
+impl Solution {
+ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
+ let mut dp = vec![vec![0; text2.len() + 1]; text1.len() + 1];
+ for (i, c1) in text1.chars().enumerate() {
+ for (j, c2) in text2.chars().enumerate() {
+ if c1 == c2 {
+ dp[i + 1][j + 1] = dp[i][j] + 1;
+ } else {
+ dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]);
+ }
+ }
}
+ dp[text1.len()][text2.len()]
+ }
+}
+```
+
+一维:
+
+```rust
+impl Solution {
+ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
+ let mut dp = vec![0; text2.len() + 1];
+ for c1 in text1.chars() {
+ // 初始化 prev
+ let mut prev = 0;
+
+ for (j, c2) in text2.chars().enumerate() {
+ let temp = dp[j + 1];
+ if c1 == c2 {
+ // 使用上一次的状态,防止重复计算
+ dp[j + 1] = prev + 1;
+ } else {
+ dp[j + 1] = dp[j + 1].max(dp[j]);
+ }
+ // 使用上一次的状态更新 prev
+ prev = temp;
+ }
+ }
+ dp[text2.len()]
}
- dp[m]
}
```
@@ -353,3 +382,4 @@ pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
+
diff --git a/problems/1207.独一无二的出现次数.md b/problems/1207.独一无二的出现次数.md
index 1a7a0019..2ccd30c3 100644
--- a/problems/1207.独一无二的出现次数.md
+++ b/problems/1207.独一无二的出现次数.md
@@ -31,7 +31,7 @@
* -1000 <= arr[i] <= 1000
-# 思路
+## 思路
这道题目数组在是哈希法中的经典应用,如果对数组在哈希法中的使用还不熟悉的同学可以看这两篇:[数组在哈希法中的应用](https://programmercarl.com/0242.有效的字母异位词.html)和[哈希法:383. 赎金信](https://programmercarl.com/0383.赎金信.html)
@@ -71,9 +71,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```java
class Solution {
@@ -97,7 +97,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
# 方法 1: 数组在哈西法的应用
class Solution:
@@ -133,10 +134,8 @@ class Solution:
```
+### JavaScript:
-Go:
-
-JavaScript:
``` javascript
// 方法一:使用数组记录元素出现次数
var uniqueOccurrences = function(arr) {
@@ -171,7 +170,7 @@ var uniqueOccurrences = function(arr) {
};
```
-TypeScript:
+### TypeScript:
> 借用数组:
@@ -201,11 +200,32 @@ function uniqueOccurrences(arr: number[]): boolean {
})
return countMap.size === new Set(countMap.values()).size;
};
+```
+
+
+### Go:
+```Go
+func uniqueOccurrences(arr []int) bool {
+ count := make(map[int]int) // 统计数字出现的频率
+ for _, v := range arr {
+ count[v] += 1
+ }
+ fre := make(map[int]struct{}) // 看相同频率是否重复出现
+ for _, v := range count {
+ if _, ok := fre[v]; ok {
+ return false
+ }
+ fre[v] = struct{}{}
+ }
+ return true
+}
```
+
+
diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md
index b898b7f2..cc7a7007 100644
--- a/problems/1356.根据数字二进制下1的数目排序.md
+++ b/problems/1356.根据数字二进制下1的数目排序.md
@@ -46,7 +46,7 @@
-# 思路
+## 思路
这道题其实是考察如何计算一个数的二进制中1的数量。
@@ -87,7 +87,7 @@ int bitCount(int n) {
下面我就使用方法二,来做这道题目:
-## C++代码
+
```C++
class Solution {
@@ -116,9 +116,9 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -151,7 +151,7 @@ class Solution {
-## Python
+### Python
```python
class Solution:
@@ -167,7 +167,7 @@ class Solution:
return count
```
-## Go
+### Go
```go
func sortByBits(arr []int) []int {
@@ -205,7 +205,7 @@ func bitCount(n int) int {
}
```
-## JavaScript
+### JavaScript
```js
var sortByBits = function(arr) {
@@ -227,3 +227,4 @@ var sortByBits = function(arr) {
+
diff --git a/problems/1365.有多少小于当前数字的数字.md b/problems/1365.有多少小于当前数字的数字.md
index 7c268769..c706ba21 100644
--- a/problems/1365.有多少小于当前数字的数字.md
+++ b/problems/1365.有多少小于当前数字的数字.md
@@ -39,7 +39,7 @@
* 2 <= nums.length <= 500
* 0 <= nums[i] <= 100
-# 思路
+## 思路
两层for循环暴力查找,时间复杂度明显为$O(n^2)$。
@@ -113,9 +113,9 @@ public:
可以排序之后加哈希,时间复杂度为$O(n\log n)$
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```Java
public int[] smallerNumbersThanCurrent(int[] nums) {
@@ -136,7 +136,8 @@ public int[] smallerNumbersThanCurrent(int[] nums) {
}
```
-Python:
+### Python:
+
```python
class Solution:
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
@@ -151,7 +152,8 @@ class Solution:
return res
```
-Go:
+### Go:
+
```go
func smallerNumbersThanCurrent(nums []int) []int {
// map,key[数组中出现的数] value[比这个数小的个数]
@@ -180,7 +182,8 @@ func smallerNumbersThanCurrent(nums []int) []int {
}
```
-JavaScript:
+### JavaScript:
+
```javascript
// 方法一:使用哈希表记录位置
var smallerNumbersThanCurrent = function(nums) {
@@ -217,7 +220,7 @@ var smallerNumbersThanCurrent = function(nums) {
};
```
-TypeScript:
+### TypeScript:
> 暴力法:
diff --git a/problems/1382.将二叉搜索树变平衡.md b/problems/1382.将二叉搜索树变平衡.md
index 0f81745c..57e56b8f 100644
--- a/problems/1382.将二叉搜索树变平衡.md
+++ b/problems/1382.将二叉搜索树变平衡.md
@@ -28,7 +28,7 @@
* 树节点的数目在 1 到 10^4 之间。
* 树节点的值互不相同,且在 1 到 10^5 之间。
-# 思路
+## 思路
这道题目,可以中序遍历把二叉树转变为有序数组,然后在根据有序数组构造平衡二叉搜索树。
@@ -71,9 +71,10 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
+
+### Java:
-Java:
```java
class Solution {
ArrayList
+
diff --git a/problems/1791.找出星型图的中心节点.md b/problems/1791.找出星型图的中心节点.md
index d44c1476..9bcc7ef9 100644
--- a/problems/1791.找出星型图的中心节点.md
+++ b/problems/1791.找出星型图的中心节点.md
@@ -3,6 +3,7 @@
+
diff --git a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md
index a488c0ba..8be48f38 100644
--- a/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md
+++ b/problems/O(n)的算法居然超时了,此时的n究竟是多大?.md
@@ -13,7 +13,7 @@
计算机究竟1s可以执行多少次操作呢? 接下来探讨一下这个问题。
-# 超时是怎么回事
+## 超时是怎么回事

@@ -25,7 +25,7 @@
如果n的规模已经足够让O(n)的算法运行时间超过了1s,就应该考虑log(n)的解法了。
-# 从硬件配置看计算机的性能
+## 从硬件配置看计算机的性能
计算机的运算速度主要看CPU的配置,以2015年MacPro为例,CPU配置:2.7 GHz Dual-Core Intel Core i5 。
@@ -44,7 +44,7 @@
所以我们的程序在计算机上究竟1s真正能执行多少次操作呢?
-# 做个测试实验
+## 做个测试实验
在写测试程序测1s内处理多大数量级数据的时候,有三点需要注意:
@@ -155,7 +155,7 @@ O(nlogn)的算法,1s内大概计算机可以运行 2 * (10^7)次计算,符
至于O(log n)和O(n^3) 等等这些时间复杂度在1s内可以处理的多大的数据规模,大家可以自己写一写代码去测一下了。
-# 完整测试代码
+## 完整测试代码
```CPP
#include
+
diff --git a/problems/qita/join.md b/problems/qita/join.md
new file mode 100644
index 00000000..3b3e2d40
--- /dev/null
+++ b/problems/qita/join.md
@@ -0,0 +1,249 @@
+
+
+# 如何在Github上提交PR(pull request)
+
+
+* 如何提交代码
+* 合入不规范
+ * 提交信息不规范
+ * Markdown 代码格式
+ * pull request里的commit数量
+ * 代码注释
+ * 说明具体是哪种方法
+ * 代码规范
+ * 代码逻辑
+ * 处理冲突
+
+以下在 [https://github.com/youngyangyang04/leetcode-master](https://github.com/youngyangyang04/leetcode-master) 上提交pr为为例
+
+## 如何合入代码
+
+首先来说一说如何合入代码,不少录友还不太会使用github,所以这里也做一下科普。
+
+我特意申请一个新的Github账号,给大家做一个示范。
+
+需要强调一下,一个commit就只更新一道题目,不要很多题目一起放在一个commit里,那样就很乱。
+
+首先LeetCode-Master每天都有更新,如何保持你fork到自己的仓库是最新的版本呢。
+
+点击这里Fetch upstream。
+
+























+
diff --git a/problems/关于时间复杂度,你不知道的都在这里!.md b/problems/关于时间复杂度,你不知道的都在这里!.md
index c479dddc..95c7567c 100644
--- a/problems/关于时间复杂度,你不知道的都在这里!.md
+++ b/problems/关于时间复杂度,你不知道的都在这里!.md
@@ -8,6 +8,8 @@
所以重新整理的时间复杂度文章,正式和大家见面啦!
+# 时间复杂度
+
## 究竟什么是时间复杂度
**时间复杂度是一个函数,它定性描述该算法的运行时间**。
@@ -145,7 +147,7 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项
**当然这不是这道题目的最优解,我仅仅是用这道题目来讲解一下时间复杂度**。
-# 总结
+## 总结
本篇讲解了什么是时间复杂度,复杂度是用来干什么,以及数据规模对时间复杂度的影响。
@@ -157,17 +159,6 @@ O(2 × n^2 + 10 × n + 1000) < O(3 × n^2),所以说最后省略掉常数项
如果感觉「代码随想录」很不错,赶快推荐给身边的朋友同学们吧,他们发现和「代码随想录」相见恨晚!
-## 其他语言版本
-
-
-Java:
-
-
-Python:
-
-
-Go:
-
@@ -175,3 +166,4 @@ Go:
+
diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md
index b6446403..48781eda 100644
--- a/problems/前序/ACM模式如何构建二叉树.md
+++ b/problems/前序/ACM模式如何构建二叉树.md
@@ -1,6 +1,8 @@
# 力扣上如何自己构造二叉树输入用例?
+**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。
+
经常有录友问,二叉树的题目中输入用例在ACM模式下应该怎么构造呢?
力扣上的题目,输入用例就给了一个数组,怎么就能构造成二叉树呢?
diff --git a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md
index 0b9d230f..0012f88e 100644
--- a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md
+++ b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md
@@ -5,6 +5,10 @@
什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。
+
+**这里给大家推荐ACM模式练习网站**:[kamacoder.com](https://kamacoder.com),把上面的题目刷完,ACM模式就没问题了。
+
+
而力扣上是核心代码模式,就是把要处理的数据都已经放入容器里,可以直接写逻辑,例如这样:
```CPP
diff --git a/problems/前序/代码风格.md b/problems/前序/代码风格.md
index b5083460..8fd8b40c 100644
--- a/problems/前序/代码风格.md
+++ b/problems/前序/代码风格.md
@@ -29,7 +29,7 @@
这里我简单说一说规范问题。
-**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:googlec++编程规范,就可以领取。(涉及到微信后台的回复,没更改)
+**权威的C++规范以Google为主**,我给大家下载了一份中文版本,在公众号「代码随想录」后台回复:编程规范,就可以领取。
**具体的规范要以自己团队风格为主**,融入团队才是最重要的。
diff --git a/problems/前序/深圳互联网公司总结.md b/problems/前序/深圳互联网公司总结.md
index f8c07016..52a8448b 100644
--- a/problems/前序/深圳互联网公司总结.md
+++ b/problems/前序/深圳互联网公司总结.md
@@ -36,7 +36,7 @@
* 微众银行(总部深圳)
* 招银科技(总部深圳)
* 平安系列(平安科技、平安寿险、平安产险、平安金融、平安好医生等)
-* Shopee(东南亚最大的电商平台,最近发展势头非常强劲)
+* Shopee(21年有裁员风波)
* 有赞(深圳)
* 迅雷(总部深圳)
* 金蝶(总部深圳)
diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md
index 008b7915..a3fb7ab3 100644
--- a/problems/剑指Offer58-II.左旋转字符串.md
+++ b/problems/剑指Offer58-II.左旋转字符串.md
@@ -142,7 +142,7 @@ class Solution {
}
```
-### python:
+### Python:
(版本一)使用切片
```python
@@ -338,7 +338,7 @@ func reverseString(_ s: inout [Character], startIndex: Int, endIndex: Int) {
```
-### PHP
+### PHP:
```php
function reverseLeftWords($s, $n) {
@@ -418,4 +418,3 @@ impl Solution {
-
diff --git a/problems/动态规划-股票问题总结篇.md b/problems/动态规划-股票问题总结篇.md
index 2b04b7b0..4df21fb7 100644
--- a/problems/动态规划-股票问题总结篇.md
+++ b/problems/动态规划-股票问题总结篇.md
@@ -471,21 +471,10 @@ public:
「代码随想录」值得推荐给身边每一位学习算法的朋友同学们,关注后都会发现相见恨晚!
-## 其他语言版本
-
-
-Java:
-
-
-Python:
-
-
-Go:
-
-
+
diff --git a/problems/动态规划总结篇.md b/problems/动态规划总结篇.md
index 8a1531f8..e28bfd04 100644
--- a/problems/动态规划总结篇.md
+++ b/problems/动态规划总结篇.md
@@ -40,7 +40,7 @@
好啦,我们再一起回顾一下,动态规划专题中我们都讲了哪些内容。
-## 动划基础
+## 动态规划基础
* [关于动态规划,你该了解这些!](https://programmercarl.com/动态规划理论基础.html)
* [动态规划:斐波那契数](https://programmercarl.com/0509.斐波那契数.html)
diff --git a/problems/动态规划理论基础.md b/problems/动态规划理论基础.md
index a99c0690..bff26d1d 100644
--- a/problems/动态规划理论基础.md
+++ b/problems/动态规划理论基础.md
@@ -12,7 +12,7 @@
## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划理论基础](https://www.bilibili.com/video/BV13Q4y197Wg),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 什么是动态规划
@@ -135,3 +135,4 @@
+
diff --git a/problems/周总结/20201030回溯周末总结.md b/problems/周总结/20201030回溯周末总结.md
index 61270161..7f6fd114 100644
--- a/problems/周总结/20201030回溯周末总结.md
+++ b/problems/周总结/20201030回溯周末总结.md
@@ -9,6 +9,8 @@
--------------------------
+# 本周小结!(回溯算法系列一)
+
## 周一
本周我们正式开始了回溯算法系列,那么首先当然是概述。
diff --git a/problems/周总结/20210107动规周末总结.md b/problems/周总结/20210107动规周末总结.md
index b4baa4ad..831ce348 100644
--- a/problems/周总结/20210107动规周末总结.md
+++ b/problems/周总结/20210107动规周末总结.md
@@ -1,3 +1,4 @@
+# 本周小结!(动态规划系列一)
这周我们正式开始动态规划的学习!
diff --git a/problems/周总结/20210114动规周末总结.md b/problems/周总结/20210114动规周末总结.md
index a77efa2f..71cb49a9 100644
--- a/problems/周总结/20210114动规周末总结.md
+++ b/problems/周总结/20210114动规周末总结.md
@@ -1,3 +1,4 @@
+# 本周小结!(动态规划系列二)
## 周一
diff --git a/problems/周总结/20210225动规周末总结.md b/problems/周总结/20210225动规周末总结.md
index 8fd292ed..e74dd6be 100644
--- a/problems/周总结/20210225动规周末总结.md
+++ b/problems/周总结/20210225动规周末总结.md
@@ -1,3 +1,4 @@
+# 本周小结!(动态规划系列六)
本周我们主要讲解了打家劫舍系列,这个系列也是dp解决的经典问题,那么来看看我们收获了哪些呢,一起来回顾一下吧。
diff --git a/problems/周总结/20210304动规周末总结.md b/problems/周总结/20210304动规周末总结.md
index b814e1b3..ec442a39 100644
--- a/problems/周总结/20210304动规周末总结.md
+++ b/problems/周总结/20210304动规周末总结.md
@@ -1,3 +1,4 @@
+# 本周小结!(动态规划系列七)
本周的主题就是股票系列,来一起回顾一下吧
diff --git a/problems/回溯总结.md b/problems/回溯总结.md
index 21d78bc2..f2e46829 100644
--- a/problems/回溯总结.md
+++ b/problems/回溯总结.md
@@ -8,7 +8,9 @@
> 20张树形结构图、14道精选回溯题目,21篇回溯法精讲文章,由浅入深,一气呵成,这是全网最强回溯算法总结!
-# 回溯法理论基础
+# 回溯总结篇
+
+## 回溯法理论基础
转眼间[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)里已经分享连续讲解了21天的回溯算法,是时候做一个大总结了,本篇高能,需要花费很大的精力来看!
@@ -51,10 +53,10 @@ void backtracking(参数) {
**事实证明这个模板会伴随整个回溯法系列!**
-# 组合问题
-
## 组合问题
+### 组合问题
+
在[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)中,我们开始用回溯法解决第一道题目:组合问题。
我在文中开始的时候给大家列举k层for循环例子,进而得出都是同样是暴力解法,为什么要用回溯法!
@@ -82,9 +84,9 @@ void backtracking(参数) {
**在for循环上做剪枝操作是回溯法剪枝的常见套路!** 后面的题目还会经常用到。
-## 组合总和
+### 组合总和
-### 组合总和(一)
+#### 组合总和(一)
在[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)中,相当于 [回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html)加了一个元素总和的限制。
@@ -99,7 +101,7 @@ void backtracking(参数) {
所以剪枝的代码可以在for循环加上 `i <= 9 - (k - path.size()) + 1` 的限制!
-### 组合总和(二)
+#### 组合总和(二)
在[回溯算法:求组合总和(二)](https://programmercarl.com/0039.组合总和.html)中讲解的组合总和问题,和[回溯算法:求组合问题!](https://programmercarl.com/0077.组合.html),[回溯算法:求组合总和!](https://programmercarl.com/0216.组合总和III.html)和区别是:本题没有数量要求,可以无限重复,但是有总和的限制,所以间接的也是有个数的限制。
@@ -128,7 +130,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;

-### 组合总和(三)
+#### 组合总和(三)
在[回溯算法:求组合总和(三)](https://programmercarl.com/0040.组合总和II.html)中集合元素会有重复,但要求解集不能包含重复的组合。
@@ -151,7 +153,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;
对于去重,其实排列和子集问题也是一样的道理。
-## 多个集合求组合
+### 多个集合求组合
在[回溯算法:电话号码的字母组合](https://programmercarl.com/0017.电话号码的字母组合.html)中,开始用多个集合来求组合,还是熟悉的模板题目,但是有一些细节。
@@ -167,7 +169,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;
其实本题不算难,但也处处是细节,还是要反复琢磨。
-# 切割问题
+## 切割问题
在[回溯算法:分割回文串](https://programmercarl.com/0131.分割回文串.html)中,我们开始讲解切割问题,虽然最后代码看起来好像是一道模板题,但是从分析到学会套用这个模板,是比较难的。
@@ -192,9 +194,9 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;

-# 子集问题
+## 子集问题
-## 子集问题(一)
+### 子集问题(一)
在[回溯算法:求子集问题!](https://programmercarl.com/0078.子集.html)中讲解了子集问题,**在树形结构中子集问题是要收集所有节点的结果,而组合问题是收集叶子节点的结果**。
@@ -219,7 +221,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加
}
```
-## 子集问题(二)
+### 子集问题(二)
在[回溯算法:求子集问题(二)](https://programmercarl.com/0090.子集II.html)中,开始针对子集问题进行去重。
@@ -229,7 +231,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加

-## 递增子序列
+### 递增子序列
在[回溯算法:递增子序列](https://programmercarl.com/0491.递增子序列.html)中,处处都能看到子集的身影,但处处是陷阱,值得好好琢磨琢磨!
@@ -247,9 +249,9 @@ if (startIndex >= nums.size()) { // 终止条件可以不加
**相信这个图胜过千言万语的解释了**。
-# 排列问题
+## 排列问题
-## 排列问题(一)
+### 排列问题(一)
[回溯算法:排列问题!](https://programmercarl.com/0046.全排列.html) 又不一样了。
@@ -266,7 +268,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加
* 每层都是从0开始搜索而不是startIndex
* 需要used数组记录path里都放了哪些元素了
-## 排列问题(二)
+### 排列问题(二)
排列问题也要去重了,在[回溯算法:排列问题(二)](https://programmercarl.com/0047.全排列II.html)中又一次强调了“树层去重”和“树枝去重”。
@@ -290,7 +292,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加
本题used数组即是记录path里都放了哪些元素,同时也用来去重,一举两得。
-# 去重问题
+## 去重问题
以上我都是统一使用used数组来去重的,其实使用set也可以用来去重!
@@ -310,7 +312,7 @@ if (startIndex >= nums.size()) { // 终止条件可以不加
used数组可是全局变量,每层与每层之间公用一个used数组,所以空间复杂度是O(n + n),最终空间复杂度还是O(n)。
-# 重新安排行程(图论额外拓展)
+## 重新安排行程(图论额外拓展)
之前说过,有递归的地方就有回溯,深度优先搜索也是用递归来实现的,所以往往伴随着回溯。
@@ -327,9 +329,9 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所
本题其实是一道深度优先搜索的题目,但是我完全使用回溯法的思路来讲解这道题题目,**算是给大家拓展一下思维方式,其实深搜和回溯也是分不开的,毕竟最终都是用递归**。
-# 棋盘问题
+## 棋盘问题
-## N皇后问题
+### N皇后问题
在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。
@@ -349,7 +351,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所
-## 解数独问题
+### 解数独问题
在[回溯算法:解数独](https://programmercarl.com/0037.解数独.html)中要征服回溯法的最后一道山峰。
@@ -373,7 +375,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所
**这样,解数独这么难的问题也被我们攻克了**。
-# 性能分析
+## 性能分析
**关于回溯算法的复杂度分析在网上的资料鱼龙混杂,一些所谓的经典面试书籍不讲回溯算法,算法书籍对这块也避而不谈,感觉就像是算法里模糊的边界**。
@@ -410,7 +412,7 @@ N皇后问题分析:
**一般说道回溯算法的复杂度,都说是指数级别的时间复杂度,这也算是一个概括吧!**
-# 总结
+## 总结
**[「代码随想录」](https://img-blog.csdnimg.cn/20200815195519696.png)历时21天,14道经典题目分析,20张树形图,21篇回溯法精讲文章,从组合到切割,从子集到排列,从棋盘问题到最后的复杂度分析**,至此收尾了。
@@ -449,3 +451,4 @@ N皇后问题分析:
+
diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md
index 4e09b925..2be79805 100644
--- a/problems/回溯算法去重问题的另一种写法.md
+++ b/problems/回溯算法去重问题的另一种写法.md
@@ -246,8 +246,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所
## 其他语言版本
-
-Java:
+### Java
**47.全排列II**
@@ -350,7 +349,7 @@ class Solution {
}
}
```
-Python:
+### Python
**90.子集II**
@@ -432,7 +431,7 @@ class Solution:
```
-JavaScript:
+### JavaScript
**90.子集II**
@@ -510,7 +509,7 @@ function permuteUnique(nums) {
};
```
-TypeScript:
+### TypeScript
**90.子集II**
@@ -592,7 +591,7 @@ function permuteUnique(nums: number[]): number[][] {
};
```
-Rust:
+### Rust
**90.子集II**:
@@ -713,3 +712,4 @@ impl Solution {
+
diff --git a/problems/回溯算法理论基础.md b/problems/回溯算法理论基础.md
index f11dbaef..dff1823c 100644
--- a/problems/回溯算法理论基础.md
+++ b/problems/回溯算法理论基础.md
@@ -6,13 +6,19 @@
# 回溯算法理论基础
-## 题目分类大纲如下:
+## 题目分类
-可以配合我的B站视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/) 一起学习!
+## 算法公开课
-## 什么是回溯法
+
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/),相信结合视频再看本篇题解,更有助于大家对本题的理解。**
+
+## 理论基础
+
+### 什么是回溯法
回溯法也可以叫做回溯搜索法,它是一种搜索的方式。
@@ -22,7 +28,7 @@
**所以以下讲解中,回溯函数也就是递归函数,指的都是一个函数**。
-## 回溯法的效率
+### 回溯法的效率
回溯法的性能如何呢,这里要和大家说清楚了,**虽然回溯法很难,很不好理解,但是回溯法并不是什么高效的算法**。
@@ -34,7 +40,7 @@
此时大家应该好奇了,都什么问题,这么牛逼,只能暴力搜索。
-## 回溯法解决的问题
+### 回溯法解决的问题
回溯法,一般可以解决如下几种问题:
@@ -55,7 +61,7 @@
记住组合无序,排列有序,就可以了。
-## 如何理解回溯法
+### 如何理解回溯法
**回溯法解决的问题都可以抽象为树形结构**,是的,我指的是所有回溯法的问题都可以抽象为树形结构!
@@ -66,7 +72,7 @@
这块可能初学者还不太理解,后面的回溯算法解决的所有题目中,我都会强调这一点并画图举相应的例子,现在有一个印象就行。
-## 回溯法模板
+### 回溯法模板
这里给出Carl总结的回溯算法模板。
@@ -173,3 +179,4 @@ void backtracking(参数) {
+
diff --git a/problems/栈与队列总结.md b/problems/栈与队列总结.md
index 31ce9554..e7f8ef86 100644
--- a/problems/栈与队列总结.md
+++ b/problems/栈与队列总结.md
@@ -3,6 +3,7 @@
+
diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md
index bd3191dc..7511ac87 100644
--- a/problems/背包理论基础01背包-1.md
+++ b/problems/背包理论基础01背包-1.md
@@ -8,8 +8,11 @@
# 动态规划:01背包理论基础
+## 算法公开课
-**《代码随想录》算法视频公开课:[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透0-1背包问题!](https://www.bilibili.com/video/BV1cg411g7Y6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这周我们正式开始讲解背包问题!
@@ -37,7 +40,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目,
之前可能有些录友已经可以熟练写出背包了,但只要把这个文章仔细看完,相信你会意外收获!
-## 01 背包
+### 01 背包
有n件物品和一个最多能背重量为w 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品只能用一次**,求解将哪些物品装入背包里物品价值总和最大。
@@ -67,7 +70,7 @@ leetcode上没有纯01背包的问题,都是01背包应用方面的题目,
以下讲解和图示中出现的数字都是以这个例子为例。
-## 二维dp数组01背包
+### 二维dp数组01背包
依然动规五部曲分析一波。
@@ -226,9 +229,6 @@ dp[i-1][j]和dp[i - 1][j - weight[i]] 都在dp[i][j]的左上角方向(包括
主要就是自己没有动手推导一下dp数组的演变过程,如果推导明白了,代码写出来就算有问题,只要把dp数组打印出来,对比一下和自己推导的有什么差异,很快就可以发现问题了。
-
-## 完整c++测试代码
-
```cpp
void test_2_wei_bag_problem1() {
vector
+
diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md
index 1a856bf5..50c2e5bf 100644
--- a/problems/背包问题理论基础多重背包.md
+++ b/problems/背包问题理论基础多重背包.md
@@ -144,8 +144,7 @@ int main() {
## 其他语言版本
-
-Java:
+### Java:
```Java
public void testMultiPack1(){
@@ -192,7 +191,7 @@ public void testMultiPack2(){
}
```
-Python:
+### Python:
改变物品数量为01背包格式(无参版)
```python
@@ -315,7 +314,7 @@ if __name__ == "__main__":
test_multi_pack(weight, value, nums, bagWeight)
```
-Go:
+### Go:
```go
package theory
@@ -406,7 +405,7 @@ func Test_multiplePack(t *testing.T) {
PASS
```
-TypeScript:
+### TypeScript:
> 版本一(改变数据源):
@@ -469,3 +468,4 @@ testMultiPack();
+
diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md
index 088a3d50..ac4c4a1c 100644
--- a/problems/背包问题理论基础完全背包.md
+++ b/problems/背包问题理论基础完全背包.md
@@ -7,9 +7,13 @@
# 动态规划:完全背包理论基础
-**《代码随想录》算法视频公开课:[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+## 算法公开课
-## 完全背包
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
+
+### 完全背包
有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品都有无限个(也就是可以放入背包多次)**,求解将哪些物品装入背包里物品价值总和最大。
@@ -113,8 +117,6 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量
}
```
-## C++测试代码
-
完整的C++测试代码如下:
```CPP
@@ -181,7 +183,7 @@ int main() {
## 其他语言版本
-Java:
+### Java:
```java
//先遍历物品,再遍历背包
@@ -221,9 +223,7 @@ private static void testCompletePackAnotherWay(){
-Python:
-
-
+### Python:
先遍历物品,再遍历背包(无参版)
```python
@@ -299,7 +299,8 @@ if __name__ == "__main__":
```
-Go:
+### Go:
+
```go
// test_CompletePack1 先遍历物品, 在遍历背包
@@ -352,7 +353,8 @@ func main() {
fmt.Println(test_CompletePack2(weight, price, 4))
}
```
-Javascript:
+### Javascript:
+
```Javascript
// 先遍历物品,再遍历背包容量
function test_completePack1() {
@@ -385,7 +387,7 @@ function test_completePack2() {
}
```
-TypeScript:
+### TypeScript:
```typescript
// 先遍历物品,再遍历背包容量
@@ -404,7 +406,7 @@ function test_CompletePack(): void {
test_CompletePack();
```
-Scala:
+### Scala:
```scala
// 先遍历物品,再遍历背包容量
@@ -426,7 +428,7 @@ object Solution {
}
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -468,3 +470,4 @@ fn test_complete_pack() {
+
diff --git a/problems/贪心算法总结篇.md b/problems/贪心算法总结篇.md
index d52e8551..c375503d 100644
--- a/problems/贪心算法总结篇.md
+++ b/problems/贪心算法总结篇.md
@@ -154,3 +154,4 @@ Carl个人认为:如果找出局部最优并可以推出全局最优,就是
+
diff --git a/problems/链表总结篇.md b/problems/链表总结篇.md
index dacd4dee..3f2f5c97 100644
--- a/problems/链表总结篇.md
+++ b/problems/链表总结篇.md
@@ -3,6 +3,7 @@