并行两个栈
注意:
1.python最后一个元素下标。list[-1]
2.python最大值math.inf
执行用时:60 ms, 在所有 Python3 提交中击败了92.45%的用户
内存消耗:18 MB, 在所有 Python3 提交中击败了22.76%的用户
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack=[]
self.minstack=[math.inf]
def push(self, val: int) -> None:
self.stack.append(val)
self.minstack.append(min(self.minstack[-1],val))
def pop(self) -> None:
if self.stack!=None:
self.stack.pop()
self.minstack.pop()
def top(self) -> int:
if self.stack!=None:
return self.stack[-1]
def getMin(self) -> int:
if self.minstack!=None:
return self.minstack[-1]
# 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()