class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack=[]
self.min_stack=[]
def push(self, x: int) -> None:
self.stack.append(x)
if len(self.min_stack)==0:
self.min_stack.append(x)
else:
if x<self.min_stack[-1]:
self.min_stack.append(x)
elif x==self.min_stack[-1]:
self.min_stack.append(self.min_stack[-1])
def pop(self) -> None:
x=self.stack.pop()
if self.min_stack[-1]==x:
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
# 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()
leetcode155最小栈
最新推荐文章于 2024-06-05 17:32:08 发布