非独立思考
class MyQueue {Stack<Integer> inStack;Stack<Integer> outStack;/*** Initialize your data structure here.*/public MyQueue() {inStack = new Stack<>();outStack = new Stack<>();}/*** Push element x to the back of queue.*/public void push(int x) {// push进入的是inStackinStack.push(x);}/*** Removes the element from in front of queue and returns that element.*/public int pop() {// pop前要先检查:// 如果outStack中还有元素,需要等它先pop完毕才能加入新值// 要将inStack里的元素一次性放入outStackif (outStack.isEmpty()) {in2out();}return outStack.pop();}/*** Get the front element.*/public int peek() {// pop前要先检查:// 如果outStack中还有元素,需要等它先pop完毕才能加入新值// 要将inStack里的元素一次性放入outStackif (outStack.isEmpty()) {in2out();}return outStack.peek();}/*** Returns whether the queue is empty.*/public boolean empty() {return inStack.empty() && outStack.empty();}private void in2out() {while (!inStack.empty()) {outStack.push(inStack.pop());}}}
