This commit is contained in:
Henry Zheng 2024-05-20 09:00:23 +08:00
commit 14809cd3b3
260 changed files with 8691 additions and 1258 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
**/.DS_Store

View File

@ -3,8 +3,8 @@
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)
> 1. **介绍** :本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
> 2. **正式出版** [《代码随想录》](https://programmercarl.com/other/publish.html) 。
> 3. **PDF版本** [「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html) 。
> 2. **正式出版** [《代码随想录》](https://programmercarl.com/qita/publish.html) 。
> 3. **PDF版本** [「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/qita/algo_pdf.html) 。
> 4. **算法公开课** [《代码随想录》算法视频公开课](https://www.bilibili.com/video/BV1fA4y1o715) 。
> 5. **最强八股文** [代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html) 。
> 6. **刷题顺序** README已经将刷题顺序排好了按照顺序一道一道刷就可以。
@ -14,7 +14,7 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="./pics/训练营.png" width="800"/>
</a>
</p>
@ -74,22 +74,9 @@
* 编程语言
* [C++面试&C++学习指南知识点整理](https://github.com/youngyangyang04/TechCPP)
* [C++语言基础课](https://kamacoder.com/course.php?course_id=1)
* [Java语言基础课](https://kamacoder.com/course.php?course_id=2)
* [编程语言基础课](https://kamacoder.com/courseshop.php)
* [23种设计模式](https://github.com/youngyangyang04/kama-DesignPattern)
* 项目
* [基于跳表的轻量级KV存储引擎](https://github.com/youngyangyang04/Skiplist-CPP)
* [Nosql数据库注入攻击系统](https://github.com/youngyangyang04/NoSQLAttack)
* 编程素养
* [看了这么多代码,谈一谈代码风格!](./problems/前序/代码风格.md)
* [力扣上的代码想在本地编译运行?](./problems/前序/力扣上的代码想在本地编译运行?.md)
* [什么是核心代码模式什么又是ACM模式](./problems/前序/什么是核心代码模式什么又是ACM模式.md)
* [刷题要不要用库函数](./problems/前序/刷力扣用不用库函数.md)
* [ACM模式如何构造二叉树](./problems/前序/ACM模式如何构建二叉树.md)
* [解密互联网大厂研发流程](./problems/前序/互联网大厂研发流程.md)
* 工具
* [一站式vim配置](https://github.com/youngyangyang04/PowerVim)
* [保姆级Git入门教程万字详解](https://mp.weixin.qq.com/s/Q_O0ey4C9tryPZaZeJocbA)
@ -103,13 +90,12 @@
* [BAT级别技术面试流程和注意事项都在这里了](./problems/前序/BAT级别技术面试流程和注意事项都在这里了.md)
* 算法性能分析
* [关于时间复杂度,你不知道的都在这里!](./problems/前序/关于时间复杂度,你不知道的都在这里!.md)
* [O(n)的算法居然超时了此时的n究竟是多大](./problems/前序/On的算法居然超时了此时的n究竟是多大.md)
* [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md)
* [本周小结!(算法性能分析系列一)](./problems/周总结/20201210复杂度分析周末总结.md)
* [关于空间复杂度,可能有几个疑问?](./problems/前序/关于空间复杂度,可能有几个疑问?.md)
* [关于时间复杂度,你不知道的都在这里!](./problems/前序/时间复杂度.md)
* [O(n)的算法居然超时了此时的n究竟是多大](./problems/前序/算法超时.md)
* [通过一道面试题目,讲一讲递归算法的时间复杂度!](./problems/前序/递归算法的时间复杂度.md)
* [关于空间复杂度,可能有几个疑问?](./problems/前序/空间复杂度.md)
* [递归算法的时间与空间复杂度分析!](./problems/前序/递归算法的时间与空间复杂度分析.md)
* [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md)
* [刷了这么多题,你了解自己代码的内存消耗么?](./problems/前序/内存消耗.md)
## 数组
@ -190,8 +176,9 @@
## 二叉树
题目分类大纲如下:
<img src='https://img-blog.csdnimg.cn/20210219190809451.png' width=600 alt='二叉树大纲'> </img></div>
<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20240424172231.png' width=600 alt='二叉树大纲'> </img></div>
1. [关于二叉树,你该了解这些!](./problems/二叉树理论基础.md)
2. [二叉树:二叉树的递归遍历](./problems/二叉树的递归遍历.md)
@ -233,7 +220,7 @@
题目分类大纲如下:
<img src='https://img-blog.csdnimg.cn/20210219192050666.png' width=600 alt='回溯算法大纲'> </img></div>
<img src='https://code-thinking-1253855093.file.myqcloud.com/pics/20240424172311.png' width=600 alt='回溯算法大纲'> </img></div>
1. [关于回溯算法,你该了解这些!](./problems/回溯算法理论基础.md)
2. [回溯算法77.组合](./problems/0077.组合.md)
@ -499,7 +486,7 @@
# 贡献者
[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本让更多的读者益于此项目。
[点此这里](https://github.com/youngyangyang04/leetcode-master/graphs/contributors)查看LeetCode-Master的所有贡献者。感谢他们补充了LeetCode-Master的其他语言版本让更多的读者益于此项目。
# Star 趋势
@ -507,24 +494,13 @@
# 关于作者
大家好我是程序员Carl哈工大师兄《代码随想录》作者先后在腾讯和百度从事后端技术研发。对算法和C++后端技术有一定的见解利用工作之余重新刷leetcode。
大家好我是程序员Carl哈工大师兄《代码随想录》作者先后在腾讯和百度从事后端技术底层技术研发。
加入「代码随想录」刷题小分队(微信群),可以扫下方二维码,加代码随想录客服微信。
# PDF下载
如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦**
添加如下企业微信会自动发送给大家PDF版本顺便可以选择是否加入刷题群。
添加微信记得备注,如果是已工作,备注:姓名-城市-岗位。如果学生,备注:姓名-学校-年级。**备注没有自我介绍不通过哦**
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/第二企业刷题活码.png" data-img="1" width="200" height="200"></img></div>
# 公众号
更多精彩文章持续更新微信搜索「代码随想录」第一时间围观关注后回复666可以获得我的所有算法专题原创PDF。
**来看看就知道了,你会发现相见恨晚!**
<a name="公众号"></a>
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20211026122841.png" data-img="1" width="650" height="500"></img></div>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 280 KiB

After

Width:  |  Height:  |  Size: 151 KiB

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 1. 两数之和
@ -349,6 +349,7 @@ function twoSum(nums: number[], target: number): number[] {
index = helperMap.get(target - nums[i]);
if (index !== undefined) {
resArr = [i, index];
break;
}
helperMap.set(nums[i], i);
}
@ -537,8 +538,8 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){
return NULL;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -677,8 +677,8 @@ public class Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,13 +1,11 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 用哈希表解决了[两数之和](https://programmercarl.com/0001.两数之和.html),那么三数之和呢?
# 第15题. 三数之和
[力扣题目链接](https://leetcode.cn/problems/3sum/)
@ -938,8 +936,8 @@ object Solution {
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 17.电话号码的字母组合
@ -260,7 +260,7 @@ class Solution {
}
//每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuilder
//每次迭代获取一个字符串,所以会涉及大量的字符串拼接,所以这里选择更为高效的 StringBuilder
StringBuilder temp = new StringBuilder();
//比如digits如果为"23",num 为0则str表示2对应的 abc
@ -274,7 +274,7 @@ class Solution {
String str = numString[digits.charAt(num) - '0'];
for (int i = 0; i < str.length(); i++) {
temp.append(str.charAt(i));
//c
//递归,处理下一层
backTracking(digits, numString, num + 1);
//剔除末尾的继续尝试
temp.deleteCharAt(temp.length() - 1);
@ -765,8 +765,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 一样的道理,能解决四数之和
@ -697,8 +697,8 @@ def four_sum(nums, target)
return result
end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -22,10 +22,12 @@
输入head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2
输入head = [1], n = 1
输出:[]
示例 3
输入head = [1,2], n = 1
@ -98,27 +100,32 @@ public:
### Java:
```java
public ListNode removeNthFromEnd(ListNode head, int n){
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//新建一个虚拟头节点指向head
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
//快慢指针指向虚拟头节点
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
// 只要快慢指针相差 n 个结点即可
for (int i = 0; i <= n; i++) {
fastIndex = fastIndex.next;
}
while (fastIndex != null) {
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
// 只要快慢指针相差 n 个结点即可
for (int i = 0; i < n ; i++){
fastIndex = fastIndex.next;
// 此时 slowIndex 的位置就是待删除元素的前一个位置。
// 具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
// 检查 slowIndex.next 是否为 null以避免空指针异常
if (slowIndex.next != null) {
slowIndex.next = slowIndex.next.next;
}
return dummyNode.next;
}
while (fastIndex != null){
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
//此时 slowIndex 的位置就是待删除元素的前一个位置。
//具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
slowIndex.next = slowIndex.next.next;
return dummyNode.next;
}
```
@ -187,16 +194,18 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
let ret = new ListNode(0, head),
slow = fast = ret;
while(n--) fast = fast.next;
while (fast.next !== null) {
fast = fast.next;
slow = slow.next
};
slow.next = slow.next.next;
return ret.next;
var removeNthFromEnd = function (head, n) {
// 创建哨兵节点,简化解题逻辑
let dummyHead = new ListNode(0, head);
let fast = dummyHead;
let slow = dummyHead;
while (n--) fast = fast.next;
while (fast.next !== null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummyHead.next;
};
```
### TypeScript:

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 24. 两两交换链表中的节点
@ -81,7 +81,7 @@ public:
上面的代码我第一次提交执行用时8ms打败6.5%的用户,差点吓到我了。
心想应该没有更好的方法了吧,也就$O(n)$的时间复杂度,重复提交几次,这样了:
心想应该没有更好的方法了吧,也就 $O(n)$ 的时间复杂度,重复提交几次,这样了:
![24.两两交换链表中的节点](https://code-thinking.cdn.bcebos.com/pics/24.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.png)
@ -181,6 +181,23 @@ class Solution {
}
```
```java
// 将步骤 2,3 交换顺序,这样不用定义 temp 节点
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode cur = dummy;
while (cur.next != null && cur.next.next != null) {
ListNode node1 = cur.next;// 第 1 个节点
ListNode node2 = cur.next.next;// 第 2 个节点
cur.next = node2; // 步骤 1
node1.next = node2.next;// 步骤 3
node2.next = node1;// 步骤 2
cur = cur.next.next;
}
return dummy.next;
}
```
### Python
```python
@ -285,6 +302,21 @@ var swapPairs = function (head) {
};
```
```javascript
// 递归版本
var swapPairs = function (head) {
if (head == null || head.next == null) {
return head;
}
let after = head.next;
head.next = swapPairs(after.next);
after.next = head;
return after;
};
```
### TypeScript
```typescript

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 27. 移除元素
@ -119,35 +119,6 @@ public:
* 时间复杂度O(n)
* 空间复杂度O(1)
```CPP
/**
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
* 时间复杂度O(n)
* 空间复杂度O(1)
*/
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int leftIndex = 0;
int rightIndex = nums.size() - 1;
while (leftIndex <= rightIndex) {
// 找左边等于val的元素
while (leftIndex <= rightIndex && nums[leftIndex] != val){
++leftIndex;
}
// 找右边不等于val的元素
while (leftIndex <= rightIndex && nums[rightIndex] == val) {
-- rightIndex;
}
// 将右边不等于val的元素覆盖左边等于val的元素
if (leftIndex < rightIndex) {
nums[leftIndex++] = nums[rightIndex--];
}
}
return leftIndex; // leftIndex一定指向了最终数组末尾的下一个元素
}
};
```
## 相关题目推荐
@ -276,6 +247,24 @@ class Solution:
### Go
```go
// 暴力法
// 时间复杂度 O(n^2)
// 空间复杂度 O(1)
func removeElement(nums []int, val int) int {
size := len(nums)
for i := 0; i < size; i ++ {
if nums[i] == val {
for j := i + 1; j < size; j ++ {
nums[j - 1] = nums[j]
}
i --
size --
}
}
return size
}
```
```go
// 快慢指针法
// 时间复杂度 O(n)
@ -318,7 +307,6 @@ func removeElement(nums []int, val int) int {
right--
}
}
fmt.Println(nums)
return left
}
```
@ -488,6 +476,32 @@ public class Solution {
}
```
###Dart:
```dart
int removeElement(List<int> nums, int val) {
//相向双指针法
var left = 0;
var right = nums.length - 1;
while (left <= right) {
//寻找左侧的val将其被右侧非val覆盖
if (nums[left] == val) {
while (nums[right] == val&&left<=right) {
right--;
if (right < 0) {
return 0;
}
}
nums[left] = nums[right--];
} else {
left++;
}
}
//覆盖后可以将0至left部分视为所需部分
return left;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 在一个串中查找是否出现过另一个串这是KMP的看家本领。
@ -564,6 +564,38 @@ public:
## 其他语言版本
### Java
```Java
class Solution {
/**
牺牲空间,换取最直白的暴力法
时间复杂度 O(n * m)
空间 O(n + m)
*/
public int strStr(String haystack, String needle) {
// 获取 haystack 和 needle 的长度
int n = haystack.length(), m = needle.length();
// 将字符串转换为字符数组,方便索引操作
char[] s = haystack.toCharArray(), p = needle.toCharArray();
// 遍历 haystack 字符串
for (int i = 0; i < n - m + 1; i++) {
// 初始化匹配的指针
int a = i, b = 0;
// 循环检查 needle 是否在当前位置开始匹配
while (b < m && s[a] == p[b]) {
// 如果当前字符匹配,则移动指针
a++;
b++;
}
// 如果 b 等于 m说明 needle 已经完全匹配,返回当前位置 i
if (b == m) return i;
}
// 如果遍历完毕仍未找到匹配的子串,则返回 -1
return -1;
}
}
```
```Java
class Solution {

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -268,8 +268,8 @@ var nextPermutation = function(nums) {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 34. 在排序数组中查找元素的第一个和最后一个位置
@ -233,7 +233,7 @@ class Solution {
if (index == -1) { // nums 中不存在 target直接返回 {-1, -1}
return new int[] {-1, -1}; // 匿名数组
}
// nums 中存在 targe则左右滑动指针来找到符合题意的区间
// nums 中存在 target,则左右滑动指针,来找到符合题意的区间
int left = index;
int right = index;
// 向左滑动,找左边界
@ -450,7 +450,7 @@ class Solution:
return -1
index = binarySearch(nums, target)
if index == -1:return [-1, -1] # nums 中不存在 target直接返回 {-1, -1}
# nums 中存在 targe则左右滑动指针来找到符合题意的区间
# nums 中存在 target,则左右滑动指针,来找到符合题意的区间
left, right = index, index
# 向左滑动,找左边界
while left -1 >=0 and nums[left - 1] == target: left -=1
@ -854,8 +854,8 @@ int* searchRange(int* nums, int numsSize, int target, int* returnSize){
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -313,18 +313,18 @@ func searchInsert(nums []int, target int) int {
```rust
impl Solution {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len();
while left < right {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
use std::cmp::Ordering::{Equal, Greater, Less};
let (mut left, mut right) = (0, nums.len() as i32 - 1);
while left <= right {
let mid = (left + right) / 2;
match nums[mid].cmp(&target) {
Ordering::Less => left = mid + 1,
Ordering::Equal => return ((left + right) / 2) as i32,
Ordering::Greater => right = mid,
match nums[mid as usize].cmp(&target) {
Less => left = mid + 1,
Equal => return mid,
Greater => right = mid - 1,
}
}
((left + right) / 2) as i32
right + 1
}
}
```
@ -332,6 +332,7 @@ impl Solution {
### Python
```python
# 第一种二分法: [left, right]左闭右闭区间
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
@ -348,6 +349,26 @@ class Solution:
return right + 1
```
```python
# 第二种二分法: [left, right)左闭右开区间
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while (left < right):
middle = (left + right) // 2
if nums[middle] > target:
right = middle
elif nums[middle] < target:
left = middle + 1
else:
return middle
return right
```
### JavaScript
```js
@ -527,8 +548,8 @@ int searchInsert(int* nums, int numsSize, int target){
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
> 如果对回溯法理论还不清楚的同学,可以先看这个视频[视频来了!!带你学透回溯算法(理论篇)](https://mp.weixin.qq.com/s/wDd5azGIYWjbU0fdua_qBg)
@ -87,7 +87,7 @@ bool backtracking(vector<vector<char>>& board)
![37.解数独](https://code-thinking-1253855093.file.myqcloud.com/pics/2020111720451790-20230310131822254.png)
在树形图中可以看出我们需要的是一个二维的递归也就是两个for循环嵌套着递归
在树形图中可以看出我们需要的是一个二维的递归 (一行一列
**一个for循环遍历棋盘的行一个for循环遍历棋盘的列一行一列确定下来之后递归遍历这个位置放9个数字的可能性**
@ -224,7 +224,7 @@ public:
### Java
解法一:
```java
class Solution {
public void solveSudoku(char[][] board) {
@ -291,7 +291,73 @@ class Solution {
}
}
```
解法二(bitmap标记)
```
class Solution{
int[] rowBit = new int[9];
int[] colBit = new int[9];
int[] square9Bit = new int[9];
public void solveSudoku(char[][] board) {
// 1 10 11
for (int y = 0; y < board.length; y++) {
for (int x = 0; x < board[y].length; x++) {
int numBit = 1 << (board[y][x] - '1');
rowBit[y] ^= numBit;
colBit[x] ^= numBit;
square9Bit[(y / 3) * 3 + x / 3] ^= numBit;
}
}
backtrack(board, 0);
}
public boolean backtrack(char[][] board, int n) {
if (n >= 81) {
return true;
}
// 快速算出行列编号 n/9 n%9
int row = n / 9;
int col = n % 9;
if (board[row][col] != '.') {
return backtrack(board, n + 1);
}
for (char c = '1'; c <= '9'; c++) {
int numBit = 1 << (c - '1');
if (!isValid(numBit, row, col)) continue;
{
board[row][col] = c; // 当前的数字放入到数组之中,
rowBit[row] ^= numBit; // 第一行rowBit[0],第一个元素eg: 1 , 0^1=1,第一个元素:4, 100^1=101,...
colBit[col] ^= numBit;
square9Bit[(row / 3) * 3 + col / 3] ^= numBit;
}
if (backtrack(board, n + 1)) return true;
{
board[row][col] = '.'; // 不满足条件,回退成'.'
rowBit[row] &= ~numBit; // 第一行rowBit[0],第一个元素eg: 1 , 101&=~1==>101&111111110==>100
colBit[col] &= ~numBit;
square9Bit[(row / 3) * 3 + col / 3] &= ~numBit;
}
}
return false;
}
boolean isValid(int numBit, int row, int col) {
// 左右
if ((rowBit[row] & numBit) > 0) return false;
// 上下
if ((colBit[col] & numBit) > 0) return false;
// 9宫格: 快速算出第n个九宫格,编号[0,8] , 编号=(row / 3) * 3 + col / 3
if ((square9Bit[(row / 3) * 3 + col / 3] & numBit) > 0) return false;
return true;
}
}
```
### Python
```python
@ -810,8 +876,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -311,7 +311,7 @@ class Solution:
for i in range(startIndex, len(candidates)):
if total + candidates[i] > target:
continue
break
total += candidates[i]
path.append(candidates[i])
self.backtracking(candidates, target, total, i, path, result)
@ -660,8 +660,8 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了!
@ -806,8 +806,8 @@ public class Solution
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -440,6 +440,33 @@ class Solution {
}
```
双指针优化
```java
class Solution {
public int trap(int[] height) {
if (height.length <= 2) {
return 0;
}
// 从两边向中间寻找最值
int maxLeft = height[0], maxRight = height[height.length - 1];
int l = 1, r = height.length - 2;
int res = 0;
while (l <= r) {
// 不确定上一轮是左边移动还是右边移动,所以两边都需更新最值
maxLeft = Math.max(maxLeft, height[l]);
maxRight = Math.max(maxRight, height[r]);
// 最值较小的一边所能装的水量已定,所以移动较小的一边。
if (maxLeft < maxRight) {
res += maxLeft - height[l ++];
} else {
res += maxRight - height[r --];
}
}
return res;
}
}
```
单调栈法
```java

View File

@ -1,10 +1,10 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心准备!
> 相对于[贪心算法:跳跃游戏](https://mp.weixin.qq.com/s/606_N9j8ACKCODoCbV1lSA)难了不少,做好心准备!
# 45.跳跃游戏 II
@ -541,8 +541,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 46.全排列
@ -518,8 +518,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -554,8 +554,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 51. N皇后
@ -920,8 +920,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -306,8 +306,8 @@ class Solution {
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 53. 最大子序和

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 53. 最大子序和
@ -243,8 +243,8 @@ function maxSubArray(nums: number[]): number {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 55. 跳跃游戏

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 56. 合并区间

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
# 62.不同路径
@ -285,6 +285,24 @@ public:
}
```
状态压缩
```java
class Solution {
public int uniquePaths(int m, int n) {
// 在二维dp数组中当前值的计算只依赖正上方和正左方因此可以压缩成一维数组。
int[] dp = new int[n];
// 初始化,第一行只能从正左方跳过来,所以只有一条路径。
Arrays.fill(dp, 1);
for (int i = 1; i < m; i ++) {
// 第一列也只有一条路,不用迭代,所以从第二列开始
for (int j = 1; j < n; j ++) {
dp[j] += dp[j - 1]; // dp[j] = dp[j] (正上方)+ dp[j - 1] (正左方)
}
}
return dp[n - 1];
}
}
```
### Python
递归
@ -576,8 +594,8 @@ public class Solution
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
# 63. 不同路径 II
@ -145,7 +145,7 @@ public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍直接返回0
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍直接返回0
return 0;
vector<vector<int>> dp(m, vector<int>(n, 0));
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1;
@ -759,8 +759,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 70. 爬楼梯
@ -519,8 +519,8 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 70. 爬楼梯(进阶版)
@ -211,10 +211,33 @@ func main() {
```
### JavaScript:
```javaScript
var climbStairs = function (n) {
let dp = new Array(n + 1).fill(0);
dp[0] = 1;
// 排列题,注意循环顺序,背包在外物品在内
for (let j = 1; j <= n; j++) {//遍历背包
for (let i = 1; i <= 2; i++) {//遍历物品
if (j - i >= 0) dp[j] = dp[j] + dp[j - i];
}
}
return dp[n];
}
```
### TypeScript
```typescript
var climbStairs = function (n: number): number {
let dp: number[] = new Array(n + 1).fill(0);
dp[0] = 1;
for (let j = 1; j <= n; j++) {//遍历背包
for (let i = 1; i <= 2; i++) {//遍历物品
if (j - i >= 0) dp[j] = dp[j] + dp[j - i];
}
}
return dp[n];
}
```
### Rust:

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 72. 编辑距离

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
# 第77题. 组合
@ -469,28 +469,58 @@ func dfs(n int, k int, start int) {
```
### Javascript
未剪枝:
```js
var combine = function (n, k) {
// 回溯法
let result = [],
path = [];
let backtracking = (_n, _k, startIndex) => {
// 终止条件
if (path.length === _k) {
result.push(path.slice());
return;
}
// 循环本层集合元素
for (let i = startIndex; i <= _n; i++) {
path.push(i);
// 递归
backtracking(_n, _k, i + 1);
// 回溯操作
path.pop();
}
};
backtracking(n, k, 1);
return result;
};
```
剪枝:
```javascript
let result = []
let path = []
var combine = function(n, k) {
result = []
combineHelper(n, k, 1)
return result
var combine = function (n, k) {
// 回溯法
let result = [],
path = [];
let backtracking = (_n, _k, startIndex) => {
// 终止条件
if (path.length === _k) {
result.push(path.slice());
return;
}
// 循环本层集合元素
for (let i = startIndex; i <= _n - (_k - path.length) + 1; i++) {
path.push(i);
// 递归
backtracking(_n, _k, i + 1);
// 回溯操作
path.pop();
}
};
backtracking(n, k, 1);
return result;
};
const combineHelper = (n, k, startIndex) => {
if (path.length === k) {
result.push([...path])
return
}
for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
path.push(i)
combineHelper(n, k, i + 1)
path.pop()
}
}
```
### TypeScript
@ -845,8 +875,8 @@ public class Solution
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -411,8 +411,8 @@ object Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 78.子集
@ -466,8 +466,8 @@ public class Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 84.柱状图中最大的矩形
@ -741,8 +741,8 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 90.子集II
@ -659,8 +659,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -848,8 +848,8 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
# 96.不同的二叉搜索树

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 98.验证二叉搜索树
@ -806,8 +806,8 @@ public bool IsValidBST(TreeNode root)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -339,8 +339,8 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 101. 对称二叉树
@ -945,8 +945,8 @@ public bool IsSymmetric(TreeNode root)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
# 二叉树层序遍历登场!
@ -129,7 +129,7 @@ class Solution {
return resList;
}
//DFS--递归方式
//BFS--递归方式
public void checkFun01(TreeNode node, Integer deep) {
if (node == null) return;
deep++;
@ -692,27 +692,29 @@ func levelOrderBottom(root *TreeNode) [][]int {
#### Javascript:
```javascript
var levelOrderBottom = function(root) {
let res = [], queue = [];
queue.push(root);
while(queue.length && root!==null) {
// 存放当前层级节点数组
let curLevel = [];
// 计算当前层级节点数量
let length = queue.length;
while(length--) {
let node = queue.shift();
// 把当前层节点存入curLevel数组
curLevel.push(node.val);
// 把下一层级的左右节点存入queue队列
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
// 从数组前头插入值,避免最后反转数组,减少运算时间
res.unshift(curLevel);
var levelOrderBottom = function (root) {
let res = [],
queue = [];
queue.push(root);
while (queue.length && root !== null) {
// 存放当前层级节点数组
let curLevel = [];
// 计算当前层级节点数量
let length = queue.length;
while (length--) {
let node = queue.shift();
// 把当前层节点存入curLevel数组
curLevel.push(node.val);
// 把下一层级的左右节点存入queue队列
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
return res;
// 从数组前头插入值,避免最后反转数组,减少运算时间
res.unshift(curLevel);
}
return res;
};
```
#### TypeScript:
@ -1140,7 +1142,7 @@ impl Solution {
### 思路
本题就是层序遍历的时候把一层求个总和取一个均值。
本题就是层序遍历的时候把一层求个总和取一个均值。
C++代码:
@ -1295,26 +1297,26 @@ func averageOfLevels(root *TreeNode) []float64 {
```javascript
var averageOfLevels = function(root) {
//层级平均值
let res = [], queue = [];
queue.push(root);
while(queue.length && root!==null) {
//每一层节点个数
let length = queue.length;
//sum记录每一层的和
let sum = 0;
for(let i=0; i < length; i++) {
let node = queue.shift();
sum += node.val;
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
//每一层的平均值存入数组res
res.push(sum/length);
let res = [],
queue = [];
queue.push(root);
while (queue.length) {
// 每一层节点个数;
let lengthLevel = queue.length,
len = queue.length,
// sum记录每一层的和;
sum = 0;
while (lengthLevel--) {
const node = queue.shift();
sum += node.val;
// 队列存放下一层节点
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
return res;
// 求平均值
res.push(sum / len);
}
return res;
};
```
@ -1925,26 +1927,28 @@ func max(x, y int) int {
#### Javascript
```javascript
var largestValues = function(root) {
//使用层序遍历
let res = [], queue = [];
queue.push(root);
while(root !== null && queue.length) {
//设置max初始值就是队列的第一个元素
let max = queue[0].val;
let length = queue.length;
while(length--) {
let node = queue.shift();
max = max > node.val ? max : node.val;
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
//把每一层的最大值放到res数组
res.push(max);
}
var largestValues = function (root) {
let res = [],
queue = [];
queue.push(root);
if (root === null) {
return res;
}
while (queue.length) {
let lengthLevel = queue.length,
// 初始值设为负无穷大
max = -Infinity;
while (lengthLevel--) {
const node = queue.shift();
// 在当前层中找到最大值
max = Math.max(max, node.val);
// 找到下一层的节点
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
res.push(max);
}
return res;
};
```
@ -2805,21 +2809,23 @@ func maxDepth(root *TreeNode) int {
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
// 最大的深度就是二叉树的层数
if (root === null) return 0;
let queue = [root];
let height = 0;
while (queue.length) {
let n = queue.length;
height++;
for (let i=0; i<n; i++) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
var maxDepth = function (root) {
// 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
let max = 0,
queue = [root];
if (root === null) {
return max;
}
while (queue.length) {
max++;
let length = queue.length;
while (length--) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
return height;
}
return max;
};
```
@ -3226,8 +3232,8 @@ impl Solution {
**致敬叶师傅!**
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -77,7 +77,7 @@ return depth;
所以整体c++代码如下:
```CPP
class solution {
class Solution {
public:
int getdepth(TreeNode* node) {
if (node == NULL) return 0;
@ -94,7 +94,7 @@ public:
代码精简之后c++代码如下:
```CPP
class solution {
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == null) return 0;
@ -110,7 +110,7 @@ public:
本题当然也可以使用前序,代码如下:(**充分表现出求深度回溯的过程**)
```CPP
class solution {
class Solution {
public:
int result;
void getdepth(TreeNode* node, int depth) {
@ -144,7 +144,7 @@ public:
注意以上代码是为了把细节体现出来,简化一下代码如下:
```CPP
class solution {
class Solution {
public:
int result;
void getdepth(TreeNode* node, int depth) {
@ -183,7 +183,7 @@ public:
c++代码如下:
```CPP
class solution {
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
@ -232,7 +232,7 @@ public:
c++代码:
```CPP
class solution {
class Solution {
public:
int maxDepth(Node* root) {
if (root == 0) return 0;
@ -249,7 +249,7 @@ public:
依然是层序遍历,代码如下:
```CPP
class solution {
class Solution {
public:
int maxDepth(Node* root) {
queue<Node*> que;
@ -278,7 +278,7 @@ public:
104.二叉树的最大深度
```java
class solution {
class Solution {
/**
* 递归法
*/
@ -319,7 +319,7 @@ class Solution {
```
```java
class solution {
class Solution {
/**
* 迭代法,使用层序遍历
*/
@ -369,7 +369,7 @@ class Solution {
```
```java
class solution {
class Solution {
/**
* 迭代法,使用层序遍历
*/
@ -402,7 +402,7 @@ class solution {
递归法:
```python
class solution:
class Solution:
def maxdepth(self, root: treenode) -> int:
return self.getdepth(root)
@ -417,7 +417,7 @@ class solution:
递归法:精简代码
```python
class solution:
class Solution:
def maxdepth(self, root: treenode) -> int:
if not root:
return 0
@ -1159,8 +1159,8 @@ public int MaxDepth(TreeNode root)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -1242,8 +1242,8 @@ public TreeNode BuildTree(int[] inorder, int[] postorder)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 构造二叉搜索树,一不小心就平衡了
@ -334,6 +334,18 @@ class Solution:
return root
```
递归 精简(自身调用)
```python
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:
return
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid + 1 :])
return root
```
迭代法
```python
@ -549,8 +561,8 @@ public TreeNode Traversal(int[] nums, int left, int right)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -934,8 +934,8 @@ public int GetHeight(TreeNode root)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 和求最大深度一个套路?
@ -64,7 +64,7 @@
代码如下:
```
```CPP
int getDepth(TreeNode* node)
```
@ -74,14 +74,14 @@ int getDepth(TreeNode* node)
代码如下:
```
```CPP
if (node == NULL) return 0;
```
3. 确定单层递归的逻辑
这块和求最大深度可就不一样了,一些同学可能会写如下代码:
```
```CPP
int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
int result = 1 + min(leftDepth, rightDepth);

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
# 112. 路径总和
@ -1579,8 +1579,8 @@ public class Solution {
// @lc code=end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 115.不同的子序列
@ -375,8 +375,8 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 116. 填充每个节点的下一个右侧节点指针
@ -438,8 +438,8 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 121. 买卖股票的最佳时机
@ -287,9 +287,6 @@ class Solution {
return dp[1];
}
}
```
```Java
```
### Python:

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 122.买卖股票的最佳时机 II
@ -422,8 +422,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 122.买卖股票的最佳时机II
@ -455,8 +455,8 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 123.买卖股票的最佳时机III

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 127. 单词接龙

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -382,8 +382,8 @@ int sumNumbers(struct TreeNode* root){
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 130. 被围绕的区域
@ -792,8 +792,8 @@ impl Solution {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 切割问题其实是一种组合问题!
@ -952,8 +952,8 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -372,8 +372,8 @@ var minCut = function(s) {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 134. 加油站
@ -654,8 +654,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 135. 分发糖果
@ -401,8 +401,8 @@ public class Solution
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -394,7 +394,28 @@ class Solution:
dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j])
return dp[len(s)]
```
DP剪枝
```python
class Solution(object):
def wordBreak(self, s, wordDict):
# 先对单词按长度排序
wordDict.sort(key=lambda x: len(x))
n = len(s)
dp = [False] * (n + 1)
dp[0] = True
# 遍历背包
for i in range(1, n + 1):
# 遍历单词
for word in wordDict:
# 简单的 “剪枝”
if len(word) > i:
break
dp[i] = dp[i] or (dp[i - len(word)] and s[i - len(word): i] == word)
return dp[-1]
```
### Go

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 141. 环形链表
@ -159,8 +159,8 @@ function hasCycle(head: ListNode | null): boolean {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 143.重排链表

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 这不仅仅是一道好题,也展现出计算机的思考方式
@ -169,8 +169,12 @@ class Solution {
```python
from operator import add, sub, mul
class Solution:
op_map = {'+': add, '-': sub, '*': mul, '/': lambda x, y: int(x / y)}
def div(x, y):
# 使用整数除法的向零取整方式
return int(x / y) if x * y > 0 else -(abs(x) // abs(y))
class Solution(object):
op_map = {'+': add, '-': sub, '*': mul, '/': div}
def evalRPN(self, tokens: List[str]) -> int:
stack = []
@ -186,18 +190,31 @@ class Solution:
另一种可行但因为使用eval相对较慢的方法:
```python
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
from operator import add, sub, mul
def div(x, y):
# 使用整数除法的向零取整方式
return int(x / y) if x * y > 0 else -(abs(x) // abs(y))
class Solution(object):
op_map = {'+': add, '-': sub, '*': mul, '/': div}
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
for item in tokens:
if item not in {"+", "-", "*", "/"}:
stack.append(item)
for token in tokens:
if token in self.op_map:
op1 = stack.pop()
op2 = stack.pop()
operation = self.op_map[token]
stack.append(operation(op2, op1))
else:
first_num, second_num = stack.pop(), stack.pop()
stack.append(
int(eval(f'{second_num} {item} {first_num}')) # 第一个出来的在运算符后面
)
return int(stack.pop()) # 如果一开始只有一个数,那么会是字符串形式的
stack.append(int(token))
return stack.pop()
```

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
同:[链表:链表相交](https://programmercarl.com/面试题02.07.链表相交.html)
<p align="center">

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 188.买卖股票的最佳时机IV
@ -553,8 +553,8 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 189. 旋转数组
@ -201,8 +201,8 @@ function reverseByRange(nums: number[], left: number, right: number): void {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 198.打家劫舍
@ -360,8 +360,8 @@ impl Solution {
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 200. 岛屿数量
@ -408,9 +408,9 @@ impl Solution {
}
}
```
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>
```

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 200. 岛屿数量

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -533,8 +533,8 @@ public class Solution {
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -497,27 +497,67 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
```php
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
// 虚拟头+双指针
func removeElements(head *ListNode, val int) *ListNode {
dummyHead := &ListNode{}
dummyHead.Next = head
pred := dummyHead
cur := head
for cur != nil {
if cur.Val == val {
pred.Next = cur.Next
} else {
pred = cur
//版本一(在原链表上直接删除):
class Solution {
/**
* @param ListNode $head
* @param Integer $val
* @return ListNode
*/
function removeElements($head, $val)
{
if ($head == null) {
return null;
}
cur = cur.Next
$now = $head;
while ($now->next != null) {
if ($now->next->val == $val) {
$now->next = $now->next->next;
} else {
$now = $now->next;
}
}
if ($head->val == $val) {
return $head->next;
}
return $head;
}
}
//版本二(虚拟头结点方式):
class Solution {
/**
* @param ListNode $head
* @param Integer $val
* @return ListNode
*/
function removeElements($head, $val)
{
$dummyHead = new ListNode(0, $head);
$now = $dummyHead;
while ($now->next != null){
if ($now->next->val == $val) {
$now->next = $now->next->next;
} else {
$now = $now->next;
}
}
return $dummyHead->next;
}
return dummyHead.Next
}
```
@ -545,7 +585,7 @@ impl Solution {
let mut dummyHead = Box::new(ListNode::new(0));
dummyHead.next = head;
let mut cur = dummyHead.as_mut();
// 使用take()替换std::men::replace(&mut node.next, None)达到相同的效果,并且更普遍易读
// 使用take()替换std::mem::replace(&mut node.next, None)达到相同的效果,并且更普遍易读
while let Some(nxt) = cur.next.take() {
if nxt.val == val {
cur.next = nxt.next;
@ -664,8 +704,8 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 205. 同构字符串
@ -179,8 +179,8 @@ function isIsomorphic(s: string, t: string): boolean {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 反转链表的写法很简单,一些同学甚至可以背下来但过一阵就忘了该咋写,主要是因为没有理解真正的反转过程。

View File

@ -0,0 +1,65 @@
<p align="center">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
拓扑排序指的是一种 解决问题的大体思路, 而具体算法,可能是 广搜 可能是深搜。
大家可能发现 各式各样的解法,纠结哪个是拓扑排序?
只要能在把 有向无环图 进行线性排序 的算法 都可以叫做 拓扑排序。
引用与任务调度,课程安排等等。
「拓扑排序」是专门应用于有向图的算法;
把一个 有向无环图 转成 线性的排序 就叫 拓扑排序。
拓扑排序Kahn 算法,其实就是广度优先遍历的思路)
这道题的做法同样适用于第 210 题。
```CPP
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> inDegree(numCourses, 0);
unordered_map<int, vector<int>> umap;
for (int i = 0; i < prerequisites.size(); i++) {
// prerequisites[i][0] 是 课程入度prerequisites[i][1] 是课程出度
// 即: 上课prerequisites[i][0] 之前必须先上课prerequisites[i][1]
// prerequisites[i][1] -> prerequisites[i][0]
inDegree[prerequisites[i][0]]++;//当前课程入度值+1
umap[prerequisites[i][1]].push_back(prerequisites[i][0]); // 添加 prerequisites[i][1] 指向的课程
}
queue<int> que;
for (int i = 0; i < numCourses; i++) {
if (inDegree[i] == 0) que.push(i); // 所有入度为0即为 开头课程 加入队列
}
int count = 0;
while (que.size()) {
int cur = que.front(); //当前选的课
que.pop();
count++; // 选课数+1
vector<int> courses = umap[cur]; //获取这门课指向的课程,也就是这么课的后续课
if (courses.size()) { // 有后续课
for (int i = 0; i < courses.size(); i++) {
inDegree[courses[i]]--; // 它的后续课的入度-1
if (inDegree[courses[i]] == 0) que.push(courses[i]); // 如果入度为0加入队列
}
}
}
if (count == numCourses) return true;
return false;
}
};
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 209.长度最小的子数组
@ -270,22 +270,21 @@ var minSubArrayLen = function(target, nums) {
```typescript
function minSubArrayLen(target: number, nums: number[]): number {
let left: number = 0, right: number = 0;
let res: number = nums.length + 1;
let sum: number = 0;
while (right < nums.length) {
sum += nums[right];
if (sum >= target) {
// 不断移动左指针,直到不能再缩小为止
while (sum - nums[left] >= target) {
sum -= nums[left++];
}
res = Math.min(res, right - left + 1);
}
right++;
let left: number = 0,
res: number = Infinity,
subLen: number = 0,
sum: number = 0;
for (let right: number = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
subLen = right - left + 1;
res = Math.min(res, subLen);
sum -= nums[left];
left++;
}
return res === nums.length + 1 ? 0 : res;
};
}
return res === Infinity ? 0 : res;
}
```
### Swift:

View File

@ -0,0 +1,39 @@
```CPP
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> inDegree(numCourses, 0);
vector<int> result;
unordered_map<int, vector<int>> umap;
for (int i = 0; i < prerequisites.size(); i++) {
// prerequisites[i][0] 是 课程入度prerequisites[i][1] 是课程出度
// 即: 上课prerequisites[i][0] 之前必须先上课prerequisites[i][1]
// prerequisites[i][1] -> prerequisites[i][0]
inDegree[prerequisites[i][0]]++;//当前课程入度值+1
umap[prerequisites[i][1]].push_back(prerequisites[i][0]); // 添加 prerequisites[i][1] 指向的课程
}
queue<int> que;
for (int i = 0; i < numCourses; i++) {
if (inDegree[i] == 0) que.push(i); // 所有入度为0即为 开头课程 加入队列
}
int count = 0;
while (que.size()) {
int cur = que.front(); //当前选的课
que.pop();
count++; // 选课数+1
result.push_back(cur);
vector<int> courses = umap[cur]; //获取这门课指向的课程,也就是这么课的后续课
if (courses.size()) { // 有后续课
for (int i = 0; i < courses.size(); i++) {
inDegree[courses[i]]--; // 它的后续课的入度-1
if (inDegree[courses[i]] == 0) que.push(courses[i]); // 如果入度为0加入队列
}
}
}
if (count == numCourses) return result;
else return vector<int>();
}
};
```

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 213.打家劫舍II

View File

@ -1,9 +1,9 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
@ -417,6 +417,7 @@ func dfs(k, n int, start int, sum int) {
```
### JavaScript
- 未剪枝:
```js
/**
@ -424,32 +425,74 @@ func dfs(k, n int, start int, sum int) {
* @param {number} n
* @return {number[][]}
*/
var combinationSum3 = function(k, n) {
let res = [];
let path = [];
let sum = 0;
const dfs = (path,index) => {
// 剪枝操作
if (sum > n){
return
}
if (path.length == k) {
if(sum == n){
res.push([...path]);
return
}
}
for (let i = index; i <= 9 - (k-path.length) + 1;i++) {
path.push(i);
sum = sum + i;
index += 1;
dfs(path,index);
sum -= i
path.pop()
}
var combinationSum3 = function (k, n) {
// 回溯法
let result = [],
path = [];
const backtracking = (_k, targetSum, sum, startIndex) => {
// 终止条件
if (path.length === _k) {
if (sum === targetSum) {
result.push(path.slice());
}
// 如果总和不相等,就直接返回
return;
}
dfs(path,1);
return res
// 循环当前节点因为只使用数字1到9所以最大是9
for (let i = startIndex; i <= 9; i++) {
path.push(i);
sum += i;
// 回调函数
backtracking(_k, targetSum, sum, i + 1);
// 回溯
sum -= i;
path.pop();
}
};
backtracking(k, n, 0, 1);
return result;
};
```
- 剪枝:
```js
/**
* @param {number} k
* @param {number} n
* @return {number[][]}
*/
var combinationSum3 = function (k, n) {
// 回溯法
let result = [],
path = [];
const backtracking = (_k, targetSum, sum, startIndex) => {
if (sum > targetSum) {
return;
}
// 终止条件
if (path.length === _k) {
if (sum === targetSum) {
result.push(path.slice());
}
// 如果总和不相等,就直接返回
return;
}
// 循环当前节点因为只使用数字1到9所以最大是9
for (let i = startIndex; i <= 9 - (_k - path.length) + 1; i++) {
path.push(i);
sum += i;
// 回调函数
backtracking(_k, targetSum, sum, i + 1);
// 回溯
sum -= i;
path.pop();
}
};
backtracking(k, n, 0, 1);
return result;
};
```

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 222.完全二叉树的节点个数
@ -54,7 +54,7 @@
1. 确定递归函数的参数和返回值参数就是传入树的根节点返回就返回以该节点为根节点二叉树的节点数量所以返回值为int类型。
代码如下:
```
```CPP
int getNodesNum(TreeNode* cur) {
```
@ -62,7 +62,7 @@ int getNodesNum(TreeNode* cur) {
代码如下:
```
```CPP
if (cur == NULL) return 0;
```
@ -70,7 +70,7 @@ if (cur == NULL) return 0;
代码如下:
```
```CPP
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
@ -893,8 +893,8 @@ public int CountNodes(TreeNode root)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
@ -1249,8 +1249,8 @@ impl MyStack {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 226.翻转二叉树
@ -1028,8 +1028,8 @@ public TreeNode InvertTree(TreeNode root) {
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 工作上一定没人这么搞,但是考察对栈、队列理解程度的好题

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 234.回文链表
@ -428,8 +428,8 @@ function reverseList(head: ListNode | null): ListNode | null {
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 235. 二叉搜索树的最近公共祖先
@ -547,8 +547,8 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 本来是打算将二叉树和二叉搜索树的公共祖先问题一起讲,后来发现篇幅过长了,只能先说一说二叉树的公共祖先问题。
@ -247,7 +247,7 @@ public:
### Java
递归
```Java
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
@ -271,6 +271,47 @@ class Solution {
}
}
```
迭代
```Java
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
int max = Integer.MAX_VALUE;
Stack<TreeNode> st = new Stack<>();
TreeNode cur = root, pre = null;
while (cur != null || !st.isEmpty()) {
while (cur != null) {
st.push(cur);
cur = cur.left;
}
cur = st.pop();
if (cur.right == null || cur.right == pre) {
// p/q是 中/左 或者 中/右 , 返回中
if (cur == p || cur == q) {
if ((cur.left != null && cur.left.val == max) || (cur.right != null && cur.right.val == max)) {
return cur;
}
cur.val = max;
}
// p/q是 左/右 , 返回中
if (cur.left != null && cur.left.val == max && cur.right != null && cur.right.val == max) {
return cur;
}
// MAX_VALUE 往上传递
if ((cur.left != null && cur.left.val == max) || (cur.right != null && cur.right.val == max)) {
cur.val = max;
}
pre = cur;
cur = null;
} else {
st.push(cur);
cur = cur.right;
}
}
return null;
}
}
```
### Python
@ -445,8 +486,8 @@ public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的
@ -390,8 +390,8 @@ object Solution {
* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
> 以为只用了递归,其实还用了回溯
@ -40,7 +40,7 @@
要传入根节点记录每一条路径的path和存放结果集的result这里递归不需要返回值代码如下
```
```CPP
void traversal(TreeNode* cur, vector<int>& path, vector<string>& result)
```
@ -48,7 +48,7 @@ void traversal(TreeNode* cur, vector<int>& path, vector<string>& result)
在写递归的时候都习惯了这么写:
```
```CPP
if (cur == NULL) {
终止处理逻辑
}
@ -59,7 +59,7 @@ if (cur == NULL) {
**那么什么时候算是找到了叶子节点?** 是当 cur不为空其左右孩子都为空的时候就找到叶子节点。
所以本题的终止条件是:
```
```CPP
if (cur->left == NULL && cur->right == NULL) {
终止处理逻辑
}
@ -102,7 +102,7 @@ if (cur->left == NULL && cur->right == NULL) { // 遇到叶子节点
所以递归前要加上判断语句,下面要递归的节点是否为空,如下
```
```CPP
if (cur->left) {
traversal(cur->left, path, result);
}

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 279.完全平方数
@ -219,7 +219,7 @@ class Solution {
### Python
先遍历物品, 再遍历背包
先遍历背包, 再遍历物品
```python
class Solution:
def numSquares(self, n: int) -> int:
@ -234,7 +234,7 @@ class Solution:
return dp[n]
```
先遍历背包, 再遍历物品
先遍历物品, 再遍历背包
```python
class Solution:
def numSquares(self, n: int) -> int:
@ -271,7 +271,27 @@ class Solution:
# 返回结果
return dp[n]
```
```python
class Solution(object):
def numSquares(self, n):
# 先把可以选的数准备好,更好理解
nums, num = [], 1
while num ** 2 <= n:
nums.append(num ** 2)
num += 1
# dp数组初始化
dp = [float('inf')] * (n + 1)
dp[0] = 0
# 遍历准备好的完全平方数
for i in range(len(nums)):
# 遍历背包容量
for j in range(nums[i], n+1):
dp[j] = min(dp[j], dp[j-nums[i]]+1)
# 返回结果
return dp[-1]
```
### Go
@ -389,7 +409,7 @@ function numSquares(n: number): number {
};
```
## C
### C
```c
#define min(a, b) ((a) > (b) ? (b) : (a))

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 283. 移动零:动态规划:一样的套路,再求一次完全平方数
@ -172,8 +172,8 @@ void moveZeroes(int* nums, int numsSize){
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

View File

@ -1,8 +1,8 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们益!</strong></p>
# 300.最长递增子序列
@ -129,6 +129,7 @@ public:
```Java
class Solution {
public int lengthOfLIS(int[] nums) {
if (nums.length <= 1) return nums.length;
int[] dp = new int[nums.length];
int res = 1;
Arrays.fill(dp, 1);
@ -137,8 +138,8 @@ class Solution {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
res = Math.max(res, dp[i]);
}
res = Math.max(res, dp[i]);
}
return res;
}

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