leetCode-Record icon indicating copy to clipboard operation
leetCode-Record copied to clipboard

面试题30. 包含min函数的栈

Open fireairforce opened this issue 5 years ago • 0 comments

最小栈,这里维护两个栈就行了(因为时间复杂度要为O(1))

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

/**
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function(x) {
  this.stack.push(x)
  if (!this.minStack.length || x <= this.minStack[this.minStack.length - 1]) {
    this.minStack.push(x)
  }
}

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
  let temp = this.stack.pop()
  if (temp === this.minStack[this.minStack.length - 1]) {
    this.minStack.pop()
  }
}

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

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

// let obj = new MinStack()
// obj.push(0)
// obj.push(1)
// obj.push(0)
// console.log(obj.min())
// obj.pop()
// console.log(obj.min())

fireairforce avatar Feb 22 '20 13:02 fireairforce