Implement Queue by Stacks

As the title described, you should only use two stacks to implement a queue's actions.

The queue should supportpush(element),pop()andtop()where pop is pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

Example

push(1)
pop()     // return 1
push(2)
push(3)
top()     // return 2
pop()     // return 2

Note

全O(1): pop Amortized O(1)

s1收纳所有元素

Code

Last updated