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. ...
随机推荐
- java在目录中过滤文件
package com.zh.test; import java.io.File; import java.io.FilenameFilter; public class Test2 { /** * ...
- java ee 中文乱码的问题
java ee 中文乱码的问题 发生中文乱码的三种情况 (一) 表单form Post 方法 直接在服务器中设置 request.setCharacterEncoding("utf-8&qu ...
- javascript this 详解
前言 Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象.Javascript可以通过一定的设计模式来实现面向对象的编程,其中this ...
- 从svn服务器自动同步到另一台服务器
需求场景 A commit B post-commit C (workstation) --------------> (svn server) ---------------------> ...
- 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
- Weka算法Classifier-tree-J48源代码分析(一个)基本数据结构和算法
大约一年,我没有照顾的博客,再次拿起笔不知从何写上,想来想去手从最近使用Weka要正确书写. Weka为一个Java基础上的机器学习工具.上手简单,并提供图形化界面.提供如分类.聚类.频繁项挖掘等工具 ...
- NSThread - (void)start vs java Thread implements Runnable
This method spawns the new thread and invokes the receiver’s main method on the new thread. If you i ...
- mq安装参考
CentOS 6.2 64bit 安装erlang及RabbitMQ Server 1.操作系统环境(CentOS 6.2 64bit) [root@leekwen ~]# cat /etc/issu ...
- dedecms首页调用栏目内容和单页内容的方法
常用的需要调到首页来的单页内容,比如企业简介.联系我们等等内容,我们在首页可能都要进行体现.通过常规的方式,包括查阅dede官方论坛资料,都找不到比较合适的答案.今天我们就提供两种方式进行调用. 我们 ...
- Lua函数之一
LUA函数之一 函数声明: function foo(arguments) statements end 1.函数调用 调用函数的时候,如果参数列表为空,必须使用()表明是函数调用,例如: os.da ...