力扣 155. 最小栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import collections
class MinStack:

def __init__(self):
self.stack = collections.deque()
self.min_stack = collections.deque()

def push(self, val: int) -> None:
self.stack.append(val)
if not self.min_stack or self.min_stack[-1] >= val:
self.min_stack.append(val)

def pop(self) -> None:
cur = self.stack.pop()
if cur == self.min_stack[-1]: self.min_stack.pop()

def top(self) -> int:
return self.stack[-1]

def getMin(self) -> int:
return self.min_stack[-1]
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信