解释: 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
/** Removes the element from in front of queue and returns that element. */ func(this *MyQueue) Pop() int { iflen(this.out) == 0 { // 将输入栈 弹出到输出栈中 this.in2out() } out := this.out[len(this.out)-1] this.out = this.out[:len(this.out)-1] return out }
/** Get the front element. */ func(this *MyQueue) Peek() int { iflen(this.out) == 0 { // 将输入栈 弹出到输出栈中 this.in2out() } return this.out[len(this.out)-1] }