Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector2D:
- def __init__(self, vec: List[List[int]]):
- self.vec = vec
- # outer list
- self.row = 0
- # inner list
- self.col = 0
- def next(self) -> int:
- if not self.hasNext():
- return
- val = self.vec[self.row][self.col]
- self.col += 1
- # skip empty/exhausted rows right here
- while (self.row < len(self.vec)) and self.col >= len(self.vec[self.row]):
- self.row += 1
- self.col = 0
- return val
- def hasNext(self) -> bool:
- # skip empty rows
- while (self.row < len(self.vec)) and self.col >= len(self.vec[self.row]):
- self.row += 1
- self.col = 0
- if self.row < len(self.vec):
- return True
- else:
- return False
- # Your Vector2D object will be instantiated and called as such:
- # obj = Vector2D(vec)
- # param_1 = obj.next()
- # param_2 = obj.hasNext()
Advertisement
Add Comment
Please, Sign In to add comment