Nested List Weight Sum I & II
Nested List Weight Sum I
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)
From:
public int depthSum(List<NestedInteger> nestedList) {
return helper(nestedList, );
} public int helper(List<NestedInteger> nestedList, int depth) {
if (nestedList == null || nestedList.size() == )
return ; int sum = ;
for (NestedInteger ni : nestedList) {
if (ni.isInteger()) {
sum += ni.getInteger() * depth;
} else {
sum += helper(ni.getList(), depth + );
}
} return sum;
}
public int depthSum(List<NestedInteger> nestedList) {
int sum = ; Queue<NestedInteger> queue = new LinkedList<>();
Queue<Integer> depth = new LinkedList<>(); for (NestedInteger ni : nestedList) {
queue.offer(ni);
depth.offer();
} while (!queue.isEmpty()) {
NestedInteger top = queue.poll();
int dep = depth.poll(); if (top.isInteger()) {
sum += dep * top.getInteger();
} else {
for (NestedInteger ni : top.getList()) {
queue.offer(ni);
depth.offer(dep + );
}
}
} return sum;
}
Nested List Weight Sum II
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)
From: https://cyqz.wordpress.com/2016/06/23/leetcode-364-nested-list-weight-sum-ii/
/**
* // 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();
* }
*/
public class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if (nestedList == null || nestedList.size() == ) return ;
int h = helper(nestedList);
return getSum(nestedList, h);
} private int getSum(List<NestedInteger> list, int layer) {
int sum = ;
if (list == null || list.size() == ) return sum;
for (NestedInteger n : list) {
if (n.isInteger())
sum += n.getInteger() * layer;
else
sum += getSum(n.getList(), layer - );
}
return sum;
} private int helper(List<NestedInteger> list) {
if (list == null || list.size() == ) return ;
int max = ;
for (NestedInteger n : list) {
if (n.isInteger())
max = Math.max(max, );
else
max = Math.max(max, helper(n.getList()) + );
}
return max;
}
}
public class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if (nestedList == null) return ;
int[] height = new int[];
height(nestedList, height, );
return getSum(nestedList, height[]);
} private int getSum(List<NestedInteger> nestedList, int height) {
int sum = ;
for (NestedInteger ni : nestedList) {
if (ni.isInteger()) {
sum += ni.getInteger() * height;
} else {
sum += getSum(ni.getList(), height - );
}
}
return sum;
} private void height(List<NestedInteger> nestedList, int[] height, int currentHeight) {
if (nestedList == null) return;
for (NestedInteger ni : nestedList) {
if (ni.isInteger()) {
height[] = Math.max(height[], currentHeight);
} else {
height(ni.getList(), height, currentHeight + );
}
}
}
}
public int depthSumInverse(List<NestedInteger> nestedList) {
if (nestedList == null || nestedList.size() == )
return ; HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); // two stacks: one is for processing nested integer, the other is for tracking
// layers.
Stack<NestedInteger> stack = new Stack<>();
Stack<Integer> layers = new Stack<>(); // put all NestedIntegers to Stack and record its layer to be 1
for (NestedInteger ni : nestedList) {
stack.push(ni);
layers.push();
} int maxLayer = Integer.MIN_VALUE; while (!stack.isEmpty()) {
NestedInteger top = stack.pop();
int topLayer = layers.pop(); maxLayer = Math.max(maxLayer, topLayer); if (top.isInteger()) {
if (map.containsKey(topLayer)) {
map.get(topLayer).add(top.getInteger());
} else {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(top.getInteger());
map.put(topLayer, list);
}
} else {
for (NestedInteger ni : top.getList()) {
stack.push(ni);
layers.push(topLayer + );
}
}
} // calcualte sum
int result = ;
for (int i = maxLayer; i >= ; i--) {
if (map.get(i) != null) {
for (int v : map.get(i)) {
result += v * (maxLayer - i + );
}
}
}
return result;
}
Nested List Weight Sum I & II的更多相关文章
- [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]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
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- LeetCode Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 嵌套列表的加权和 · Nested List Weight Sum
[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 【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 ...
- [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. ...
随机推荐
- 网站性能工具Yslow的使用方法
Yslow是雅虎开发的基于网页性能分析浏览器插件,从年初我使用了YSlow后,改变了博客模板大量冗余代码,不仅提升了网页的打开速度,这款插件还帮助我分析了不少其他网站的代码,之前我还特意写了提高网站速 ...
- unicode和gbk的互相转换
unicode和gbk的互相转换主要依靠window下的escape和unescape方法,然后把%u替换成\u就好了; var GB2312UnicodeConverter = { ToUnicod ...
- Java基础-四要素之一《封装》
封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可能地隐藏内部的细节,只保 ...
- Java sun的JDK
JDK概述 JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK(Softwa ...
- jQuery技术交流资料
jQuery技术交流资料https://www.zybuluo.com/jikeytang/note/65371
- Oracle之自定义函数
数据库中函数包含四个部分:声明.返回值.函数体和异常处理. --没有参数的函数 create or replace function get_user return varchar2 is v_use ...
- 【poj2079】 Triangle
http://poj.org/problem?id=2079 (题目链接) 题意 求凸包内最大三角形面积 Solution 旋转卡壳. 只会n²的做法,但是竟然过了.就是枚举每一个点,然后旋转卡壳另外 ...
- Android视频直播解决方案(rstp、udp)
做局域网视频直播有两种方案,通过rstp或udp协议. 1.rstp协议,网络上有个开源项目,基于Android,且这个项目也是一个服务端,里面也集成了http访问页面,可以通过http或者rstp直 ...
- PHP Code Reviewing Learning
相关学习资料 http://code-tech.diandian.com/post/2012-11-04/40042129192 http://ssv.sebug.net/高级PHP应用程序漏洞审核技 ...
- hihocoder #1270 建造基地
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...