easy understanding python solution


  • 0
    W
    class NestedIterator(object):
    
        def __init__(self, nestedList):
            """
            Initialize your data structure here.
            :type nestedList: List[NestedInteger]
            """
            def show(a):
                res=[]
                if a.isInteger():
                    return [a.getInteger()]
                for i in a.getList():
                    res+=show(i)
                return res
                
            res=[]
            for i in nestedList:
                res+=show(i)
            self.nums=res
            self.i=0
    
        def next(self):
            """
            :rtype: int
            """
            self.i+=1
            return self.nums[self.i-1]
            
            
    
        def hasNext(self):
            """
            :rtype: bool
            """
            if self.i<len(self.nums):
                return True
            return False
    

Log in to reply
 

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