https://leetcode-cn.com/problems/min-stack/
    需要注意的点
    思路:
    维护两个栈,一个保存当前值的最小值,一个保存当前值
    当推入/推出当前值时,推入/推出当前值的最小值
    要理解栈的原型
    leetcode cn 的官方解释很清楚

    1. import math
    2. class MinStack(object):
    3. '''
    4. 思路:
    5. 维护两个栈,一个保存当前值的最小值,一个保存当前值
    6. 当推入/推出当前值时,推入/推出当前值的最小值
    7. 要理解栈的原型
    8. leetcode cn 的官方解释很清楚
    9. '''
    10. # 维护一个最小值栈
    11. def __init__(self):
    12. self.list = []
    13. self.minlist = [math.inf]
    14. '''
    15. '''
    16. def push(self, val):
    17. """
    18. :type val: int
    19. :rtype: None
    20. """
    21. self.list.append(val)
    22. self.minlist.append(min(val, self.minlist[-1]))
    23. def pop(self):
    24. """
    25. :rtype: None
    26. """
    27. self.list.pop()
    28. self.minlist.pop()
    29. def top(self):
    30. """
    31. :rtype: int
    32. """
    33. return self.list[-1]
    34. def getMin(self):
    35. """
    36. :rtype: int
    37. """
    38. return self.minlist[-1]
    39. # Your MinStack object will be instantiated and called as such:
    40. # obj = MinStack()
    41. # obj.push(val)
    42. # obj.pop()
    43. # param_3 = obj.top()
    44. # param_4 = obj.getMin()