File tree Expand file tree Collapse file tree 1 file changed +17
-35
lines changed Expand file tree Collapse file tree 1 file changed +17
-35
lines changed Original file line number Diff line number Diff line change 1
- class MinStack ( object ) :
2
-
1
+ class MinStack :
2
+
3
3
def __init__ (self ):
4
- """
5
- initialize your data structure here.
6
- """
7
- self .s = []
8
- self .min_s = []
4
+ self .data = [(None , float ('inf' ))]
9
5
10
- def push (self , x ):
11
- """
12
- :type x: int
13
- :rtype: None
14
- """
15
- self .s .append (x )
16
- if self .min_s :
17
- self .min_s .append (min (x , self .min_s [- 1 ]))
18
- else :
19
- self .min_s .append (x )
6
+ def push (self , x : 'int' ) -> 'None' :
7
+ self .data .append ((x , min (x , self .data [- 1 ][1 ])))
20
8
21
- def pop (self ):
22
- """
23
- :rtype: None
24
- """
25
- self .min_s .pop ()
26
- self .s .pop ()
9
+ def pop (self ) -> 'None' :
10
+ if len (self .data ) > 1 : self .data .pop ()
27
11
12
+ def top (self ) -> 'int' :
13
+ return self .data [- 1 ][0 ]
28
14
29
- def top (self ):
30
- """
31
- :rtype: int
32
- """
33
- return self .s [- 1 ]
15
+ def getMin (self ) -> 'int' :
16
+ return self .data [- 1 ][1 ]
34
17
35
- def getMin (self ):
36
- """
37
- :rtype: int
38
- """
39
- return self .min_s [- 1 ]
40
-
41
18
42
19
# Your MinStack object will be instantiated and called as such:
43
20
# obj = MinStack()
44
21
# obj.push(x)
45
22
# obj.pop()
46
23
# param_3 = obj.top()
47
- # param_4 = obj.getMin()
24
+ # param_4 = obj.getMin()
25
+
26
+ # 作者:
27
+ # 链接:https://leetcode.cn/problems/min-stack/solutions/4114/python-mei-ge-yi-xing-by-knifezhu/
28
+ # 来源:力扣(LeetCode)
29
+ # 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
You can’t perform that action at this time.
0 commit comments