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的更多相关文章

  1. [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. ...

  2. [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. ...

  3. [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. ...

  4. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  5. LeetCode Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  6. 嵌套列表的加权和 · Nested List Weight Sum

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

  7. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  8. 【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 ...

  9. [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. ...

随机推荐

  1. oracle 分区表

    分区表用途 分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明.Oracle的分区表可以包括多个分区,每个分区都是一个独立的段(SEGMENT),可以存放到不同的表空间 ...

  2. HP 电脑装 纯净版的win7

    新买的 HP 电脑,自带 Win10 的操作系统,今天把它改成 装win7 系统 在安装的过程中遇到的问题 1.数字证书错误.您安装的操作系统来源不明之类的错误,具体没有记下来 2.磁盘的格式不是NT ...

  3. 图解Android - Android GUI 系统 (1) - 概论

    Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...

  4. ztree点击文字勾选checkbox,radio实现方法

    ztree的复选框checkbok,单选框radio是用背景图片来模拟的,所以点击文字即使用label括起checkbox,radio文字一起,点击文字也是无法勾选checkbox. 要想点击ztre ...

  5. Java编程思想学习(十六) 并发编程

    线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...

  6. Ubuntu安装VMware Tools的方法

    最后我将提供一个12版本的VMware Tools集合,包括了linux.iso. 背景: VMware Tools是VMware虚拟机中自带的一种增强工具,相当于VirtualBox中的增强功能(S ...

  7. BZOJ3172 后缀数组

    题意:求出一篇文章中每个单词的出现次数 对样例的解释: 原文是这样的: a aa aaa 注意每个单词后都会换行 所以a出现次数为6,aa为3 (aa中一次,aaa中两次),aaa为1 标准解法好像是 ...

  8. SPOJ Play on Words

    传送门 WORDS1 - Play on Words #graph-theory #euler-circuit Some of the secret doors contain a very inte ...

  9. idea修改运行内存

    安装目录下的bin 找到idea.exe.vmoptions 最大的修改下-Xmx1024m 找到idea64.exe.vmoptions 最大的修改下-Xmx1024m

  10. 深入了解A*

    一.前言 在这里我将对A*算法的实际应用进行一定的探讨,并且举一个有关A*算法在最短路径搜索的例子.值得注意的是这里并不对A*的基本的概念作介绍,如果你还对A*算法不清楚的话,请看姊妹篇<初识A ...