美文网首页
341. Flatten Nested List Iterato

341. Flatten Nested List Iterato

作者: codingXue | 来源:发表于2016-09-10 10:07 被阅读130次

    问题描述

    Given a nested list of integers, implement an iterator to flatten it.
    Each element is either an integer, or a list -- whose elements may also be integers or other lists.
    Example 1:
    Given the list [[1,1],2,[1,1]],
    By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
    Example 2:
    Given the list [1,[4,[6]]],
    By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

    问题分析

    运用栈的思想,当栈为空时,从NestedList头部拿一个元素到stack;
    当stack的栈顶元素不为Integer,则取栈顶原素,将其列表成员按逆序加入栈;

    需要注意的一点是,其实取next的主要工作是在hasNext做的,因为只有得到了next整数,才能证明有next,而取next只要取栈顶元素即可。还需要注意,NestedList可能是有空数组,例如“[[]]”。

    AC代码

    # """
    # This is the interface that allows for creating nested lists.
    # You should not implement it, or speculate about its implementation
    # """
    #class NestedInteger(object):
    #    def isInteger(self):
    #        """
    #        @return True if this NestedInteger holds a single integer, rather than a nested list.
    #        :rtype bool
    #        """
    #
    #    def getInteger(self):
    #        """
    #        @return the single integer that this NestedInteger holds, if it holds a single integer
    #        Return None if this NestedInteger holds a nested list
    #        :rtype int
    #        """
    #
    #    def getList(self):
    #        """
    #        @return the nested list that this NestedInteger holds, if it holds a nested list
    #        Return None if this NestedInteger holds a single integer
    #        :rtype List[NestedInteger]
    #        """
    
    class NestedIterator(object):
    
        def __init__(self, nestedList):
            """
            Initialize your data structure here.
            :type nestedList: List[NestedInteger]
            """
            self.stack = []
            self.nested = nestedList        
    
        def next(self):
            """
            :rtype: int
            """
            
            return self.stack.pop().getInteger()       
    
        def hasNext(self):
            """
            :rtype: bool
            """
            while self.stack or self.nested:
                if not self.stack:
                    self.stack.append(self.nested.pop(0))
                while self.stack and not self.stack[-1].isInteger():
                    tmp = self.stack.pop().getList()
                    for t in tmp[::-1]:
                        self.stack.append(t)
                if self.stack:
                    return True
            return False
           
    # Your NestedIterator object will be instantiated and called as such:
    # i, v = NestedIterator(nestedList), []
    # while i.hasNext(): v.append(i.next())
    

    相关文章

      网友评论

          本文标题:341. Flatten Nested List Iterato

          本文链接:https://www.haomeiwen.com/subject/gceyettx.html