Min Stack


  • 0
    S
    class MinStack(object):
    
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.stack = []
    
        def push(self, x):
            """
            :type x: int
            :rtype: void
            """
            try:
                self.stack.append((x, min(x,self.stack[-1][1])))
            except:
                self.stack.append((x,x))
    
        def pop(self):
            """
            :rtype: void
            """
            try:
                self.stack.pop()
            except:
                return
    
        def top(self):
            """
            :rtype: int
            """
            try:
                return self.stack[-1][0]
            except:
                return None
            
    
        def getMin(self):
            """
            :rtype: int
            """
            try:
                return self.stack[-1][1]
            except:
                return None
    
    
    # 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.