225. Implement Stack using Queues(E)
https://leetcode.com/problems/implement-stack-using-queues/
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push
, top
, pop
, and empty
).
Implement the MyStack
class:
void push(int x)
Pushes element x to the top of the stack.int pop()
Removes the element on the top of the stack and returns it.int top()
Returns the element on the top of the stack.boolean empty()
Returnstrue
if the stack is empty,false
otherwise.
Notes:
You must use only standard operations of a queue, which means that only
push to back
,peek/pop from front
,size
andis empty
operations are valid.Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
Example 1:
Constraints:
1 <= x <= 9
At most
100
calls will be made topush
,pop
,top
, andempty
.All the calls to
pop
andtop
are valid.
Follow-up: Can you implement the stack using only one queue?
2.Code
如果说双栈实现队列比较巧妙,那么用队列实现栈就比较简单粗暴了,只需要一个队列作为底层数据结构。首先看下栈的 API:
先说 push
API,直接将元素加入队列,同时记录队尾元素,因为队尾元素相当于栈顶元素,如果要 top
查看栈顶元素的话可以直接返回:
我们的底层数据结构是先进先出的队列,每次 pop
只能从队头取元素;但是栈是后进先出,也就是说 pop
API 要从队尾取元素:
解决方法简单粗暴,把队列前面的都取出来再加入队尾,让之前的队尾元素排到队头,这样就可以取出了:
这样实现还有一点小问题就是,原来的队尾元素被提到队头并删除了,但是 top_elem
变量没有更新,我们还需要一点小修改:
最后,API empty
就很容易实现了,只要看底层的队列是否为空即可:
很明显,用队列实现栈的话,pop
操作时间复杂度是 O(N),其他操作都是 O(1)。
个人认为,用队列实现栈是没啥亮点的问题,但是用双栈实现队列是值得学习的。
从栈 s1
搬运元素到 s2
之后,元素在 s2
中就变成了队列的先进先出顺序,这个特性有点类似「负负得正」,确实不太容易想到。
Last updated