[设计 栈] 232. 用栈实现队列(双栈模拟队列、缓冲区法)
232. 用栈实现队列
题目链接:https://leetcode-cn.com/problems/implement-queue-using-stacks/
分类:
- 设计:用两个栈模拟队列(常规方法、缓冲区法)
- 栈:利用栈FILO的特点模拟队列的四个基本函数(push,pop,peek,empty)
思路1:改造pop,peek
栈的特点是先进后出,队列的特点是先进先出。我们开辟两个栈,stack1作为主栈,stack2作为反转栈。
接着利用两个栈模拟队列的基本函数:
- push:将元素push到stack1(时间复杂度O(1))
- pop:最早加入的是stack1的栈底元素,所以队列的pop需要弹出该栈底元素,先将stack1的前n-1个元素弹出加入到stack2,此时stack1剩余的最后一个元素就是需要弹出的队首,将栈顶弹出,然后把stack2所有元素再弹出压回stack1。(时间复杂度O(N))
- peek():和pop过程类似,将stack1前n-1个元素都弹出到stack2,此时stack1剩余的最后一个元素就是队首,但stack1不弹出该元素,而是直接把stack2所有元素再压回stack1.(时间复杂度O(N))
- empty():返回stack1.isEmpty()(时间复杂度O(1))
class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
while(stack1.size() > 1){
stack2.push(stack1.pop());
}
int first = stack1.pop();
while(stack2.size() > 0){
stack1.push(stack2.pop());
}
return first;
}
/** Get the front element. */
public int peek() {
while(stack1.size() > 1){
stack2.push(stack1.pop());
}
int first = stack1.peek();
while(stack2.size() > 0){
stack1.push(stack2.pop());
}
return first;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty();
}
}
思路2:改造push
同样利用两个栈来模拟队列:
- push:如果stack2不为空,就将stack2所有元素弹出压入stack1,将新元素压入stack1栈顶,再重新弹出stack1所有元素压回stack2。(时间复杂度O(N))
- pop:弹出stack2的栈顶就是队首,弹出stack2的栈顶即可;(时间复杂度O(1))
- peek:返回stack2的栈顶即可。(时间复杂度O(1))
- empty():返回stack1.isEmpty()(时间复杂度O(1))
class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
while(stack2.size() > 0){
stack1.push(stack2.pop());
}
stack1.push(x);
while(stack1.size() > 0){
stack2.push(stack1.pop());
}
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
return stack2.pop();
}
/** Get the front element. */
public int peek() {
return stack2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack2.isEmpty();
}
}
思路3:缓冲区思想,均摊O(1) (推荐)
前面两个思路,都是一个栈作为主栈,一个作为辅助栈,无论是对push或pop的改造,每执行一次有效操作,都需要将一个栈里所有元素转移到另一个栈。
思路3将这两个思路优化为均摊时间复杂度O(1),把其中一个栈拿来作为输入缓冲区,另一个栈从栈顶到栈底按队列的顺序排列。
算法设计:
- push:stack1作为缓冲区,将输入的新元素都暂存在stack1。设置一个全局变量first,如果新元素加入前stack1为空,则说明该元素会成为队首元素,令first=该元素。如果stack1不为空,则直接加入stack1.
- pop:执行pop时如果stack2为空,则将stack1所有元素弹出压入Stack2,进入“stack2不为空”分支;
如果stack2不为空,则此时stack2的所有元素从栈顶到栈底就按队列的顺序排列,每调用peek,pop都可以直接获取stack2的栈顶。 - peek:如果stack2为空,则返回全局变量first;如果Stack2不为空,则返回stack2的栈顶。
- empty:两个栈都为空才返回true.
为什么均摊时间复杂度=O(1)?
https://leetcode-cn.com/problems/implement-queue-using-stacks/solution/yong-zhan-shi-xian-dui-lie-by-leetcode/ 方法2
class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
int first;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
if(stack1.isEmpty()){
first = x;
}
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stack2.isEmpty()){
while((stack1.size() > 0)){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
/** Get the front element. */
public int peek() {
if(stack2.isEmpty()) return first;
else return stack2.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}