一、题目链接
https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
二、具体代码
/**
* initialize your data structure here.
*/
// 时间复杂度:O(1)
// 空间复杂度:O(n)
var MinStack = function() {
this.stackA = [];
this.stackB = [];
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.stackA.push(x);
if(this.stackB.length === 0 || this.stackB[this.stackB.length - 1] >= x) {
this.stackB.push(x);
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
if(this.stackA.pop() === this.stackB[this.stackB.length - 1]) {
this.stackB.pop();
}
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stackA[this.stackA.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.min = function() {
return this.stackB[this.stackB.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.min()
*/