My Python Solution


  • 0
    K

    class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.val = []
        self.mini = None
        
    
    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        self.val.append(x)
        if self.mini is None:
            self.mini = x
        elif x < self.mini:
            self.mini = x
        
    
    def pop(self):
        """
        :rtype: void
        """
        x = self.val.pop(-1)
        if self.val==[]:
            self.mini = None
        elif self.mini == x:
            self.mini = min(self.val)
        
    
    def top(self):
        """
        :rtype: int
        """
        return self.val[-1]
        
    
    def getMin(self):
        """
        :rtype: int
        """
        return self.mini

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.