-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path155. 最小栈
More file actions
35 lines (28 loc) · 834 Bytes
/
155. 最小栈
File metadata and controls
35 lines (28 loc) · 834 Bytes
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
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.a=[]
self.a_min=float('inf')
def push(self, x: int) -> None:
self.a.append(x)
# 加入元素的时候检查最小值
self.a_min=x if x<self.a_min else self.a_min
def pop(self) -> None:
# 删除元素的时候 重新检查最小值
result = self.a[-1]
del self.a[-1]
if result==self.a_min:
self.a_min=min(self.a) if self.a else float('inf')
return result
def top(self) -> int:
return self.a[-1]
def getMin(self) -> int:
return self.a_min
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()