面试题 03.02. 栈的最小值

41 阅读1分钟

请设计一个栈,除了常规栈支持的poppush函数以外,还支持min函数,该函数返回栈元素中的最小值。执行pushpopmin操作的时间复杂度必须为O(1)

示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.

题解:

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

/** 
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function (x) {
    this.stack.push(x)
    this.min_stack.push(Math.min(x, this.min_stack[this.min_stack.length - 1]))
};

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

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

/**
 * @return {number}
 */
MinStack.prototype.getMin = function () {
    return this.min_stack.length && this.min_stack[this.min_stack.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()
 */

来源:力扣(LeetCode)

链接:leetcode.cn/problems/mi…

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。