-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (56 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// problem https://leetcode.com/problems/min-stack/
/**
* initialize your data structure here.
*/
const MinStack = function () {
this.stack = []
this.min = []
};
/**
* @param {number} x
* @return {void}
*
* 算法:
* 1、当 this.min 为空时,第一个值为最小值
* 2、当 push 进来的值 x 小于当前 this.min 最后一项时,将 x push 进 this.min
* 3、this.min 最后一项为当前栈中最小值
*/
MinStack.prototype.push = function (x) {
this.stack.push(x)
if (this.min.length === 0 || x <= this.min[this.min.length - 1]) {
this.min.push(x)
}
};
/**
* @return {void}
*
* 算法:
* 1、当栈顶元素 pop 出时,判断该栈顶元素是否为当前栈最小值,如果是,则将其 pop
* 2、this.min 最后一项为当前栈中最小值
*/
MinStack.prototype.pop = function () {
const pop = this.stack.pop()
if (pop === this.min[this.min.length - 1]) {
this.min.pop()
}
};
/**
* @return {number}
*/
MinStack.prototype.top = function () {
return this.stack[this.stack.length - 1]
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function () {
return this.min[this.min.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()
*/