解题思路
栈是先进后出 FILO,队列是先进先出 FIFO。
利用两个栈,一个叫做“输入栈”,另一个叫做“输出栈”。当执行 push
操作当时候,往输入栈里添加元素;当执行 pop
和 peek
操作当时候,先检查“输出栈”里有没有元素,如果有则直接出,如果没有,就将输入栈里当元素依次放入输出栈里。
代码
➥ JavaScript
/**
* Initialize your data structure here.
*/
var MyQueue = function() {
this.inputStack = []
this.outputStack = []
};
/**
* Push element x to the back of queue.
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.inputStack.push(x)
};
/**
* Removes the element from in front of queue and returns that element.
* @return {number}
*/
MyQueue.prototype.pop = function() {
if (this.outputStack.length === 0) {
while(this.inputStack.length > 0) {
this.outputStack.push(this.inputStack.pop())
}
}
return this.outputStack.pop()
};
/**
* Get the front element.
* @return {number}
*/
MyQueue.prototype.peek = function() {
if (this.outputStack.length === 0) {
while(this.inputStack.length > 0) {
this.outputStack.push(this.inputStack.pop())
}
}
return this.outputStack[this.outputStack.length - 1]
};
/**
* Returns whether the queue is empty.
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.inputStack.length + this.outputStack.length === 0
};
/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/