解释: MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false
Stack<Integer> input = new Stack(); Stack<Integer> output = new Stack(); /** Initialize your data structure here. */ publicMyQueue(){ } /** Push element x to the back of queue. */ publicvoidpush(int x){ input.push(x); } /** Removes the element from in front of queue and returns that element. */ publicintpop(){ peek(); return output.pop(); } /** Get the front element. */ publicintpeek(){ if(output.isEmpty()){ while(!input.isEmpty()){ output.push(input.pop()); } } return output.peek(); } /** Returns whether the queue is empty. */ publicbooleanempty(){ return input.isEmpty() && output.isEmpty(); } }