题目
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、peek
和empty
- 假设所有操作都是有效的 (例如,一个空的队列不会调用
pop
或者peek
操作)
进阶:
- 你能否实现每个操作均摊时间复杂度为
O(1)
的队列?换句话说,执行n
个操作的总时间复杂度为O(n)
,即使其中一个操作可能花费较长时间。
分析
队列是先进先出
栈是先进后出
那么有两个栈的时候,如果只往一个栈按顺序插入数据,那么数据将是倒序取出的。
相当于使用栈的时候,将能获得倒序的数据,
而使用两个栈的话,相当于倒序两次,就变成了正序,也就能正序像队列一样取出数据。
也就是说可以始终保持数据在第一个栈,只往第一个栈插入数据。
然后如果想取出数据的时候,把第一个栈的数据往第二个栈迁移,这样从第二个栈取出数据的时候,因为入了两次栈,相当于倒序两次,也就变成了正序,先进先出,也就实现了模拟队列功能。
代码
#include <stack>
class MyQueue {
public:
MyQueue()
{
}
void push(int x)
{
while (!m_s2.empty()) {
int p = m_s2.top();
m_s1.push(p);
m_s2.pop();
}
m_s1.push(x);
}
int pop()
{
int p = 0;
while (!m_s1.empty()) {
p = m_s1.top();
m_s2.push(p);
m_s1.pop();
}
p = m_s2.top();
m_s2.pop();
return p;
}
int peek()
{
int p = 0;
while (!m_s1.empty()) {
p = m_s1.top();
m_s2.push(p);
m_s1.pop();
}
p = m_s2.top();
return p;
}
bool empty()
{
return m_s1.empty() && m_s2.empty();
}
private:
std::stack<int> m_s1;
std::stack<int> m_s2;
};
#ifdef __TEST__
#include <iostream>
int main(int argc, char* argv[])
{
int p = 0;
MyQueue* myQueue = new MyQueue();
myQueue->push(1); // queue is: [1]
myQueue->push(2); // queue is: [1, 2] (leftmost is front of the queue)
p = myQueue->peek(); // return 1
std::cout << "peek: " << p << std::endl;
p = myQueue->pop(); // return 1, queue is [2]
std::cout << "pop: " << p << std::endl;
bool b = myQueue->empty(); // return false
std::cout << "empty: " << b << std::endl;
delete myQueue;
return 0;
}
#endif //__TEST__
均摊代码
在代码中,使用两个栈进行了多次倒腾操作,目的是为了保障数据取出的顺序始终是正序的,如果能在操作中始终保持顺序是正序,那么就不需要进行那么多次倒腾。
比如首次往 stack1 通过 push 插入 1 2 3 4 的时候,
+出口/入口
|
v
stack1 4 3 2 1
stack2
这时候想要取出数据,则需要将 stack1 数据转移到 stack2 ,然后再从 stack2 中取出顺序数据。
这时候如果保持 stack2 中的数据顺序不变,那么继续从 stack2 中取出的话,数据始终是正序的。
如果想要继续插入数据的话,只需要继续往 stack1 插入即可,比如继续插入 5 6 7 如下:
+出口/入口
|
v
stack1 7 6 5
stack2 1 2 3 4
为了保障数据始终是正序的,取出数据的时候,需要判断 stack2 是否为空,非空的时候直接取出数据,空的时候再重新从 stack1 中搬数据,这样就能保证数据始终是正序的。
考虑到倒腾的操作不是每次都会执行,而是只在输出栈 stack2 为空的时候才执行,当不需要进行倒腾操作的时候,时间复杂度为,考虑到倒腾操作只占取出操作的一部分,因此相当于 pop 和 peek 的取出操作中的倒腾操作 被均摊到了 (非倒腾取出操作)中。
#include <stack>
class MyQueue {
public:
MyQueue()
{
}
void push(int x)
{
m_s1.push(x);
}
int pop()
{
int p = 0;
if (m_s2.empty()) {
while (!m_s1.empty()) {
p = m_s1.top();
m_s2.push(p);
m_s1.pop();
}
}
p = m_s2.top();
m_s2.pop();
return p;
}
int peek()
{
int p = 0;
if (m_s2.empty()) {
while (!m_s1.empty()) {
p = m_s1.top();
m_s2.push(p);
m_s1.pop();
}
}
p = m_s2.top();
return p;
}
bool empty()
{
return m_s1.empty() && m_s2.empty();
}
private:
std::stack<int> m_s1;
std::stack<int> m_s2;
};
#ifdef __TEST__
#include <iostream>
int main(int argc, char* argv[])
{
int p = 0;
MyQueue* myQueue = new MyQueue();
myQueue->push(1); // queue is: [1]
myQueue->push(2); // queue is: [1, 2] (leftmost is front of the queue)
p = myQueue->peek(); // return 1
std::cout << "peek: " << p << std::endl;
p = myQueue->pop(); // return 1, queue is [2]
std::cout << "pop: " << p << std::endl;
bool b = myQueue->empty(); // return false
std::cout << "empty: " << b << std::endl;
delete myQueue;
return 0;
}
#endif //__TEST__