var MyQueue = function() {
this.input = [];
this.output = [];
};
MyQueue.prototype.move = function(x) {
if(this.output.length) {
return true;
}
this.output = this.input.reverse().concat(this.output);
this.input = [];
}
/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.input.push(x);
};
/**
* @return {number}
*/
MyQueue.prototype.pop = function() {
this.move();
return this.output.pop();
};
/**
* @return {number}
*/
MyQueue.prototype.peek = function() {
this.move();
return this.output[this.output.length - 1];
};
/**
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return !this.output.length && !this.input.length
};
/**
* 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()
*/
【leetcode】 232. 用栈实现队列
最新推荐文章于 2024-07-22 16:27:42 发布