Given a nested list of integers, return the sum of all integers in the list weighted by their depth.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Example 1:

Input: [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1's at depth 2, one 2 at depth 1.

Example 2:

Input: [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27.

题目思路为DFS, 用一个helper function去记录nestInteger的值, 传入的input包括depth.

Data Structure.

# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger(object):
# def __init__(self, value=None):
# """
# If value is not specified, initializes an empty list.
# Otherwise initializes a single integer equal to value.
# """
#
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def add(self, elem):
# """
# Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
# :rtype void
# """
#
# def setInteger(self, value):
# """
# Set this NestedInteger to hold a single integer equal to value.
# :rtype void
# """
#
# 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]
# """

Code

class Solution:
def NestedListWeightSum(self, nestedList):
def helper(nestedList, depth):
s = 0
for each in nestedList:
if each.isInteger():
s += each.getInteger() * depth
else:
s += dfs(each.getList(), depth + 1)
return s
return helper(nestedList, 1)

[LeetCode] 339. Nested List Weight Sum_Easy tag:DFS的更多相关文章

  1. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  2. LeetCode 339. Nested List Weight Sum (嵌套列表重和)$

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  3. [leetcode]339. Nested List Weight Sum嵌套列表加权和

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  4. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  5. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  6. 【leetcode】339. Nested List Weight Sum

    原题 Given a nested list of integers, return the sum of all integers in the list weighted by their dep ...

  7. [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  8. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  9. 【LeetCode】339. Nested List Weight Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...

随机推荐

  1. js 去掉花括号

    "asd {{name}} {{age}}".replace(/{{(.*?)}}/g,'$1'); // "asd name age" "asd { ...

  2. h5 . css入门 2.CSS基础

    CSS基础 学习目标 1.CSS简介 2.CSS语法 3.样式的创建 4.两种引入外部样式表的区别 5.样式表的优先级和作用域 6.CSS选择器 7.选择器的权重 8.浮动属性的简单应用 9.HTML ...

  3. 170824、storm 环境搭建

    /*********************storm 环境搭建 **********************/ 1 安装jdk1.7 2 安装zookeeper集群环境 3 安装Python2.6. ...

  4. .NET Core开发日志——Peachpie

    .NET Core的生态圈随着开源社区的力量不断注入至其中,正在变得越来越强盛,并且不时得就出现些有意思的项目,比如Peachpie,它使得PHP的代码迁移到.NET Core项目变得可能. 从创建简 ...

  5. 品尝阿里云容器服务:用nginx镜像创建容器,体验基于域名的路由机制

    在前一篇博文中我们了解了阿里云容器服务的路由机制: 请求 -> 负载均衡80端口 -> 容器主机9080端口 -> acsrouting路由容器80端口 --基于域名--> W ...

  6. [No0000C4]TortoiseSVN配置外部对比工具

    TortoiseSVN -> Settings Diff Viewer : 选中External->找到外部工具:如BCompare.路径如果有空格的最好用"双引号"括 ...

  7. linux 编程

    edit skill: 1. i:当前光标前插入 2. a:当前光标后插入 3. SHIFT+a—行尾插入 4. SHIFT+i—行首插入 5. o—>下一行插入 6. SHIFT+o--上一行 ...

  8. 挖矿程序的工作原理(BTC为例)

    Mining时代进化:CPU挖矿 -> GPU挖矿 -> FPGA挖矿 -> ASIC挖矿CPU挖矿时代:SENGENERATEGPU挖矿时代:GETWORK Miner:挖矿的程序 ...

  9. PP图和QQ图 检查2个数据集是否符合同一分布

    1.QQ图检查2个数据集是否符合同一分布,Purpose:Check If Two Data Sets Can Be Fit With the Same Distribution PP图和QQ图 - ...

  10. python发送邮件 大全汇总

    https://blog.csdn.net/bmxwm/article/details/79007871 参考菜鸟教程发送只有文字的邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...