What's wrong with my code? In my local IDE, the result is right.


  • -1
    W
    class MinStack:
        # @param x, an integer
        # @return an integer
        def __init__(self):
            self.stack = []
            self.min = int
    
        def push(self, x):
            if len(self.stack) == 0:
                self.stack.append(x)
                self.min = x
    
            self.stack.append(x-self.min)
            if x < self.min:
                self.min = x
        # @return nothing
        def pop(self):
            if len(self.stack) > 0:
                pop = self.stack.pop()
                if pop < 0:
                    self.min = self.min - pop
    
    
        # @return an integer
        def top(self):
            if len(self.stack) == 0:
                return
            else:
                top = self.stack[-1]
                if top < 0:
                    return self.min
                else:
                    return self.min + top
    
        # @return an integer
        def getMin(self):
            return self.min
    

    The wrong answer is:

    Input:     	push(1),push(2),top,getMin,pop,getMin,top
    Output: 	[2147483647,2147483646,2147483646,2147483647,2147483646,-2147483648,-2147483648,2147483646]
    Expected: 	[2,1,1,1]

Log in to reply
 

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