232. 用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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:
输入:
["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
操作)
思路
首先必须了解
栈的特点是:先进后出
队列的特点是:先进先出
如图,把栈当作队列来看,那么栈底元素是队首,栈顶元素是队尾。
队列的入队
:
此时就是入栈
队列的出队
:
但是队列的出队
就稍嫌麻烦了:
这个出队显然无法一步直达,需要分步来:
首先0
需要出栈,必须其他元素都先出栈:
然后其他元素依次入栈:
那怎么保存入栈的数呢?
可以借助另一个栈!
返回队列第一个元素
复用出队方法就行
队列判空
与栈的判空一样
Java代码
class MyQueue {
public Stack stack;
public MyQueue() {
stack = new Stack<Integer>();
}
//进队
public void push(int x) {
stack.push(x);
}
//出队
public int pop() {
if(stack.size() == 0) {
return -1;
}
int size = stack.size();
Stack tempStack = new Stack<Integer>();
for(int i = 0; i < size - 1; i++) {
tempStack.push(stack.pop());
}
Integer top = (Integer)stack.pop();
for(int i = 0; i < size - 1; i++) {
stack.push(tempStack.pop());
}
return top;
}
//返回队首元素
public int peek() {
if(empty()) {
return -1;
}
int size = stack.size();
Stack tempStack = new Stack<Integer>();
for(int i = 0; i < size - 1; i++) {
tempStack.push(stack.pop());
}
Integer top = (Integer)stack.pop();
stack.push(top);
for(int i = 0; i < size - 1; i++) {
stack.push(tempStack.pop());
}
return top;
}
//判断是否为空队列
public boolean empty() {
return stack.isEmpty();
}
}
225. 用队列实现栈
请你仅使用两个队列实现一个后入先出(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(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、top
和empty
- 每次调用
pop
和top
都保证栈不为空
进阶: 你能否仅用一个队列来实现栈。
思路
本题可以像上一题那样,用两个队列来实现栈
但是因为队列的特点,其实可以只用一个队列来实现栈
入栈
和队列入队一样
出栈
上图中,左边是出栈前,右边是出栈后
对队列来说显然也不能一步到位
需要经历如下步骤【无需借助另一个队列】
返回栈顶元素
可以复用出栈方法
栈的判空
和队列的判空一样
class MyStack {
private Queue queue;
public MyStack() {
queue = new LinkedList<Integer>();
}
//入栈
public void push(int x) {
queue.add(x);
}
//出栈
public int pop() {
int size = queue.size();
for(int i = 0; i < size - 1; i++) {
queue.add(queue.poll());
}
Integer poll = (Integer) queue.poll();
return poll;
}
//返回栈顶元素
public int top() {
int pop = pop();
push(pop);
return pop;
}
//返回是否为空
public boolean empty() {
return queue.isEmpty();
}
}