01刷题

 

 

 

```js
function MinStack() {
this.stack = []
this.helpStack = []
this.min = Infinity

}
MinStack.prototype.push = function(item) {
this.stack.push(item)
this.min = this.min > item ? item : this.min
this.helpStack.push(this.min)

}
MinStack.prototype.pop = function() {
const item = this.stack.pop()
//如果删了stack里面的最小值,就需要删除helpStack里面所有的最小值,while循环删除
while (this.min === item) {
this.min = this.helpStack.pop()
}

return item
}
MinStack.prototype.getMin = function() {

return this.min
}

const s = new MinStack()
const arr = [3]
arr.map(item => s.push(item))
console.log(s)

while (s.stack.length) {
let res = s.pop()
console.log(res, s.getMin())

}
```

posted on 2021-02-08 09:48  章画  阅读(35)  评论(0编辑  收藏  举报

导航