Fast , clean and easy understand python code


  • 0
    Y
    class Queue(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.s1 = []
            self.s2 = []
    
        def push(self, x):
            """
            :type x: int
            :rtype: nothing
            """
            self.s1.append(x)
            
    
        def pop(self):
            """
            :rtype: nothing
            """
            if len(self.s2)==0:
                while self.s1:
                    self.s2.append(self.s1.pop())
            return self.s2.pop()
    
        def peek(self):
            """
            :rtype: int
            """
            if len(self.s2)==0:
                while self.s1:
                    self.s2.append(self.s1.pop())
            return self.s2[-1]
            
    
        def empty(self):
            """
            :rtype: bool
            """
            if not self.s1 and not self.s2:
                return True
            else:
                return False
    

Log in to reply
 

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