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. day_6.14py网络编程

    被动套接字和新建出套接字的区别 单任务,单线程,非阻塞!!!!服务器! #2018-6-14 16:51:25 #!--*--coding:utf-8 --*-- ''' 单进程,单任务 少的可以,多 ...

  2. 【Java并发编程六】线程池

    一.概述 在执行并发任务时,我们可以把任务传递给一个线程池,来替代为每个并发执行的任务都启动一个新的线程,只要池里有空闲的线程,任务就会分配一个线程执行.在线程池的内部,任务被插入一个阻塞队列(Blo ...

  3. 网络通信协议八之UDP协议详解

    视频传输中使用UDP协议比较多 UDP协议的责任 >>创建进程到进程间的通信(由端口号完成) >>有限的差错控制,出现差错悄悄丢弃报文(注意这点和TCP协议的区别)

  4. 【Linux】解决Android Stadio报错:error in opening zip file

    报错: Failed to complete Gradle Execution Cause: error in opening zip file. 原因: 安装gradle失败引起的,往往是上网需要验 ...

  5. Zabbix使用grafana展示图形

    系统环境查看 官网下载grafana wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.1-1. ...

  6. shell 基本命令

    Shell基本命令   前言 前面咱们已经成功安装了Linux系统--centos7,那么现在跟着超哥奔向Linux的大门. Linux命令行的组成结构 [root@oldboy_python ~]# ...

  7. [No0000180]改善C#程序的建议8:避免锁定不恰当的同步对象

    在C#中让线程同步的另一种编码方式就是使用线程锁.所谓线程锁,就是锁住一个资源,使得应用程序只能在此刻有一个线程访问该资源.可以用下面这句不是那么贴切的话来理解线程锁的作用:锁,就是让多线程变成单线程 ...

  8. Converting Python Virtual Machine Code to C

    Converting Python Virtual Machine Code to C

  9. IndentationError: expected an indented block 在继承中出现的问题:未完

    1. class Foo(object): def __init__(self,name,price,period): self.name=name self.price=price self.per ...

  10. 转:Redis 3.2.1集群搭建

    Redis 3.2.1集群搭建   一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2) ...