Leetcode 225. 用队列实现栈
题目描述:
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
原题目要求使用两个队列实现一个后入先出的栈。
但本文包括使用双队列实现栈和单队列实现栈。
分别实现了偏写入和偏读取两种方式。
- 两个队列实现,偏写入,即push()操作为O(1)
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue1.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
while (queue1.size() > 1) {
queue2.add(queue1.poll());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
return queue2.poll();
}
/** Get the top element. */
public int top() {
while (queue1.size() > 1) {
queue2.add(queue1.poll());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
int x = queue2.peek();
queue1.add(queue2.poll());
return x;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.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();
*/
- 两个队列,偏读取,即pop(),top()操作为O(1)
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<>();
queue2 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue1.add(x);
while (queue1.size() > 1) {
queue2.add(queue1.poll());
}
while (!queue2.isEmpty()) {
queue1.add(queue2.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue1.poll();
}
/** Get the top element. */
public int top() {
return queue1.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty();
}
}
- 一个队列,偏写入
class MyStack {
Queue<Integer> queue1;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue1.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
int size = queue1.size();
while (size-- > 1) {
queue1.add(queue1.poll());
}
return queue1.poll();
}
/** Get the top element. */
public int top() {
int size = queue1.size();
while (size-- > 1) {
queue1.add(queue1.poll());
}
int x = queue1.peek();
queue1.add(queue1.poll());
return x;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty();
}
}
- 一个队列,偏读取
class MyStack {
Queue<Integer> queue1;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue1.add(x);
int size = queue1.size();
while (size-- > 1) {
queue1.add(queue1.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue1.poll();
}
/** Get the top element. */
public int top() {
return queue1.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.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();
*/

浙公网安备 33010602011771号