原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/

题目:

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:
Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)

Example 2:
Given the list [1,[4,[6]]], return 27. (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27)

题解:

For dfs state, it needs current nested list and current depth.

For each NestedInteger ni in the list, if it is integer, add its value * depth to res. Otherwise, continue DFS with it and depth+1.

Time Complexity: O(n). n 是指全部叶子的数目加上dfs走过层数的总数. [[[[[5]]]],[[3]], 1], 3个叶子, dfs一共走了6层. 所以用了 3 + 6 = 9 的时间.

Space: O(D). D 是recursive call用的stack的最大数目, 即是最深的层数, 上面例子最深走过4层, 这里D = 4.

AC Java:

 /**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class Solution {
public int depthSum(List<NestedInteger> nestedList) {
return dfs(nestedList, 1);
}
private int dfs(List<NestedInteger> nestedList, int depth){
int sum = 0;
for(NestedInteger item : nestedList){
if(item.isInteger()){
sum += item.getInteger()*depth;
}else{
sum += dfs(item.getList(), depth+1);
}
}
return sum;
}
}

类似Employee ImportanceNested List Weight Sum II.

LeetCode 339. Nested List Weight Sum的更多相关文章

  1. 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. ...

  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]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. ...

  4. 【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 ...

  5. [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. ...

  6. [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. ...

  7. LeetCode 364. Nested List Weight Sum II

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

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

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

  9. 339. Nested List Weight Sum

    https://leetcode.com/problems/nested-list-weight-sum/description/ Given a nested list of integers, r ...

随机推荐

  1. MyBatis逆向工程生成配置 generator (生成pojo、mapper.xml、mapper.java)

    MyBatis逆向工程生成 mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java.mapper.xml ...

  2. Visual Studio 2019 离线安装方法

    1. 网址 1.1 阅读官方离线安装教程 离线安装官网 仔细阅读离线安装官网,差不多就能学会如何下载. 1.2 工作负荷和组件 ID 进入这个网址,Visual Studio 工作负荷和组件 ID,单 ...

  3. php面向对象之封装

    OOP三大特性:封装.继承和多态,简称封继态. 封装 类2使用关键字extends继承类1,之后,类1为类2的父类,简称父类,类2是类1的子类,简称子类.使用关键字new,实例化类1,得到对象1,对象 ...

  4. flutter从入门到精通四

    widget Flutter 从 React 中吸取灵感(如果有react的编程经验,会很容易理解flutter),通过现代化框架创建出精美的组件. 它的核心思想是用 widget 来构建你的 UI ...

  5. 论坛中的问题:47(等待类型为MSSEARCH的进程被KILL之后,一直处于回滚状态)

    原文:论坛中的问题:47(等待类型为MSSEARCH的进程被KILL之后,一直处于回滚状态) 求助:等待类型为MSSEARCH的进程被KILL之后,一直处于回滚状态 http://bbs.csdn.n ...

  6. Linux入职基础-1.1_国内开源的主要镜像站

    Linux入职基础-1.1_国内开源的主要镜像站 东北地区: 东北大学  http://mirror.neu.edu.cn 大连理工大学  http://mirror.dlut.edu.cn 大连东软 ...

  7. 《图解HTTP》摘录

    # 图解HTTP 第 1 章 了解Web及网络基础 1.1使用http协议访问web 客户端:通过发送请求获取服务器资源的Web浏览器等. Web使用一种名为 HTTP(HyperText Trans ...

  8. 开源项目hutool之zip_slip漏洞

    今天突然看到了去年写的一篇漏洞分析文章,搬到博客上 ---------------- Hutool是Github上的一个开源项目,是一个java的工具包,对文件.流.加密解密.转码.正则.线程.XML ...

  9. S3C2440 gpio + main

    相关文章:http://blog.csdn.net/zhangxuechao_/article/details/77990854 举例 start.S .globl _start _start: /* ...

  10. 部署Java项目到阿里云服务器(Ubuntu16.04 64位)

    生成Jar包 1.进入到项目所在的路径下,打开cmd命令控制台,使用如下命令打包项目. mvn package --前提将项目中使用的maven配置到系统的环境变量中 2.打包完成的jar包在项目目录 ...