classMyQueue{Stack<Integer>StackIn;Stack<Integer>StackOut;publicMyQueue(){//先新建两个栈,入栈和出栈StackIn=newStack<>();StackOut=newStack<>();}publicvoidpush(int x){StackIn.push(x);}publicintpop(){inToOut();returnStackOut.pop();}publicintpeek(){inToOut();returnStackOut.peek();}publicbooleanempty(){returnStackIn.isEmpty()&&StackOut.isEmpty();}privatevoidinToOut(){if(!StackOut.isEmpty()){//只有当出栈为空时才把入栈的所有元素放进去return;}while(!StackIn.isEmpty()){StackOut.push(StackIn.pop());}}}/**
* 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();
*/