225. Implement Stack using Queues
Posted strivegys
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了225. Implement Stack using Queues相关的知识,希望对你有一定的参考价值。
/** * Initialize your data structure here. */ var MyStack = function() { this.inQueue = []; this.outQueue = []; }; /** * Push element x onto stack. * @param {number} x * @return {void} */ MyStack.prototype.push = function(x) { this.inQueue.push(x); }; /** * Removes the element on top of the stack and returns that element. * @return {number} */ MyStack.prototype.pop = function() { this.checkQueue(); return this.outQueue.shift(); }; /** * Get the top element. * @return {number} */ MyStack.prototype.top = function() { this.checkQueue(); return this.outQueue[0]; }; /** * check the queue element * @return { void } */ MyStack.prototype.checkQueue = function() { while(this.outQueue.length) { this.inQueue.unshift(this.outQueue.shift()); } while(this.inQueue.length) { this.outQueue.push(this.inQueue.pop()); } } /** * Returns whether the stack is empty. * @return {boolean} */ MyStack.prototype.empty = function() { return (this.inQueue.length == 0 && this.outQueue.length == 0); }; /** * Your MyStack object will be instantiated and called as such: * var obj = new MyStack() * obj.push(x) * var param_2 = obj.pop() * var param_3 = obj.top() * var param_4 = obj.empty() */
以上是关于225. Implement Stack using Queues的主要内容,如果未能解决你的问题,请参考以下文章
225. Implement Stack using Queues
225. Implement Stack using Queues
225. Implement Stack using Queues
225. Implement Stack using Queues