-
Notifications
You must be signed in to change notification settings - Fork 0
/
最小栈.py
38 lines (32 loc) · 914 Bytes
/
最小栈.py
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
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.min = None
def push(self, val: int) -> None:
self.stack.append(val)
if self.min == None or self.min > val:
self.min = val
def pop(self) -> None:
popItem = self.stack.pop()
if len(self.stack) == 0:
self.min = None
return popItem
if popItem == self.min:
self.min = self.stack[0]
for i in self.stack:
if i < self.min:
self.min = i
return popItem
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()