package 剑指offer.栈;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;
public class leetcode232用栈实现队列 {
class MyQueue {
Stack<Integer> inStack;
Stack<Integer> outStack;
public MyQueue() {
inStack = new Stack<>();
outStack = new Stack<>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.isEmpty()) {
inandout();
}
return outStack.pop();
}
public int peek() {
if (outStack.isEmpty()) {
inandout();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void inandout() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
}
|