目录
1.T232:用栈实现队列
T:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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(双端队列)来模拟一个栈,只要是标准的栈操作即可。
提示:
- 1 <= x <= 9
- 最多调用 100 次 push、pop、peek 和 empty
- 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
进阶:
- 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
S:
使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈:一个输入栈,一个输出栈,这里要注意输入栈和输出栈的关系。
在push数据的时候,只要数据放进输入栈就好;但在pop的时候,操作就复杂一些,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入),再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。
最后如何判断队列为空呢?
如果进栈和出栈都为空的话,说明模拟的队列为空了。
在代码实现的时候,会发现pop() 和 peek()两个函数功能类似,代码实现上也是类似的,可以思考一下如何把代码抽象一下。
代码实现
class MyQueue {
//class Stack<E> extends Vector<E> extends AbstractList<E> implements List<E>
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
dumpStackIn();
return stackOut.pop();
}
public int peek() {
dumpStackIn();
return stackOut.peek();
}
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
private void dumpStackIn() {
// 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
// 就单个栈而言,in:栈顶->out:栈底,但这正好是队列的顺序(先入先出)
if (!stackOut.isEmpty()) return;
while (!stackIn.isEmpty()) {
stackOut.push(stackIn.pop());
}
}
}
2.T225:用队列实现栈
T:请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
-
void push(int x) 将元素 x 压入栈顶。
-
int pop() 移除并返回栈顶元素。
-
int top() 返回栈顶元素。
-
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
- 你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
- 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
提示:
- 1 <= x <= 9
- 最多调用100 次 push、pop、top 和 empty
- 每次调用 pop 和 top 都保证栈不为空
进阶:你能否仅用一个队列来实现栈。
S:首先默认题目中的队列一律指的是单向队列。
依然还是要用2个队列来模拟栈,只不过没有输入和输出的关系,另一个队列完全是用来备份的!
代码实现
法1、用2个队列
- 看了动图后自己对着Queue接口复现(最笨的一种实现)
class MyStack { Queue<Integer> queue1; Queue<Integer> queue2; public MyStack() { //LinkedList<E> implements List<E>, Deque<E> extends Queue<E>, extends Collection<E> queue1 = new LinkedList<>(); queue2 = new LinkedList<>(); } public void push(int x) { queue1.offer(x); } public int pop() { dumpQueue1(); // return queue1.poll(); int res = queue1.remove(); recoverQueue1(); return res; } public int top() { dumpQueue1(); // return queue1.peek(); int res = queue1.element(); queue2.add(queue1.remove()); recoverQueue1(); return res; } public boolean empty() { // return queue1.isEmpty() && queue2.isEmpty(); return queue1.isEmpty(); } private void dumpQueue1() { if (queue1.isEmpty()) return; while (queue1.size() > 1) { queue2.offer(queue1.remove()); } } private void recoverQueue1() { if (queue2.isEmpty()) return; while (queue2.size() > 0) { queue1.offer(queue2.remove()); } } }
每次top、pop中都需要先拿剩一个、最后再复原,很麻烦,扫了两眼Java版的答案后,发现可以在添加上做文章,这样拿的时候就很省事了
- 加的时候麻烦点,拿的时候省事
class MyStack { Queue<Integer> queue1; Queue<Integer> queue2; public MyStack() { //LinkedList<E> implements List<E>, Deque<E> extends Queue<E>, extends Collection<E> queue1 = new LinkedList<>(); queue2 = new LinkedList<>(); } public void push(int x) { dumpQueue1(); queue1.offer(x); recoverQueue1(); } public int pop() { return queue1.remove(); } public int top() { return queue1.element(); } public boolean empty() { // return queue1.isEmpty() && queue2.isEmpty(); return queue1.isEmpty(); } private void dumpQueue1() { if (queue1.isEmpty()) return; // while (queue1.size() > 1) { while (queue1.size() > 0) { queue2.offer(queue1.remove()); } } private void recoverQueue1() { if (queue2.isEmpty()) return; while (queue2.size() > 0) { queue1.offer(queue2.remove()); } } }
实际上,1个队列足矣
法2、用一个队列
一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时再去弹出元素就是栈的顺序了。
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
queue.offer(x);
// while (!queue.isEmpty()) {//死循环
int size = queue.size();
while (size-- > 1) {
queue.offer(queue.remove());
}
}
public int pop() {
return queue.remove();
}
public int top() {
return queue.element();
}
public boolean empty() {
return queue.isEmpty();
}
}
实际上,Java中也可以用双向队列Deque接口中的方法实现,那样代码非常简单(违背了题目中所说只能使用队列基本操作的要求,不过能通过;如果只用基本操作,那跟Queue流程是一样的),这里不再赘述。