232. Implement Queue using Stacks
實作只用Stack來模擬Queue的行為。
Example:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
思路:
1.就...資料結構課本裡面的東西,利用一個stack暫存頂層元素,拿到底層元素
之後再把剩餘元素放回原stack即可。
Java Code: