Skip to content

Latest commit

 

History

History
21 lines (21 loc) · 466 Bytes

7.md

File metadata and controls

21 lines (21 loc) · 466 Bytes

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。


let MinStack = function() {
  this.item = []
  return this
}
MinStack.prototype.push = function(x) {
  this.item.push(x)
}
MinStack.prototype.pop = function() {
  if (this.item.length) {
    this.item.splice(this.item.length -1, 1)
    return this.item
  }
  return undefined
}
MinStack.prototype.getMin = function() {
  return Math.min(...this.item)
}