Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push
, peek
, pop
, and empty
).
Implement the MyQueue
class:
void push(int x)
Pushes element x to the back of the queue.int pop()
Removes the element from the front of the queue and returns it.int peek()
Returns the element at the front of the queue.boolean empty()
Returnstrue
if the queue is empty,false
otherwise.
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, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.
Follow-up: Can you implement the queue such that each operation is amortized O(1)
time complexity? In other words, performing n
operations will take overall O(n)
time even if one of those operations may take longer.
Example 1:
1 | Input |
Constraints:
1 <= x <= 9
- At most
100
calls will be made topush
,pop
,peek
, andempty
. - All the calls to
pop
andpeek
are valid.
1 栈模拟1
设立两个栈stk1和stk2, stk1储存倒置顺序的元素, stk2为空.
push的时候先把stk1所有元素转移到stk2, 然后把x放到stk1栈底, 然后再把stk2剩下的元素返回到stk1, 这样, stk1底部多了元素x, 并且其他元素位置不变.
初始状态 清空stk1 push(5) 剩下的元素返回stk1
stk1 : 4,3,2,1 null 5 5,4,3,2,1
stk2 : null 1,2,3,4 1,2,3,4 null
但是这种push操作需要O(n)复杂度.
1 | class MyQueue { |
2 栈模拟2
上面的方法每push一次都要有O(n)的计算量.
可以改进一下, 设置一个pushstack和popstack.
每次push都直接push到pushstack中. pop或top的时候再把pushtack中的元素全都转移过来. 由于栈的特性, 转移到popstack后自然是队列的顺序了.
这样, 所有操作的复杂度都可以降为O(1)
1 | class MyQueue { |