Help me! my Top() just doesn't work. Thank you, guys.


  • 0
    G
    enter code hereclass Stack(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.queue=collections.deque()
    
    def push(self, x):
        """
        :type x: int
        :rtype: nothing
        """
        self.queue.append(x)
    
    def pop(self):
        """
        :rtype: nothing
        """
        if self.queue and len(self.queue)>1:
            for i in range(len(self.queue)-1,0,-1):
                self.queue[i]=self.queue[i-1]
            self.queue.popleft()    
        if self.queue and len(self.queue)==1:
            self.queue.popleft()
        
    def top(self):
        """
        :rtype: int
        """
        if len(self.queue)==0:
            return None
        return self.queue[len(self.queue)-1]
    
    def empty(self):
        """
        :rtype: bool
        """
        return len(self.queue)==0

  • 0
    J

    What do you mean "doesn't work"? Could not pass any test case?


Log in to reply
 

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