lintcode 528 摊平嵌套的列表

思路:递归

遍历这个列表,将列表元素放入到递归函数中。递归函数具体是,如果这个元素是个整型数,加入到摊平列表中,否则就是列表,就继续递归。
最后得到摊平了的列表,处理一下即可。

以下版本超时,到92%的测试例子时用时5秒:

"""
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 {boolean} True if this NestedInteger holds a single integer,
        # rather than a nested list.

    def getInteger(self):
        # @return {int} the single integer that this NestedInteger holds,
        # if it holds a single integer
        # Return None if this NestedInteger holds a nested list

    def getList(self):
        # @return {NestedInteger[]} the nested list that this NestedInteger holds,
        # if it holds a nested list
        # Return None if this NestedInteger holds a single integer
"""

class NestedIterator(object):

    def __init__(self, nestedList):
        # Initialize your data structure here.
        self.unpacked = []
        for i in range(len(nestedList)):
            self.dfs(nestedList[i], self.unpacked)
    def dfs(self, nestedobj, tmp_list):
        if nestedobj.isInteger():
            return tmp_list.append(nestedobj.getInteger())
        else:
            nestobj_list = nestedobj.getList()
            for i in range(len(nestobj_list)):
                self.dfs(nestobj_list[i], tmp_list)
        
    # @return {int} the next element in the iteration
    def next(self):
        # Write your code here
        tmp = self.unpacked[0]
        self.unpacked = self.unpacked[1:]
        return tmp 
        
    # @return {boolean} true if the iteration has more element or false
    def hasNext(self):
        # Write your code here
        if len(self.unpacked):
            return True
        else:
            return False 


# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())

更改后通过,代码如下:

"""
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 {boolean} True if this NestedInteger holds a single integer,
        # rather than a nested list.

    def getInteger(self):
        # @return {int} the single integer that this NestedInteger holds,
        # if it holds a single integer
        # Return None if this NestedInteger holds a nested list

    def getList(self):
        # @return {NestedInteger[]} the nested list that this NestedInteger holds,
        # if it holds a nested list
        # Return None if this NestedInteger holds a single integer
"""

class NestedIterator(object):

    def __init__(self, nestedList):
        # Initialize your data structure here.
        self.unpacked = []
        for i in range(len(nestedList)):
            self.dfs(nestedList[i], self.unpacked)
    def dfs(self, nestedobj, tmp_list):
        if nestedobj.isInteger():
            return tmp_list.append(nestedobj.getInteger())
        else:
            nestobj_list = nestedobj.getList()
            for i in range(len(nestobj_list)):
                self.dfs(nestobj_list[i], tmp_list)
        
    # @return {int} the next element in the iteration
    def next(self):
        # Write your code here
        tmp = self.unpacked[0]
        self.unpacked = self.unpacked[1:]
        return tmp 
        
    # @return {boolean} true if the iteration has more element or false
    def hasNext(self):
        # Write your code here
        if len(self.unpacked):
            return True
        else:
            return False 


# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())

超时原因:

next函数,每次除去一个函数的时候,都用切片切出新列表,然后再将新列表赋给旧列表,这个过程涉及到新列表的建立,也就是,每次除去一个元素都要建立一次列表,这个过程过于耗时。
解决办法,在init函数里,先将得到的摊平了的列表倒序,然后,在next函数里取的时候,pop掉末尾的即可。
这样改了以后,最长的耗时不到两秒。
原先以为dfs耗时导致的超时,如果真是这样,要么剪枝要么不用递归,思路可能都要大改,看了下,没有可以剪的地方,不到万不得已不改思路,所以,先尝试改一下别的地方,结果可以通过,惊险,dfs是个好东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值