My Python Using DFS And Stack


  • 0
    J
    class NestedIterator(object):
    
        def __init__(self, nestedList):
            """
            Initialize your data structure here.
            :type nestedList: List[NestedInteger]
            """
            self.l = []
            self.idx = 0
            temp = []
            for i in nestedList:
                temp.append(i)
                while temp:
                    t = temp.pop()
                    if t.isInteger():
                        self.l.append(t.getInteger())
                    else:
                        temp.extend(t.getList()[::-1]) 
    
        def next(self):
            """
            :rtype: int
            """
            result = self.l[self.idx]
            self.idx += 1
            return result
    
        def hasNext(self):
            """
            :rtype: bool
            """
            if self.idx < len(self.l):
                return True
            else:
                return False
    

Log in to reply
 

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