两个栈,倒买倒卖
public class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
int result = 0;
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
result = stack2.pop();
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return result;
}
public int peek() {
int result = 0;
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
result = stack2.peek();
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
return result;
}
public boolean empty() {
return stack1.isEmpty();
}
}