码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Nested List Weight Sum"

时间:2016-03-31 02:00:12      阅读:141      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!