File tree 1 file changed +59
-0
lines changed
1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=155 lang=javascript
3
+ *
4
+ * [155] 最小栈
5
+ *
6
+ * 1. 用两个栈, A 栈存数据, B 栈存当前时刻的最小值
7
+ * 2. top 即是 A 栈顶, min 即是 B 栈顶
8
+ */
9
+
10
+ // @lc code=start
11
+ /**
12
+ * initialize your data structure here.
13
+ */
14
+ var MinStack = function ( ) {
15
+ this . stack = [ ] ;
16
+ this . min = [ ] ;
17
+ } ;
18
+
19
+ /**
20
+ * @param {number } x
21
+ * @return {void }
22
+ */
23
+ MinStack . prototype . push = function ( x ) {
24
+ this . min . push ( this . stack . length === 0 ? x : Math . min ( this . min [ this . min . length - 1 ] , x ) ) ;
25
+ this . stack . push ( x ) ;
26
+ } ;
27
+
28
+ /**
29
+ * @return {void }
30
+ */
31
+ MinStack . prototype . pop = function ( ) {
32
+ this . stack . pop ( ) ;
33
+ this . min . pop ( ) ;
34
+ } ;
35
+
36
+ /**
37
+ * @return {number }
38
+ */
39
+ MinStack . prototype . top = function ( ) {
40
+ return this . stack [ this . stack . length - 1 ] ;
41
+ } ;
42
+
43
+ /**
44
+ * @return {number }
45
+ */
46
+ MinStack . prototype . getMin = function ( ) {
47
+ return this . min [ this . min . length - 1 ] ;
48
+ } ;
49
+
50
+ /**
51
+ * Your MinStack object will be instantiated and called as such:
52
+ * var obj = new MinStack()
53
+ * obj.push(x)
54
+ * obj.pop()
55
+ * var param_3 = obj.top()
56
+ * var param_4 = obj.getMin()
57
+ */
58
+ // @lc code=end
59
+
You can’t perform that action at this time.
0 commit comments