add: 最小栈
This commit is contained in:
47
src/stack/min-stack.js
Normal file
47
src/stack/min-stack.js
Normal file
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* initialize your data structure here.
|
||||
*/
|
||||
export const MinStack = function () {
|
||||
this.stack = []
|
||||
this.minStack = [Infinity]
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} x
|
||||
* @return {void}
|
||||
*/
|
||||
MinStack.prototype.push = function (x) {
|
||||
this.stack.push(x)
|
||||
this.minStack.push(Math.min(this.minStack[this.minStack.length - 1], x))
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {void}
|
||||
*/
|
||||
MinStack.prototype.pop = function () {
|
||||
this.stack.pop()
|
||||
this.minStack.pop()
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
MinStack.prototype.top = function () {
|
||||
return this.stack[this.stack.length - 1]
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {number}
|
||||
*/
|
||||
MinStack.prototype.getMin = 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.getMin()
|
||||
*/
|
Reference in New Issue
Block a user