原题链接在这里: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. Python10之列表1(创建列表和列表添加元素的方法)

    一.创建列表 列表名 = [’元素一‘,’元素二‘,’元素三‘,’元素四‘] 列表中的元素可以是任何类型,甚至可以是一个列表. list1 = ['詹姆斯','戴维斯','保罗乔治','字母哥',in ...

  2. 剑指offer59:按之字形顺序打印二叉树:[[1], [3,2], [4,5,6,7]]

    1 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 2 思路和方法 先给定一个二叉树的样式: ...

  3. javascript加超链接

    JavaScript link 方法:给字符串加上超链接JavaScript link 方法link 方法返回使用 HTML a 标签属性定义的(斜体)字符串.其语法如下:str_object.lin ...

  4. python 之 并发编程(生产者消费者模型、守护进程的应用)

    9.8 生产者消费者模型 该模型中包含两类重要的角色: 1.生产者:将负责造数据的任务比喻为生产者 2.消费者:接收生产者造出的数据来做进一步的处理的被比喻成消费者 实现生产者消费者模型三要素:1.生 ...

  5. Oracle 11g xe版本---总结1

    一.创建用户和授予权限 1.1 环境: Oracle 11g xe 第三方图形客户端: PLSQL Windows 10 必须登录 HR 用户,下面的查询会使用到 HR 中的表. 1.2 SQL 语句 ...

  6. Scratch编程:游来游去的鱼(二)

    “ Scratch编程学习环境搭建好了吗?让我们一起来进行游戏吧!” 01 — 游戏介绍 这是一款简单的小游戏,实现了一条小鱼在池塘里游来游去. 02 — 设计思路 1,这个游戏主要由一个池塘背景和一 ...

  7. nginx Proxy Cache 配置

    总结一下 proxy cache 设置的常用指令及使用方法: proxy_cache proxy_cache zone | off 配置一块公用的内存区域的名称,该区域可以存放缓存的索引数据.注意:z ...

  8. ES6中 对字符串增强

    在曾经,我们只能用A.indexof(B)来判断A中是否含有B字符串: 现在在ES6中 有了: includes(), startswith(),endswith() reapt()重复次数: 输出 ...

  9. android 动画总结一

    一.补间动画 补间动画就是指开发者指定动画的开始.动画的结束的"关键帧",而动画变化的"中间帧"由系统计算,并补齐. 补间动画分为四种:平移动画(Transla ...

  10. python网络爬虫之爬取图片

    今天使用requests和BeautifulSoup爬取了一些图片,还是很有成就感的,注释可能有误,希望大家多提意见: 方法一:requests import requests from bs4 im ...