请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
输入: [“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”] [[], [1], [2], [], [], []] 输出: [null, null, null, 1, 1, false]
解释: 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 <= x <= 9 最多调用 100 次 push、pop、peek 和 empty 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
队列是—种先进先出(first in - first out,FIFO)的数据结构,队列中的元素都从后端(rear)入队(push),从前端(front)出队(pop)。 实现队列最直观的方法是用链表,但在这篇文章里我会介绍另—个方法-使用栈。 栈是一种后进先出(last in - first out,LIFO)的数据结构,栈中元素从栈顶(top)压入(push),也从栈顶弹出(pop)。为了满足队列的FIFO的特性,我们需要用到两个栈,用它们其中一个来反转元素的入队顺序,用另一个来存储元素的最终顺序。
入队(push) 一个队列是FIFO的,但一个栈是 LIFO的。这就意味着最新压入的元素必须得放在栈底。为了实现这个目的,我们首先需要把s1中所有的元素移到s2中,接着把新元素压入s2。最后把s2中所有的元素弹出,再把弹出的元素压入s1。 出队(pop) 直接从s1弹出就可以了,因为s1的栈顶元素就是队列的队首元素。同时我们把弹出之后s1的栈顶元素赋值给代表队首元素的front变量。 判断空(empty) s1存储了队列所有的元素,所以只需要检查s1 的是否为空就可以了。 取队首元素(peek) 在我们的算法中,用了front变量来存储队首元素,在每次入队操作或者出队操作之后这个变量都会随之更新。
入队
private int front;
public void push(int x) {
if (s1.empty())
front = x;
while (!s1.isEmpty())
s2.push(s1.pop());
s2.push(x);
while (!s2.isEmpty())
s1.push(s2.pop());
}
出队
// Removes the element from the front of queue.
public void pop() {
s1.pop();
if (!s1.empty())
front = s1.peek();
}
判空
// Return whether the queue is empty.
public boolean empty() {
return s1.isEmpty();
}
取队首元素
// Get the front element.
public int peek() {
return front;
}