Python Solution beats 95%


  • 0
    M
    class Vector2D(object):
    
        def __init__(self, vec2d):
            """
            Initialize your data structure here.
            :type vec2d: List[List[int]]
            """
            from collections import deque
            
            self.dq = deque(vec2d)
            self.curr_list = []
            self.curr_pos = 0
    
        def next(self):
            """
            :rtype: int
            """
            while self.curr_pos >= len(self.curr_list):
                self.curr_list = self.dq.popleft()
                self.curr_pos = 0
            ret = self.curr_list[self.curr_pos]
            self.curr_pos += 1
            return ret
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return any(self.dq) or self.curr_pos < len(self.curr_list)
    

Log in to reply
 

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