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. shell 脚本部分变量含义

    $ # 传递到脚本的参数个数$ * 以一个单字符串显示所有向脚本传递的参数.与位置变量不同,此选项参数可超过9个$ $ 脚本运行的当前进程I D号$ ! 后台运行的最后一个进程的进程I D号$ @ 与 ...

  2. MySQL+Amoeba实现数据库读写分离

    参考:https://www.cnblogs.com/liuyisai/p/6009379.html 一,Amoeba是什么 Amoeba(变形虫)项目,专注 分布式数据库 proxy 开发.座落与C ...

  3. 自动化运维工具-pdsh工具安装配置及简单使用讲解

    1.先决条件: 安装pssh工具的主机针对远程主机需要配置免秘钥认证: ssh-keygen -t rsa ssh-copy-id [remotehost] 2.下载pssh工具安装介质: https ...

  4. bootstrape学习

    bootstrape学习 已分享到有道上:http://note.youdao.com/share/?id=076fb6314c99c742a79f6fb66b2a58b0&type=note ...

  5. fastreport好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了

    好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了 可以用FASTREPORT中的分组打印,你看一下里面的DEMO,里面都有的, 高版本的有suppressRepeat ...

  6. 转:Spring AOP中的动态代理

    原文链接:Spring AOP中的动态代理 0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  S ...

  7. Beanstalkd 基本概念和使用

    1:什么是 Beanstalkd ? Beanstalkd 一个高性能.轻量级的分布式内存队列系统 简单来说,就是一个队列,相比于 数据库/redis 队列相比. 更专业.能完成的功能更多.就这么理解 ...

  8. vuex的小demo

    效果图: vue的app.vue <template> <div> <p>click {{count}} times,count is {{evenOrOdd}}& ...

  9. SSH端口转发详解及实例-转载

    作者:珂儿吖 出处:http://www.cnblogs.com/keerya/ 目录 1.1 SSH端口转发的两大功能 实验一:实现SSH端口转发——本地转发 实验二.实现SSH端口转发——远程转发 ...

  10. [MySQL 5.6] information_schema.innodb_metrics

    1. 概括 已关闭/打开的配置 use information_schema select count(*), status from innodb_metrics group by status; ...