mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
29
README.md
29
README.md
@ -56,6 +56,8 @@
|
||||
* [字符串:听说你对KMP有这些疑问?](https://mp.weixin.qq.com/s/mqx6IM2AO4kLZwvXdPtEeQ)
|
||||
* [字符串:KMP算法还能干这个!](https://mp.weixin.qq.com/s/lR2JPtsQSR2I_9yHbBmBuQ)
|
||||
* [字符串:前缀表不右移,难道就写不出KMP了?](https://mp.weixin.qq.com/s/p3hXynQM2RRROK5c6X7xfw)
|
||||
* [字符串:总结篇!](https://mp.weixin.qq.com/s/gtycjyDtblmytvBRFlCZJg)
|
||||
* [栈与队列:来看看栈和队列不为人知的一面](https://mp.weixin.qq.com/s/VZRjOccyE09aE-MgLbCMjQ)
|
||||
|
||||
(持续更新中....)
|
||||
|
||||
@ -66,11 +68,11 @@
|
||||
这里我总结了各个类型的经典题目,**初学者可以按照如下顺序来刷题**,算法老手可以按照这个list查缺补漏!
|
||||
|
||||
* 数组经典题目
|
||||
* [0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md)
|
||||
* [0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md)
|
||||
* [0035.搜索插入位置](https://mp.weixin.qq.com/s/fCf5QbPDtE6SSlZ1yh_q8Q)
|
||||
* [0027.移除元素](https://mp.weixin.qq.com/s/wj0T-Xs88_FHJFwayElQlA)
|
||||
* [0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md)
|
||||
* [0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md)
|
||||
* [0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md)
|
||||
* [0209.长度最小的子数组](https://mp.weixin.qq.com/s/UrZynlqi4QpyLlLhBPglyg)
|
||||
* [0059.螺旋矩阵II](https://mp.weixin.qq.com/s/KTPhaeqxbMK9CxHUUgFDmg)
|
||||
|
||||
* 链表经典题目
|
||||
* [0203.移除链表元素](https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA)
|
||||
@ -94,16 +96,16 @@
|
||||
|
||||
* 循环不变量原则
|
||||
* [0035.搜索插入位置](https://mp.weixin.qq.com/s/fCf5QbPDtE6SSlZ1yh_q8Q)
|
||||
* [0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md)
|
||||
* [0059.螺旋矩阵II](https://mp.weixin.qq.com/s/KTPhaeqxbMK9CxHUUgFDmg)
|
||||
|
||||
* 字符串经典题目
|
||||
* [0344.反转字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0344.反转字符串.md)
|
||||
* [0541.反转字符串II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0541.反转字符串II.md)
|
||||
* [剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md)
|
||||
* [0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md)
|
||||
* 延伸左旋转字符串(剑指offer上的题目)
|
||||
* [0028.实现strStr()](https://github.com/youngyangyang04/leetcode/blob/master/problems/0028.实现strStr().md)
|
||||
* [0459.重复的子字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0459.重复的子字符串.md)
|
||||
* [0344.反转字符串](https://mp.weixin.qq.com/s/X02S61WCYiCEhaik6VUpFA)
|
||||
* [0541.反转字符串II](https://mp.weixin.qq.com/s/XGSk1GyPWhfqj2g7Cb1Vgw)
|
||||
* [剑指Offer05.替换空格](https://mp.weixin.qq.com/s/t0A9C44zgM-RysAQV3GZpg)
|
||||
* [0151.翻转字符串里的单词](https://mp.weixin.qq.com/s/X3qpi2v5RSp08mO-W5Vicw)
|
||||
* [剑指Offer58-II.左旋转字符串](https://mp.weixin.qq.com/s/PmcdiWSmmccHAONzU0ScgQ)
|
||||
* [0028.实现strStr()](https://mp.weixin.qq.com/s/Gk9FKZ9_FSWLEkdGrkecyg)
|
||||
* [0459.重复的子字符串](https://mp.weixin.qq.com/s/lR2JPtsQSR2I_9yHbBmBuQ)
|
||||
|
||||
* 双指针法经典题目
|
||||
* [0027.移除元素](https://mp.weixin.qq.com/s/wj0T-Xs88_FHJFwayElQlA)
|
||||
@ -113,7 +115,7 @@
|
||||
* [0206.翻转链表](https://mp.weixin.qq.com/s/pnvVP-0ZM7epB8y3w_Njwg)
|
||||
* [0142.环形链表II](https://mp.weixin.qq.com/s/_QVP3IkRZWx9zIpQRgajzA)
|
||||
* [0344.反转字符串](https://mp.weixin.qq.com/s/X02S61WCYiCEhaik6VUpFA)
|
||||
* [剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md)
|
||||
* [剑指Offer05.替换空格](https://mp.weixin.qq.com/s/t0A9C44zgM-RysAQV3GZpg)
|
||||
|
||||
* 栈与队列经典题目
|
||||
* [0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md)
|
||||
@ -420,6 +422,7 @@ backtracking() {
|
||||
|[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) |树 |简单|**递归** **迭代/队列/栈**|
|
||||
|[0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md) |树 |中等|**广度优先搜索/队列**|
|
||||
|[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) |树 |简单|**递归** **迭代/队列/BFS**|
|
||||
|[0107.二叉树的层次遍历II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0107.二叉树的层次遍历II.md) |树 |简单|**广度优先搜索/队列/BFS**|
|
||||
|[0110.平衡二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0110.平衡二叉树.md) |树 |简单|**递归**|
|
||||
|[0111.二叉树的最小深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0111.二叉树的最小深度.md) |树 |简单|**递归** **队列/BFS**|
|
||||
|[0131.分割回文串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0131.分割回文串.md) |回溯 |中等|**回溯**|
|
||||
|
@ -22,7 +22,13 @@ https://leetcode-cn.com/problems/two-sum/
|
||||
|
||||
很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。
|
||||
|
||||
使用哈希法最为合适,之前已经介绍过,数组和set在哈希法中的应用,那么来看一下使用数组和set来做哈希法的局限。
|
||||
建议大家做这道题目之前,先做一下这两道
|
||||
* [242. 有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig)
|
||||
* [349. 两个数组的交集](https://mp.weixin.qq.com/s/N9iqAchXreSVW7zXUS4BVA)
|
||||
|
||||
[242. 有效的字母异位词](https://mp.weixin.qq.com/s/vM6OszkM6L1Mx2Ralm9Dig) 这道题目是用数组作为哈希表来解决哈希问题,[349. 两个数组的交集](https://mp.weixin.qq.com/s/N9iqAchXreSVW7zXUS4BVA)这道题目是通过set作为哈希表来解决哈希问题。
|
||||
|
||||
本题呢,则要使用map,那么来看一下使用数组和set来做哈希法的局限。
|
||||
|
||||
* 数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。
|
||||
* set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判断y是否存在而且还要记录y的下表位置,因为要返回x 和 y的下表。所以set 也不能用。
|
||||
@ -39,10 +45,14 @@ C++中map,有三种类型:
|
||||
|
||||
std::unordered_map 底层实现为哈希表,std::map 和std::multimap 的底层实现是红黑树。
|
||||
|
||||
同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。 更多哈希表的理论知识请看[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)。
|
||||
同理,std::map 和std::multimap 的key也是有序的(这个问题也经常作为面试题,考察对语言容器底层的理解)。 更多哈希表的理论知识请看[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)。
|
||||
|
||||
**这道题目中并不需要key有序,选择std::unordered_map 效率更高!**
|
||||
|
||||
解题思路动画如下:
|
||||
|
||||
<video src='../video/1.两数之和.mp4' controls='controls' width='640' height='320' autoplay='autoplay'> Your browser does not support the video tag.</video></div>
|
||||
|
||||
# C++代码
|
||||
|
||||
```
|
||||
|
@ -1,22 +1,53 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/implement-queue-using-stacks/
|
||||
|
||||
## 思路
|
||||
> 工作上一定没人这么搞,但是考察对栈、队列理解程度的好题
|
||||
|
||||
使用**两个栈 一个输入栈,一个输出栈**,这里要注意输入栈和输出栈的关系。
|
||||
# 232. 用栈实现队列
|
||||
|
||||
下面动画模拟一下队列的执行过程如下:
|
||||
使用栈实现队列的下列操作:
|
||||
|
||||
执行语句:
|
||||
push(x) -- 将一个元素放入队列的尾部。
|
||||
pop() -- 从队列首部移除元素。
|
||||
peek() -- 返回队列首部的元素。
|
||||
empty() -- 返回队列是否为空。
|
||||
|
||||
|
||||
示例:
|
||||
|
||||
```
|
||||
MyQueue queue = new MyQueue();
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
queue.pop(); **注意此时的输出栈的操作**
|
||||
queue.push(3);
|
||||
queue.push(4);
|
||||
queue.pop();
|
||||
queue.pop();**注意此时的输出栈的操作**
|
||||
queue.pop();
|
||||
queue.empty();
|
||||
queue.peek(); // 返回 1
|
||||
queue.pop(); // 返回 1
|
||||
queue.empty(); // 返回 false
|
||||
```
|
||||
|
||||
说明:
|
||||
|
||||
* 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
|
||||
* 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
|
||||
* 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
|
||||
|
||||
# 思路
|
||||
|
||||
这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。
|
||||
|
||||
使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈**一个输入栈,一个输出栈**,这里要注意输入栈和输出栈的关系。
|
||||
|
||||
下面动画模拟以下队列的执行过程如下:
|
||||
|
||||
执行语句:
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
queue.pop(); **注意此时的输出栈的操作**
|
||||
queue.push(3);
|
||||
queue.push(4);
|
||||
queue.pop();
|
||||
queue.pop();**注意此时的输出栈的操作**
|
||||
queue.pop();
|
||||
queue.empty();
|
||||
|
||||
<video src='../video/232.用栈实现队列版本2.mp4' controls='controls' width='640' height='320' autoplay='autoplay'> Your browser does not support the video tag.</video></div>
|
||||
|
||||
@ -24,9 +55,11 @@ queue.empty();
|
||||
|
||||
最后如何判断队列为空呢?**如果进栈和出栈都为空的话,说明模拟的队列为空了。**
|
||||
|
||||
在代码实现的时候,会发现pop() 和 peek()两个函数功能类似,代码实现上也是类似的,可以思考一下如何把代码抽象一下。
|
||||
|
||||
代码如下:
|
||||
|
||||
## C++代码
|
||||
# C++代码
|
||||
|
||||
```
|
||||
class MyQueue {
|
||||
@ -44,7 +77,7 @@ public:
|
||||
|
||||
/** Removes the element from in front of queue and returns that element. */
|
||||
int pop() {
|
||||
//只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
|
||||
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
|
||||
if (stOut.empty()) {
|
||||
// 从stIn导入数据直到stIn为空
|
||||
while(!stIn.empty()) {
|
||||
@ -71,5 +104,19 @@ public:
|
||||
};
|
||||
|
||||
```
|
||||
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
||||
# 拓展
|
||||
|
||||
可以看出peek()的实现,直接复用了pop()。
|
||||
|
||||
再多说一些代码开发上的习惯问题,在工业级别代码开发中,最忌讳的就是 实现一个类似的函数,直接把代码粘过来改一改就完事了。
|
||||
|
||||
这样的项目代码会越来越乱,**一定要懂得复用,功能相近的函数要抽象出来,不要大量的复制粘贴,很容易出问题!(踩过坑的人自然懂)**
|
||||
|
||||
工作中如果发现某一个功能自己要经常用,同事们可能也会用到,自己就花点时间把这个功能抽象成一个好用的函数或者工具类,不仅自己方便,也方面了同事们。
|
||||
|
||||
同事们就会逐渐认可你的工作态度和工作能力,自己的口碑都是这么一点一点积累起来的!在同事圈里口碑起来了之后,你就发现自己走上了一个正循环,以后的升职加薪才少不了你!哈哈哈
|
||||
|
||||
|
||||
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||
|
||||
|
@ -26,6 +26,8 @@ https://leetcode-cn.com/problems/repeated-substring-pattern/
|
||||
|
||||
这又是一道标准的KMP的题目。
|
||||
|
||||
如果KMP还不够了解,可以看我的这个视频[帮你把KMP算法学个通透!B站](https://www.bilibili.com/video/BV1PD4y1o7nd/)
|
||||
|
||||
我们在[字符串:都来看看KMP的看家本领!](https://mp.weixin.qq.com/s/Gk9FKZ9_FSWLEkdGrkecyg)里提到了,在一个串中查找是否出现过另一个串,这是KMP的看家本领。
|
||||
|
||||
那么寻找重复子串怎么也涉及到KMP算法了呢?
|
||||
|
50
problems/0637.二叉树的层平均值.md
Normal file
50
problems/0637.二叉树的层平均值.md
Normal file
@ -0,0 +1,50 @@
|
||||
## 题目地址
|
||||
|
||||
## 思路
|
||||
|
||||
这道题目就是考察二叉树的层序遍历。
|
||||
|
||||
二叉树的层序遍历还有这两道题目,大家可以先做一下,其实都是一个思路
|
||||
|
||||
* [0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md) 单纯的层寻遍历
|
||||
* |[0107.二叉树的层次遍历II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0107.二叉树的层次遍历II.md) 相对于[0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md)就是把每一层倒叙输出就可以了
|
||||
|
||||
想要学习二叉树的深度遍历可以看这里[彻底吃透前中后序递归法](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/dai-ma-sui-xiang-lu-chi-tou-qian-zhong-hou-xu-de-d/)。
|
||||
|
||||
而本题呢,就是把每一层的结果求一个和,然后取平均值。
|
||||
|
||||
层序遍历一个二叉树,需要借用一个辅助数据结构队列来实现,**队列先进先出,符合一层一层遍历的逻辑,而是用栈先进后出适合模拟深度优先遍历也就是递归的逻辑。**
|
||||
|
||||
使用队列实现广度优先遍历,动画如下:
|
||||
|
||||
<video src='../video/102二叉树的层序遍历.mp4' controls='controls' width='640' height='320' autoplay='autoplay'> Your browser does not support the video tag.</video></div>
|
||||
|
||||
这样就实现了层序从左到右遍历二叉树。
|
||||
|
||||
代码如下:(这份代码也可以作为二叉树层序遍历的模板)。
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
vector<double> averageOfLevels(TreeNode* root) {
|
||||
queue<TreeNode*> que;
|
||||
if (root != NULL) que.push(root);
|
||||
vector<double> result;
|
||||
while (!que.empty()) {
|
||||
int size = que.size();
|
||||
double sum = 0; // 统计每一层的和
|
||||
for (int i = 0; i < size; i++) { // 这里一定要使用固定大小size,不要使用que.size()
|
||||
TreeNode* node = que.front();
|
||||
que.pop();
|
||||
sum += node->val;
|
||||
if (node->left) que.push(node->left);
|
||||
if (node->right) que.push(node->right);
|
||||
}
|
||||
result.push_back(sum / size); // 将每一层放进结果集
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
BIN
video/1.两数之和.mp4
Normal file
BIN
video/1.两数之和.mp4
Normal file
Binary file not shown.
Reference in New Issue
Block a user