Queue with two stacks. Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.
思路:建立两个Stack,分别是stack1, stack2。
对于队列的enqueue操作,直接把元素push到stack1。
对于dequeue操作:
1.如果stack2为空,则将stack1中的所有元素pop出来并push进stack2,此时返回stack2的栈顶元素即可。
2.如果进行dequeue操作时stack2不为空,则直接返回stack2的栈顶元素,直到stack2为空,重复1的操作。