Implement a Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions.
The queue should support push(element), pop() and top() 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
Solution:
使用两个Stack, 左边的就是负责不停的往里面push,右边的就是pop(), 当右面的为空,pop 与top的时候需要进行adjust操作,进行左stack 到右stack.
Last updated
Was this helpful?