Simple Python Solution


  • 1
    class Vector2D(object):
    
        def __init__(self, vec2d):
            self.x, self.y = 0, 0
            self.vec2d = vec2d
    
        def next(self):
            if self.hasNext():
                ret = self.vec2d[self.x][self.y]
                self.y += 1
                return ret
    
        def hasNext(self):
            if self.x >= len(self.vec2d):
                return False
            if not self.vec2d[self.x]:
                self.x += 1
                return self.hasNext()
            if self.y >= len(self.vec2d[self.x]):
                self.y = 0
                self.x += 1
                return self.hasNext()
            return True

Log in to reply
 

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