You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Initialize your data structure here. */varMyQueue=function(){this.stack=[];this.vmStack=[];};/** * Push element x to the back of queue. * @param {number} x * @return {void} */MyQueue.prototype.push=function(x){for(leti=this.vmStack.length;i>0;i--){this.stack.push(this.vmStack.pop());}this.stack.push(x);};/** * Removes the element from in front of queue and returns that element. * @return {number} */MyQueue.prototype.pop=function(){for(leti=this.stack.length-1;i>=0;i--){this.vmStack.push(this.stack.pop());}returnthis.vmStack.pop();};/** * Get the front element. * @return {number} */MyQueue.prototype.peek=function(){for(leti=this.stack.length-1;i>=0;i--){this.vmStack.push(this.stack.pop());}console.log(this.vmStack);returnthis.vmStack[this.vmStack.length-1];};/** * Returns whether the queue is empty. * @return {boolean} */MyQueue.prototype.empty=function(){return!this.vmStack.length&&!this.stack.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() */
复杂度
时间复杂度——O(n)
空间复杂度——O(n)
The text was updated successfully, but these errors were encountered:
/**
* Initialize your data structure here.
*/
var MyQueue = function() {
this.stack = [];
this.vmStack = [];
};
/**
* Push element x to the back of queue.
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
+ // for (let i = this.vmStack.length; i > 0; i--) {+ // this.stack.push(this.vmStack.pop());+ // }
this.stack.push(x);
};
/**
* Removes the element from in front of queue and returns that element.
* @return {number}
*/
MyQueue.prototype.pop = function() {
+ if (this.vmStack.length) {+ return this.vmStack.pop();+ }
for (let i = this.stack.length - 1; i >= 0; i--) {
this.vmStack.push(this.stack.pop());
}
return this.vmStack.pop();
};
/**
* Get the front element.
* @return {number}
*/
MyQueue.prototype.peek = function() {
+ if (this.vmStack.length) {+ return this.vmStack[this.vmStack.length - 1];+ }
for (let i = this.stack.length - 1; i >= 0; i--) {
this.vmStack.push(this.stack.pop());
}
return this.vmStack[this.vmStack.length - 1];
};
/**
* Returns whether the queue is empty.
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return !this.vmStack.length && !this.stack.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()
*/
面试题 03.04. 化栈为队
解题思路
题目要求用两个栈来实现队列,我们要定义两个栈
this.stack = [];this.vmStack = [];
,因为入栈的操作和队列是一样的,但是出队列方式和出栈的方式不一样,队列是先进先出,栈是先进先出,用辅助栈解决这个问题。可以理解
vmStack
是用来进行出栈时候,现有在stack
的 “倒序”,这样就可以跟出栈一样出队列了代码
复杂度
The text was updated successfully, but these errors were encountered: