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. ...
随机推荐
- 日志框架对比 NLog VS Log4net
Log4net 先说Log4net,它是.net平台上一个老牌的日志框架,我接触的时间也不长(因为公司有自己的日志库),但是看着各开源库都在用这个于是前段时间也尝试去了解了一下. 首先让我认识到Log ...
- python 元祖(tuple)
元祖和列表几乎相同,但是元祖一旦初始化是不可变更内容的 元祖的表示方式是caassmates=(), 要记住所有列表能用的.元祖都能用,但是就是不能变内容 注:记住,在python中的元祖,为了引起不 ...
- Nginx 实现AJAX跨域请求
在工作中遇到跨域请求的问题: AJAX从一个域请求另一个域会有跨域的问题.那么如何在nginx上实现ajax跨域请求呢?要在nginx上启用跨域请求,需要添加add_header Access-Con ...
- WordPress 插件机制的简单用法和原理(Hook 钩子)
WordPress 的插件机制实际上只的就是这个 Hook 了,它中文被翻译成钩子,允许你参与 WordPress 核心的运行,是一个非常棒的东西,下面我们来详细了解一下它. PS:本文只是简单的总结 ...
- Bzoj2705 Longge的问题
Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Description Longge的数学 ...
- Linux Rootkit Sample && Rootkit Defenser Analysis
目录 . 引言 . LRK5 Rootkit . knark Rootkit . Suckit(super user control kit) . adore-ng . WNPS . Sample R ...
- (转)google Java编程风格中文版
转:http://www.hawstein.com/posts/google-java-style.html 目录 前言 源文件基础 源文件结构 格式 命名约定 编程实践 Javadoc 后记 前言 ...
- jdk版本
windows: set java_home:查看JDK安装路径 java -version:查看JDK版本 linux: whereis java which java (java执行路径) ech ...
- iOS项目工作空间搭建
一般公司的项目都是一个工作空间包包含两个项目,一个主项目,一个Pods项目,当然也有些就是一个项目,然后把第三方放在项目的文件夹里. 这样做的好处是,项目再次拷贝到其他地方报错的可能性小,而且拷完就能 ...
- pom.xml
使用intelJ idea 导入maven包管理文件是,使用Import的方式导入,会自动导入pom.xml来导入包. pom.xml会指定父子关系. 例如,总模块的pom.xml中有一下内容: &l ...