LeetCode 364. Nested List Weight Sum II
原题链接在这里: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]};
}
}
LeetCode 364. Nested List Weight Sum II的更多相关文章
- [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. ...
- [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. ...
- [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. ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://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 ...
- 364. Nested List Weight Sum II
这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- [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. ...
- 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. ...
随机推荐
- Java开发笔记(一百一十七)AWT窗口
前面介绍的所有Java代码,都只能通过日志观察运行情况,就算编译成class文件,也必须在命令行下面运行,这样的程序无疑只能给开发者做调试用,不能拿给一般人使用.因为普通用户早已习惯在窗口界面上操作, ...
- linux 创建虚拟机常见错误
无法打开内核设备global vmx86 重启虚拟机所有服务 无法创建虚拟机 需要使用管理员身份运行vm即可
- TextField 、 FTE、 TLF 的使用情景和简单说明
作者:tiangej 来源:CSDN 原文:https://blog.csdn.net/tiangej/article/details/16859239 版权声明:本文为博主原创文章,转载请附上博文链 ...
- SpringBoot项目jar启动端口设置
SpringBoot项目打包后,在target下生成的jar文件可以使用 Java - jar 直接启动,指定端口号配置 java -jar epjs-eureka.jar --server.port ...
- OI数学汇总
最前面:\(\LaTeX\)可能需要加载一会,请耐心等待o~ 前言 数学在\(\text{OI}\)中十分重要.其中大多都是数论. 什么是数论? \[ 研究整数的理论 --zzq \] 本文包含所有侧 ...
- Centos 7 替换镜像源
1 备份原始源 [root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.b ...
- GitHub Action一键部署配置,值得拥有
最近由于自己的个人应用增加,每次都需要在服务器手动发布,觉得特别麻烦,所以想通过代码控制自动发布,直接选择了GitHub Action. GitHub Action持续集成服务,目前已经免费开放使用, ...
- 7、注解@Mapper、@MapperScan
7.注解@Mapper.@MapperScan 2018年09月20日 11:12:41 飞奔的加瓦 阅读数 3284 版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载. https ...
- 返璞归真——OO第四单元总结暨学期总结
本次作业是第四单元的最后一次作业,也是本学期面向对象的最后一次作业,在此我将分别对第四单元和整个学期进行总结. 一.本单元的两次作业 第四单元的作业是关于UML的一些处理.UML语言是一种区别于具体语 ...
- JAVA相关知识
1.CopyOnWrite (1).在写操作的线程,会将数组复制出来一份进行操作.而原本的数组不会做改变. (2)读线程则不会受到影响,但是可能读到的是一个过期的数据. 在juc(java.util. ...