-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.py
More file actions
49 lines (40 loc) · 1.01 KB
/
Copy pathMinStack.py
File metadata and controls
49 lines (40 loc) · 1.01 KB
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
39
40
41
42
43
44
45
46
47
48
49
# coding=utf-8
"""
@project : algorithmPython
@ide : PyCharm
@file : MinStack
@author : illusion
@desc : 155. 最小栈 https://leetcode-cn.com/problems/min-stack/
@create : 2021/5/26 1:38 下午:22
"""
class MinStack:
def __init__(self):
self.stack = []
self.min = []
def push(self, val: int) -> None:
self.stack.append(val)
if not len(self.min):
self.min.append(val)
return
last_min = self.min[-1]
if val < last_min:
self.min.append(val)
else:
self.min.append(last_min)
def pop(self) -> None:
self.stack.pop()
self.min.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min[-1]
if __name__ == '__main__':
minStack = MinStack()
minStack.push(2)
minStack.push(0)
minStack.push(3)
print(minStack.getMin())
minStack.pop()
print(minStack.getMin())
minStack.pop()
print(minStack.getMin())