let Stack = function () {
this.items = []
this.min = null
}
Stack.prototype.push = function (item) {
this.items.push(item)
if (this.min === null) {
this.min = item
}
this.min = Math.min(item, this.min)
}
Stack.prototype.pop = function () {
return this.items.pop()
}
Stack.prototype.top = function () {
if (this.items.length === 0) {return null}
return this.items[this.items.length - 1]
}
Stack.prototype.isEmpty = function () {
return this.items.length === 0
}
Stack.prototype.size = function () {
return this.items.length
}
Stack.prototype.clear = function () {
this.items.length = 0
}
Stack.prototype.getMin = function () {
return this.min
}
let stack = new Stack()
stack.push(12)
stack.push(13)
console.log(stack)
console.log(stack.isEmpty())
console.log(stack.getMin())
console.log(stack.top())
进栈O(1),出栈O(n),获取栈顶元素O(1),获取最小元素O(1)