mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
@ -58,6 +58,7 @@
|
||||
* [字符串:前缀表不右移,难道就写不出KMP了?](https://mp.weixin.qq.com/s/p3hXynQM2RRROK5c6X7xfw)
|
||||
* [字符串:总结篇!](https://mp.weixin.qq.com/s/gtycjyDtblmytvBRFlCZJg)
|
||||
* [栈与队列:来看看栈和队列不为人知的一面](https://mp.weixin.qq.com/s/VZRjOccyE09aE-MgLbCMjQ)
|
||||
* [栈与队列:我用栈来实现队列怎么样?](https://mp.weixin.qq.com/s/P6tupDwRFi6Ay-L7DT4NVg)
|
||||
|
||||
(持续更新中....)
|
||||
|
||||
|
@ -2,40 +2,60 @@
|
||||
|
||||
https://leetcode-cn.com/problems/implement-stack-using-queues/
|
||||
|
||||
## 思路
|
||||
> 用队列实现栈还是有点别扭。
|
||||
|
||||
# 225. 用队列实现栈
|
||||
|
||||
使用队列实现栈的下列操作:
|
||||
|
||||
* push(x) -- 元素 x 入栈
|
||||
* pop() -- 移除栈顶元素
|
||||
* top() -- 获取栈顶元素
|
||||
* empty() -- 返回栈是否为空
|
||||
|
||||
注意:
|
||||
|
||||
* 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
|
||||
* 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
|
||||
* 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
|
||||
|
||||
|
||||
# 思路
|
||||
|
||||
有的同学可能疑惑这种题目有什么实际工程意义,**其实很多算法题目主要是对知识点的考察和教学意义远大于其工程实践的意义,所以面试题也是这样!**
|
||||
|
||||
相信做过[0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md)这道题目的部分同学会依然想着用一个输入队列,一个输出队列,就可以模拟栈的功能,其实不是这样的。
|
||||
刚刚做过[栈与队列:我用栈来实现队列怎么样?](https://mp.weixin.qq.com/s/P6tupDwRFi6Ay-L7DT4NVg)的同学可能依然想着用一个输入队列,一个输出队列,就可以模拟栈的功能,仔细想一下还真不行!
|
||||
|
||||
队列是先进先出的规则,你把一个队列中的数据导入另一个队列中,数据的顺序并没有变,并有变成先进后出的顺序。
|
||||
**队列是先进先出的规则,把一个队列中的数据导入另一个队列中,数据的顺序并没有变,并有变成先进后出的顺序。**
|
||||
|
||||
所以用栈实现队列, 和用队列实现栈的思路还是不一样的,这取决于这两个数据结构的性质。
|
||||
|
||||
但是依然还是要用两个队列来模拟栈,只不过没有输入和输出的关系,而是另一个队列完全用又来备份的!
|
||||
|
||||
如下面动画所示,**用两个队列que1和que2实现队列的功能,que2其实完全就是一个备份的作用**,把que1最后面的元素以外的元素都备份到que2,然后弹出最后面的元素,再把其他元素从que2导回que1。
|
||||
|
||||
模拟的队列执行语句如下:
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
模拟的队列执行语句如下:
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
queue.pop(); // 注意弹出的操作
|
||||
queue.push(3);
|
||||
queue.push(4);
|
||||
queue.pop(); // 注意弹出的操作
|
||||
queue.pop();
|
||||
queue.push(3);
|
||||
queue.push(4);
|
||||
queue.pop();
|
||||
queue.pop();
|
||||
queue.pop();
|
||||
queue.empty();
|
||||
queue.pop();
|
||||
queue.empty();
|
||||
|
||||
<video src='../video/225.用队列实现栈.mp4' controls='controls' width='640' height='320' autoplay='autoplay'> Your browser does not support the video tag.</video></div>
|
||||
|
||||
详细如代码注释所示:
|
||||
|
||||
## C++代码
|
||||
# C++代码
|
||||
|
||||
```
|
||||
class MyStack {
|
||||
public:
|
||||
queue<int> que1;
|
||||
queue<int> que2; // 辅助队列
|
||||
queue<int> que2; // 辅助队列,用来备份
|
||||
/** Initialize your data structure here. */
|
||||
MyStack() {
|
||||
|
||||
@ -55,10 +75,10 @@ public:
|
||||
que1.pop();
|
||||
}
|
||||
|
||||
int result = que1.front(); // 留下的最后一个元素就是我们要返回的值
|
||||
int result = que1.front(); // 留下的最后一个元素就是要返回的值
|
||||
que1.pop();
|
||||
que1 = que2; // 再将que2赋值给que1
|
||||
while(!que2.empty()) { // 清空que2
|
||||
que1 = que2; // 再将que2赋值给que1
|
||||
while (!que2.empty()) { // 清空que2
|
||||
que2.pop();
|
||||
}
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user