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. ...
随机推荐
- jQuery 选择器语法
jQuery选择器分为如下几类(点击“名称”会跳转到此方法的jQuery官方说明文档): 1. 基础选择器 Basics 名称 说明 举例 #id 根据元素Id选择 $("divId&quo ...
- HashMap 和 HashTable区别
HashMap 非线程安全的 HashTable线程安全的 package Collections.Map; import java.util.HashMap; public class HashMa ...
- Java基础-ArrayList和LinkedList的区别
大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为Lin ...
- JS Jquery去除数组重复元素
js jquery去除数组中的重复元素 第一种:$.unique() 第二种: for(var i = 0,len = totalArray_line.length;i < len;i++) { ...
- STL简单应用问题
问题: Input输入的第一行是一个整数T( 1 <= T <= 100 ),表示有几组输入数据.每组输入由4部分组成:(1)一个字典,最多包含2000个单词,每个单词一行.(2)一行字符 ...
- BZOJ4196 软件包管理器
Description Linux用户和OSX用户一定对软件包管理器不会陌生. 通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖 ...
- 在网络7层协议中,如果想使用UDP协议达到TCP协议的效果,可以在哪层做文章?(QQ 为什么采用 UDP 协议,而不采用 TCP 协议实现?)
为了解决这题,可以具体看看下面这个讨论. 解灵运工程师 185 人赞同 某次架构师大会上那个58同城做即时通信的人说:原因是因为当时没有epoll这种可以支持成千上万tcp并发连接的技术,所以他们使用 ...
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之五 模块(菜单)管理源码分享
http://www.cnblogs.com/huyong/p/3454012.html 使用Jquery+EasyUI 进行框架项目开发案例讲解之五 模块(菜单)管理源码分享 在上四篇文章 ...
- nginx配置ssl双向验证 nginx https ssl证书配置
1.安装nginx 参考<nginx安装>:http://www.ttlsa.com/nginx/nginx-install-on-linux/ 如果你想在单IP/服务器上配置多个http ...
- 修改eclipse/MyEclipse中包的显示结构为树形
在右上边三角那里进去设置 选第一个是显示完整的包名,第二个显示的是树形结构,我们一般用第一种,如下图: