341 Flatten Nested List Iterator

https://leetcode.com/problems/flatten-nested-list-iterator/

solution

class NestedIterator:
    def __init__(self, nestedList: [NestedInteger]):
        self.queue = collections.deque()
        self.flat(nestedList)        
    
    def next(self) -> int:
        return self.queue.popleft()        
    
    def hasNext(self) -> bool:
        return self.queue

    def flat(self, nestedList):
        for n in nestedList:            
            if not n.isInteger():
                self.flat(n.getList())
            else:
                self.queue.append(n.getInteger())

时间复杂度:O(n) 空间复杂度:O(n)

Last updated