0
点赞
收藏
分享

微信扫一扫

LeetCode力扣 155. 最小栈 Min Stack 题解代码 JavaScript


问题 ​​https://leetcode-cn.com/problems/min-stack/​​

练习使用JavaScript解答

/**
* initialize your data structure here.
*/
var MinStack = function() {
this.stack = [];
this.order = [];
};

/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.stack.push(x);
var i = this.order.length-1;
if(i<0)
this.order.push(x);
else
this.order.push((this.order[i] < x) ? this.order[i] : x );
};

/**
* @return {void}
*/
MinStack.prototype.pop = function() {
this.stack.pop();
this.order.pop();
};

/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stack[this.stack.length-1];
};

/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.order[this.order.length-1];
};

/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/

 

举报

相关推荐

0 条评论