设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
- push(x) —— 将元素 x 推入栈中。
- pop() —— 删除栈顶的元素。
- top() —— 获取栈顶元素。
- getMin() —— 检索栈中的最小元素。
示例:
输入:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
输出:
[null,null,null,null,-3,null,0,-2]
解释:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
提示:
- pop、top 和 getMin 操作总是在 非空栈 上调用。
解题思路
1.定义两个栈stack和min_stack,stack正常push,min_stack只会push需要入栈和栈顶中较小的元素;pop删除两个栈stack和min_stack的栈顶元素;top返回stack栈顶元素;getMin返回min_stack栈顶元素。
2.注意:在辅助栈中,存放着每一位主栈元素对应的最小值。
代码
var MinStack = function() {
this.stack = []
this.min_stack = [Infinity]
};
/**
* @param {number} val
* @return {void}
*/
// stack正常push,min_stack只会push需要入栈和栈顶中较小的元素
MinStack.prototype.push = function(val) {
this.stack.push(val);
this.min_stack.push(Math.min(this.min_stack[this.min_stack.length - 1], val));
};
/**
* @return {void}
*/
// stack正常pop,min_stack正常pop
MinStack.prototype.pop = function() {
this.stack.pop();
this.min_stack.pop();
};
/**
* @return {number}
*/
// 返回stack栈顶元素
MinStack.prototype.top = function() {
return this.stack[this.stack.length - 1];
};
/**
* @return {number}
*/
// 返回min_stack栈顶元素
MinStack.prototype.getMin = function() {
return this.min_stack[this.min_stack.length - 1];
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/