首页 > 其他 > 详细

LeetCode "Nested List Weight Sum"

时间:2016-03-31 01:57:56      阅读:177      评论:0      收藏:0      [点我收藏+]

An intuitive DFS one.

class Solution(object):
    def go(self, nList, dep):
        ret = 0
        for i in range(len(nList)):
            if(nList[i].isInteger()):
                ret += dep * (nList[i].getInteger())
            else:
                ret += self.go(nList[i].getList(), dep + 1)
        return ret

    def depthSum(self, nestedList):
        return self.go(nestedList, 1)

LeetCode "Nested List Weight Sum"

原文:http://www.cnblogs.com/tonix/p/5339629.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!