首页 > 其他 > 详细

Nested List Weight Sum I & II

时间:2016-08-01 06:52:37      阅读:250      评论:0      收藏:0      [点我收藏+]

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: 

 1     public int depthSum(List<NestedInteger> nestedList) {
 2         return helper(nestedList, 1);
 3     }
 4 
 5     public int helper(List<NestedInteger> nestedList, int depth) {
 6         if (nestedList == null || nestedList.size() == 0)
 7             return 0;
 8 
 9         int sum = 0;
10         for (NestedInteger ni : nestedList) {
11             if (ni.isInteger()) {
12                 sum += ni.getInteger() * depth;
13             } else {
14                 sum += helper(ni.getList(), depth + 1);
15             }
16         }
17 
18         return sum;
19     }

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/

 1 public class Solution {
 2 
 3     public int depthSumInverse(List<NestedInteger> nestedList) {
 4         if (nestedList == null || nestedList.size() == 0) return 0;
 5         int h = helper(nestedList);
 6         int res = getSum(nestedList, h);
 7         return res;
 8     }
 9 
10     private int getSum(List<NestedInteger> l, int layer) {
11         int sum = 0;
12         if (l == null || l.size() == 0) return sum;
13         for (NestedInteger n : l) {
14             if (n.isInteger())
15                 sum += n.getInteger() * layer;
16             else
17                 sum += getSum(n.getList(), layer - 1);
18         }
19         return sum;
20     }
21 
22     private int helper(List<NestedInteger> l) {
23         if (l == null || l.size() == 0) return 0;
24         int max = 0;
25         for (NestedInteger n : l) {
26             if (n.isInteger())
27                 max = Math.max(max, 1);
28             else
29                 max = Math.max(max, helper(n.getList()) + 1);
30         }
31         return max;
32     }
33 }

 

Nested List Weight Sum I & II

原文:http://www.cnblogs.com/beiyeqingteng/p/5724476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!