Python Solution for 2 lists - beats 90%


  • 0
    K
    class ZigzagIterator(object):
    
        def __init__(self, v1, v2):
            """
            Initialize your data structure here.
            :type v1: List[int]
            :type v2: List[int]
            """
            self.v1 = v1
            self.v2 = v2
            self.l1 = len(v1)
            self.l2 = len(v2)
            self.i, self.j = 0, 0
            self.odd = False
    
        def next(self):
            """
            :rtype: int
            """
            self.odd = not self.odd
            if ((self.i < self.l1 and self.odd) or (self.j >= self.l2 and not self.odd)):
                self.i += 1
                return self.v1[self.i - 1]
            if ((self.i >= self.l1 and self.odd) or (self.j < self.l2 and not self.odd)):
                self.j += 1
                return self.v2[self.j - 1]
                
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return (self.i < self.l1 or self.j < self.l2)
    

Log in to reply
 

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