【小白爬Leetcode232】2.2 用栈实现队列 Implement Queue using Stacks
Leetcode 232 e s a y \color{#228B22}{esay} esay
点击进入原题链接:Leetcode225 用队列实现栈 Implement Stack using Queues
题目
Description
Implement the following operations of a queue using stacks.
· push(x) – Push element x to the back of queue.
· pop() – Removes the element from in front of queue.
· peek() – Get the front element.
· empty() – Return whether the queue is empty.
Example:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
Notes:
- You must use only standard operations of a stack – which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a
list
ordeque
(double-ended queue), as long as you use only standard operations of a stack. - You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
中文描述
使用栈实现队列的下列操作:
· push(x) – 将一个元素放入队列的尾部。
· pop() – 从队列首部移除元素。
· peek() – 返回队列首部的元素。
· empty() – 返回队列是否为空。
说明:
你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
思路一 用两个栈改变push的规则 【入队 - O(n), 出队 - O(1)】
核心思想在于让每次push元素的时候,都能push到栈底,这样栈就成了队列。
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
std::stack<int> temp;
while(!_data.empty()){
temp.push(_data.top());
_data.pop();
}
temp.push(x);
while(!temp.empty()){
_data.push(temp.top());
temp.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int res = _data.top();
_data.pop();
return res;
}
/** Get the front element. */
int peek() {
return _data.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return _data.empty();
}
private:
std::stack<int> _data;
};
思路二 用两个栈动态储存信息 入队 - O(1),出队 - 摊还复杂度 【O(1)】
思路一的缺点在于每次push的时间复杂度都是n,而恰恰push是所有操作里最频繁的,因此效率较为低下。
而思路二的优势在于平均下来入队和出队的时间复杂度都为常数。它的实现思路是:
用两个stack s1
和s2
- 一开始队列为空,记录压入s1的第一个元素
front
,并把后续需要压入队列的元素压入s1
; - 当需要弹出元素的时候,
s1
不能实现弹出front
的操作, 于是将s1
的元素一个一个压入s2
中,那么s2
其实就是s1
的反转,此时弹出s2的栈顶元素即完成了队列的pop操作; - 像这样,新
push
的元素都进入s1
,pop
的元素都从s2
出,那么总有一天s2
会耗尽。当s2
耗尽,只需要重新将s1
的元素弹入s2
即可; - 实现的队列的
peek
有两个可能:当s2
不为空时,就是s2
的栈顶元素;当s2
为空时,则是记录的front
变量。
复杂度分析:
时间复杂度: 入队O(1),只需要push
进s1
即可; 出队:假设pop
前s1
有n个元素,当s2
为空的时候,需要将s1
的元素弹出,再压入s2
,复杂度为O(2n);当s2
不为空的时候,只需要弹出s2
的栈顶元素,复杂度为O(1),直到把s2
pop完,复杂度为O(n-1)。总共进行了n次弹出操作,平均下来时间复杂度是O(3n-1/n)=O(1)。
空间复杂度: O(n),因为用了两个stack,这已经是最好的复杂度了。
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
if(s1.empty()) front = x;
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int res;
if(s2.empty()){
while(!s1.empty()){
res = s1.top();
s2.push(res);
s1.pop();
}
}
res = s2.top();
s2.pop();
return res;
}
/** Get the front element. */
int peek() {
if(!s2.empty()) return s2.top();
else return front;
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.empty() && s2.empty();
}
private:
std::stack<int> s1;
std::stack<int> s2;
int front;
};
结果如下: