232.用栈实现队列
![image-20231104205734915](https://i-blog.csdnimg.cn/blog_migrate/a4295e68df1b8cd12f117a63e1eb919c.png)
class MyQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public MyQueue() {
inStack = new ArrayDeque<Integer>();
outStack = new ArrayDeque<Integer>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if(outStack.isEmpty()){
in2out();
}
return outStack.pop();
}
public int peek() {
if(outStack.isEmpty()){
in2out();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void in2out(){
while(!inStack.isEmpty()){
outStack.push(inStack.pop());
}
}
}