题目:
![题目](https://i-blog.csdnimg.cn/blog_migrate/82f39992938636bec9ec57b29d193d71.png)
思路:
代码:
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.data = []
self.helper = []
def push(self, x: int) -> None:
self.data.append(x)
if len(self.helper) == 0 or x <= self.helper[-1]:
self.helper.append(x)
def pop(self) -> None:
if self.data:
if self.helper and self.data[-1] == self.helper[-1]:
self.helper.pop()
self.data.pop()
def top(self) -> int:
if self.data:
return self.data[-1]
def getMin(self) -> int:
if self.helper:
return self.helper[-1]
else:
return 0