Skip to content

Commit 1eca040

Browse files
authored
Update 0155-最小栈.py
1 parent 979228f commit 1eca040

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

0155.最小栈/0155-最小栈.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,29 @@
1-
class MinStack(object):
2-
1+
class MinStack:
2+
33
def __init__(self):
4-
"""
5-
initialize your data structure here.
6-
"""
7-
self.s = []
8-
self.min_s = []
4+
self.data = [(None, float('inf'))]
95

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])))
208

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()
2711

12+
def top(self) -> 'int':
13+
return self.data[-1][0]
2814

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]
3417

35-
def getMin(self):
36-
"""
37-
:rtype: int
38-
"""
39-
return self.min_s[-1]
40-
4118

4219
# Your MinStack object will be instantiated and called as such:
4320
# obj = MinStack()
4421
# obj.push(x)
4522
# obj.pop()
4623
# 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+
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

0 commit comments

Comments
 (0)