楼主:
Rushia (みけねこ的鼻屎)
2022-12-16 09:30:23232. 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: