目录
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
创建两个栈,每一次放新元素进队列时,都先让旧元素依次出栈到辅助栈中去,
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
1. 用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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
操作)
方法一:使用辅助栈
创建两个栈,每一次放新元素进队列时,都先让旧元素依次出栈到辅助栈中去,
再放入新元素在真正的栈底,再将旧元素依次入真正使用的栈
class MyQueue {
//创建两个栈,
//每一次放新元素,都先让旧元素依次出栈,再放入新元素在栈底,再将旧元素依次入栈
//存放真正元素的栈
Stack<Integer> stack1 = new Stack<>();
//辅助栈
Stack<Integer> stack2 = new Stack<>();
public MyQueue() {
}
public void push(int x) {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
stack1.push(x);
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
}
public int pop() {
return stack1.pop();
}
public int peek() {
return stack1.peek();
}
public boolean empty() {
return stack1.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
2.用队列实现栈
请你仅使用两个队列实现一个后入先出(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
都保证栈不为空
方法一:使用辅助队列
创建两个队列,q1和q2,q1是用来存放真正数据的队列,q2是辅助队列,每一次有新元素入栈时,都让这个新元素入q2这个空队列,然后让q1中的元素依次出队列,入到q2这个队列中去,此时q2是有效队列,q1是空队列,因为q1,q2只是一个引用,所以最后将q1,q2的地址调换即可
class MyStack {
//q1用来存放真正的元素
//q2用来存放新元素
Queue <Integer>q1 = new LinkedList<>();
Queue <Integer>q2 = new LinkedList<>();
public void push(int x) {
q2.offer(x);
while(!q1.isEmpty()){
q2.offer(q1.poll());
}
Queue <Integer> temp= q1;
q1 = q2;
q2 = temp;
}
public int pop() {
return q1.poll();
}
public int top() {
return q1.peek();
}
public boolean empty() {
return q1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
方法二:只使用一个队列
每一次放入新元素,让新元素以前的元素都出队列再入队列即可
class MyStack {
//每一次放入新元素,让新元素以前的元素都出队列再入队列
Queue <Integer>q1 = new LinkedList<>();
public void push(int x) {
q1.offer(x);
for(int i = 0; i < q1.size() - 1; i++){
int temp = q1.poll();
q1.offer(temp);
}
}
public int pop() {
return q1.poll();
}
public int top() {
return q1.peek();
}
public boolean empty() {
return q1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/