Can you Tell what is wrong with this Program?


  • 0
    S

    Runtime Error Message: Line 44: TypeError: init() takes exactly 2 arguments (1 given)
    Last executed input: push(-3),getMin

    class MinStack:
        # @param x, an integer
        # @return an integer
        def __init__ (self, x):
            self.data=x
            self.next=None
            self.mini=x
        
        head=None
        
        def push(self, x):
            n=self.__init(x)
            if head is not None:
                n.next=head
                n.mini(head.mini,n.mini)
            head=n
    
        # @return nothing
        def pop(self):
            if head is None:
                return None
            n=head
            head=head.next
            x=n.data
            del n
            return x
    
        # @return an integer
        def top(self):
            if head is not None:
                return head.data
            return None
    
        # @return an integer
        def getMin(self):
            if head is not None:
                return head.mini
            return None

  • 1
    G

    in this case, MinStack 's consturctor init should not have arguments.


  • 0
    S

    Why? Please Elaborate.


  • 0
    K

    example Initialization of your class:
    stack = MinStack(5)
    example initialization of assignment class:
    stack = MinStack()

    There is nothing about initializing stack with first element (x in your init) in text of the assignment. Also, the text of the assignment clearly states how the class MinStack should look and behave.


Log in to reply
 

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