【LeetCode】232. Implement Queue using Stacks 用栈实现队列(Easy)(JAVA)
题目地址: https://leetcode.com/problems/implement-queue-using-stacks/
题目描述:
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Implement the MyQueue class:
- void push(int x) Pushes element x to the back of the queue.
- int pop() Removes the element from the front of the queue and returns it.
- int peek() Returns the element at the front of the queue.
- boolean empty() Returns true if the queue is empty, false otherwise.
Notes:
- You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
- Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.
Follow-up: Can you implement the queue such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.
Example 1:
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
Constraints:
- 1 <= x <= 9
- At most 100 calls will be made to push, pop, peek, and empty.
- All the calls to pop and peek are valid.
题目大意
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
- void push(int x) 将元素 x 推到队列的末尾
- int pop() 从队列的开头移除并返回元素
- int peek() 返回队列开头的元素
- boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
- 你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
- 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
进阶:
- 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
解题方法
- 要用两个栈实现队列,其实就是需要有一个栈把先前存入的数据翻转过来即可
- 把数据都存入正常的 stack 中,后入先出
- 需要 push 的时候,发现 reverseStack 为空,就把 stack 的数据都存入 reverseStack 中,这样后入先出就被翻转,变成先入先出,然后取出 reverseStack 的最后一个数据即可
- 时间复杂度 push, pop 和 peek 都为 O(1)
class MyQueue {
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> reverseStack = new Stack<>();
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
stack.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (reverseStack.empty()) {
reverse();
}
return reverseStack.pop();
}
/** Get the front element. */
public int peek() {
if (reverseStack.empty()) {
reverse();
}
return reverseStack.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack.empty() && reverseStack.empty();
}
private void reverse() {
while (!stack.empty()) {
reverseStack.push(stack.pop());
}
}
}
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:36.6 MB,击败了8.61% 的Java用户