This commit is contained in:
youngyangyang04
2020-07-23 19:24:24 +08:00
parent 9dcd5e3d2e
commit 6e5620132e
3 changed files with 34 additions and 37 deletions

View File

@ -1,14 +1,13 @@
# Leetcode精选: # 算法文章精选:
* [leetcode面试高频题目题解](https://github.com/youngyangyang04/leetcode-master) * [究竟什么是时间复杂度,怎么求时间复杂度,看这一篇就够了](https://mp.weixin.qq.com/s/lYL9TSxLqCeFXIdjt4dcIw)
* [究竟什么是时间复杂度,怎么求时间复杂度,看这一篇就够了](https://mp.weixin.qq.com/s/ma615my-adxMNKmzcdYVlg) * [一文带你彻底理解程序为什么会超时](https://mp.weixin.qq.com/s/T-vcJSkq2-0s0bBB-itWbQ)
* [文带你彻底理解程序为什么会超时](https://mp.weixin.qq.com/s/ZLHsvkxppD8QJMBj3njOiw) * [场面试,带你彻底掌握递归算法的时间复杂度](https://mp.weixin.qq.com/s/Kt-Mvs8LeVqidLGUqySj1g)
* [一场面试,带你彻底掌握递归算法的时间复杂度](https://mp.weixin.qq.com/s/GOYVzUVrfLUjOrDg21YOAw) * [算法分析中的空间复杂度,你真的会了么?](https://mp.weixin.qq.com/s/sXjjnOUEQ4Gf5F9QCRzy7g)
* [算法分析中的空间复杂度,你真的会了么?](https://mp.weixin.qq.com/s/uU6cn0SYRUbIf5tU2QkcOQ) * [二分法其实很简单,为什么老是写不对!!](https://mp.weixin.qq.com/s/ICdahBRWbbEeEWHKjC8Alw)
* [二分法其实很简单,为什么老是写不对!!](https://mp.weixin.qq.com/s/mZ87E2vdvqhbv55uYpdrAQ) * [程序员算法面试中,必须掌握的数组理论知识](https://mp.weixin.qq.com/s/X7R55wSENyY62le0Fiawsg)
* [程序员算法面试中,必须掌握的数组理论知识](https://mp.weixin.qq.com/s/hgSxlgs12IQPUvG0BQukBg) * [这五道数组相关的面试题,你一定要会!](https://mp.weixin.qq.com/s/vdKHt2vFSZEouZASjdWieg)
* [这五道数组相关的面试题,你一定要会](https://mp.weixin.qq.com/s/tMgABUtR1Ydv330NogS3Jg) * [关于哈希表,你该了解这些](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)
* [关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/UnuQIRZSn_i2G4VhTWK5CQ)
* 精选链表相关的面试题 * 精选链表相关的面试题
* 精选哈希表相关的面试题 * 精选哈希表相关的面试题
* 精选字符串相关的面试题 * 精选字符串相关的面试题
@ -16,15 +15,17 @@
* 精选二叉树相关的面试题 * 精选二叉树相关的面试题
* 精选递归与回溯面试题 * 精选递归与回溯面试题
(持续更新中....
# LeetCode 最强题解: # LeetCode 最强题解:
|题目 | 类型 | 难度 | 解题方法 | |题目 | 类型 | 难度 | 解题方法 |
|---|---| ---| --- | |---|---| ---| --- |
|[0000.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0000.两数之和.md) | 数组|简单|**暴力** **哈希**| |[0001.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0001.两数之和.md) | 数组|简单|**暴力** **哈希**|
|[0015.三数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0015.三数之和.md) | 数组 |中等|**双指针** **哈希**| |[0015.三数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0015.三数之和.md) | 数组 |中等|**双指针** **哈希**|
|[0018.四数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0018.四数之和) | 数组 |中等|**双指针**| |[0018.四数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0018.四数之和.md) | 数组 |中等|**双指针**|
|[0020.有效的括号](https://github.com/youngyangyang04/leetcode/blob/master/problems/0020.有效的括号) | 栈 |简单|**栈**| |[0020.有效的括号](https://github.com/youngyangyang04/leetcode/blob/master/problems/0020.有效的括号.md) | 栈 |简单|**栈**|
|[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |简单|**模拟** | |[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |简单|**模拟** |
|[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |简单|**暴力** **快慢指针** | |[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |简单|**暴力** **快慢指针** |
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **快慢指针/双指针**| |[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **快慢指针/双指针**|

View File

@ -4,6 +4,8 @@ https://leetcode-cn.com/problems/implement-stack-using-queues/
## 思路 ## 思路
有的同学可能疑惑这种题目有什么实际工程意义,其实很多算法题目主要是对知识点的考察和教学意义远大于其工程实践的意义,所以面试题也是这样!
用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质 用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质
建议大家题目编号: 225 和 232 一起做来做 建议大家题目编号: 225 和 232 一起做来做
@ -15,8 +17,8 @@ https://leetcode-cn.com/problems/implement-stack-using-queues/
``` ```
class MyStack { class MyStack {
public: public:
queue<int> queIn; queue<int> que1;
queue<int> queOut; queue<int> que2; // 辅助队列
/** Initialize your data structure here. */ /** Initialize your data structure here. */
MyStack() { MyStack() {
@ -24,35 +26,35 @@ public:
/** Push element x onto stack. */ /** Push element x onto stack. */
void push(int x) { void push(int x) {
queIn.push(x); que1.push(x);
} }
/** Removes the element on top of the stack and returns that element. */ /** Removes the element on top of the stack and returns that element. */
int pop() { int pop() {
int size = queIn.size(); int size = que1.size();
size--; size--;
while (size--) { // 将queIn 导入queOut,但要留下最后一个元素 while (size--) { // 将que1 导入que2,但要留下最后一个元素
queOut.push(queIn.front()); que2.push(que1.front());
queIn.pop(); que1.pop();
} }
int result = queIn.front(); // 留下的最后一个元素就是我们要返回的值 int result = que1.front(); // 留下的最后一个元素就是我们要返回的值
queIn.pop(); que1.pop();
queIn = queOut; // 再将queOut赋值给queIn que1 = que2; // 再将que2赋值给que1
while(!queOut.empty()) { // 清空queOut while(!que2.empty()) { // 清空que2
queOut.pop(); que2.pop();
} }
return result; return result;
} }
/** Get the top element. */ /** Get the top element. */
int top() { int top() {
return queIn.back(); return que1.back();
} }
/** Returns whether the stack is empty. */ /** Returns whether the stack is empty. */
bool empty() { bool empty() {
return queIn.empty(); return que1.empty();
} }
}; };
``` ```

View File

@ -23,9 +23,11 @@ public:
/** Removes the element from in front of queue and returns that element. */ /** Removes the element from in front of queue and returns that element. */
int pop() { int pop() {
if (stOut.empty()) { //只有当stOut为空的时候再从stIn里导入数据 //只有当stOut为空的时候再从stIn里导入数据导入stIn全部数据
if (stOut.empty()) {
// 从stIn导入数据直到stIn为空
while(!stIn.empty()) { while(!stIn.empty()) {
stOut.push(stIn.top()); // 从stIn导入数据知道stOut为空 stOut.push(stIn.top());
stIn.pop(); stIn.pop();
} }
} }
@ -47,14 +49,6 @@ public:
} }
}; };
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
``` ```
> 更过算法干货文章持续更新可以微信搜索「代码随想录」第一时间围观关注后回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。 > 更过算法干货文章持续更新可以微信搜索「代码随想录」第一时间围观关注后回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等就可以获得我多年整理的学习资料。