155. 最小栈





思路:

用list模拟栈,list的append()和pop()函数分别模拟进栈和出栈。

class MinStack(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []

    def push(self, x):
        """
        :type x: int
        :rtype: None
        """
        self.stack.append(x)

    def pop(self):
        """
        :rtype: None
        """
        self.stack.pop()

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

    def getMin(self):
        """
        :rtype: int
        """
        return min(self.stack)

posted @ 2020-05-16 13:32  人间烟火地三鲜  阅读(87)  评论(0编辑  收藏  举报