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

题目:

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.

Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.

Example 1:
Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2)

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

题解:

The question is asking for weight sum. The top level has largest depth.

Thus the top level need to be added maximum depth times.

We could accumlate the sum of Integer of the current level, add it to the res.

For next level, we could continue adding Integer to the same sum, add it to the res. Then upper level sum is added multiple times.

Time Complexity: O(n+m). n 是flat后一共有多少个数字, 就像BFS的node数. m 是层数,就像BFS的edge数.

Space: O(n).

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 {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @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();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @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();
* }
*/
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
int weight = 0;
int unweight = 0;
while(!nestedList.isEmpty()){
List<NestedInteger> nextList = new ArrayList<NestedInteger>();
for(NestedInteger item : nestedList){
if(item.isInteger()){
unweight += item.getInteger();
}else{
nextList.addAll(item.getList());
}
}
weight += unweight;
nestedList = nextList;
}
return weight;
}
}

Could also do DFS.

DFS return value needs both maximum depth and accumlated sum.

It does DFS bottom-up.

Time Complexity: O(n+m).

Space: O(n).

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 {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @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();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @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();
* }
*/
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if(nestedList == null || nestedList.size() == 0){
return 0;
} int [] res = dfs(nestedList);
return res[1];
} private int [] dfs(List<NestedInteger> nestedList){
if(nestedList == null || nestedList.size() == 0){
return new int[]{0, 0};
} int levelSum = 0;
List<NestedInteger> nexts = new ArrayList<>();
for(NestedInteger ni : nestedList){
if(ni.isInteger()){
levelSum += ni.getInteger();
}else{
nexts.addAll(ni.getList());
}
} int [] arr = dfs(nexts);
return new int[]{arr[0]+1, levelSum*(arr[0]+1)+arr[1]};
}
}

类似Nested List Weight Sum.

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

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

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

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

  4. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

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

  5. 364. Nested List Weight Sum II 大小反向的括号加权求和

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

  6. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...

  7. LeetCode 339. Nested List Weight Sum

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

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

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

  9. Leetcode: Nested List Weight Sum II

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

随机推荐

  1. Win32API文本处理

    工程模板:https://www.cnblogs.com/eternalmoonbeam/p/10793080.html 安全的文本输出方式: 需要额外包含头文件strsafe.h 依次使用以下三个函 ...

  2. PHP提取中英文首字母的方法(首字母索引)

    function Getzimu($str) { $str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的,此行就要注释掉 ...

  3. Wing电信平台操作方法

    Wing电信平台操作文档 当前文档编制于2019/9/3 一.登陆 登陆网址 https://www.ctwing.cn/ 点击右上角控制台 点击左侧栏点击产品中心 选择需要注册的产品 二.注册设备 ...

  4. 【LEETCODE】46、999. Available Captures for Rook

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  5. Charles4.2.8抓包(http+https)

    Charles 和 Fiddler 一样都是http抓包工具. 之前用 Fiddler 抓个别 ios 手机 https 报文时总卡在哪里返不回任何数据,后来怀疑是 Fiddler 问题,就考虑使用  ...

  6. NGINX 配置本地HTTPS(双向认证)

    一.SSL协议加密方式 SSL协议即用到了对称加密也用到了非对称加密(公钥加密),在建立传输链路时,SSL首先对对称加密的密钥使用公钥进行非对称加密,链路建立好之后,SSL对传输内容使用对称加密. 1 ...

  7. 用pyinstaller打包一个exe程序

    打包单个exe文件 -F 选项可以打出一个exe文件,默认是 -D,意思是打成一个文件夹. pyinstaller -F TestDataGen.py 打出的桌面程序去掉命令行黑框 -w 选项可以打桌 ...

  8. c# webapi 过滤器token、sign认证、访问日志

    1.token认证 服务端登录成功后分配token字符串.记录缓存服务器,可设置有效期 var token = Guid.NewGuid().ToString().Replace("-&qu ...

  9. numpy模块之axis(转)

    转自:https://blog.csdn.net/fangjian1204/article/details/53055219

  10. postman 测试Api接口注意事项

    1.简单数据传输 2.对象传输 使用的是post方式请求 在Headers设置: 在Body写入对象信息,主要红线的地方:1.raw选中 2.j'son格式 form表单提交数据测试 在header里 ...