Python, only change push function


  • 0
    Z
    class Queue(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.q = []
    
        def push(self, x):
            """
            :type x: int
            :rtype: nothing
            """
            l = []
            while not self.empty():
                l.append(self.q.pop())
            self.q.append(x)
            while l:
                self.q.append(l.pop())
    
        def pop(self):
            """
            :rtype: nothing
            """
            self.q.pop()
            
    
        def peek(self):
            """
            :rtype: int
            """
            # since python don't have peek function,
            # or we could pop it then push it again
            return self.q[-1] if not self.empty() else 0
    
        def empty(self):
            """
            :rtype: bool
            """
            return True if not self.q else False
            
    

Log in to reply
 

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