Python generator solution


  • 0
    Z
    class Vector2D(object):
        def __init__(self, v):
            def chain():
                x, y = 0, 0
                while x < len(v):
                    if len(v[x]) == y:
                        x += 1
                        y = 0
                    else:
                        yield v[x][y]
                        y += 1
            self.q = chain()
            
        def next(self):
            return self.val
    
        def hasNext(self):
            try:
                self.val = next(self.q)
                return True
            except StopIteration:
                return False

Log in to reply
 

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