python solution


  • 0
    H
    class MinStack(object):
    
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.stack = []
            self.Min = []
    
        def push(self, x):
            """
            :type x: int
            :rtype: void
            """
            self.stack.append(x)
            self.Min.append(min(sys.maxint if len(self.Min)==0 else self.Min[-1], x))
    
        def pop(self):
            """
            :rtype: void
            """
            self.stack.pop()
            self.Min.pop()
    
        def top(self):
            """
            :rtype: int
            """
            return self.stack[-1]
    
        def getMin(self):
            """
            :rtype: int
            """
            return self.Min[-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()
    

Log in to reply
 

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