Nested List Weight Sum I & II

本文介绍了解决两个加权嵌套列表求和问题的方法:NestedListWeightSumI 和 NestedListWeightSumII。前者从根到叶节点深度递增权重,后者从叶节点到根节点深度递减权重。通过递归遍历嵌套列表并计算各元素值乘以其对应的权重,最终得到整个列表的加权和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 /**
 2  * // This is the interface that allows for creating nested lists.
 3  * // You should not implement it, or speculate about its implementation
 4  * public interface NestedInteger {
 5  *     // Constructor initializes an empty nested list.
 6  *     public NestedInteger();
 7  *
 8  *     // Constructor initializes a single integer.
 9  *     public NestedInteger(int value);
10  *
11  *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
12  *     public boolean isInteger();
13  *
14  *     // @return the single integer that this NestedInteger holds, if it holds a single integer
15  *     // Return null if this NestedInteger holds a nested list
16  *     public Integer getInteger();
17  *
18  *     // Set this NestedInteger to hold a single integer.
19  *     public void setInteger(int value);
20  *
21  *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
22  *     public void add(NestedInteger ni);
23  *
24  *     // @return the nested list that this NestedInteger holds, if it holds a nested list
25  *     // Return null if this NestedInteger holds a single integer
26  *     public List<NestedInteger> getList();
27  * }
28  */
29 public class Solution {
30     public int depthSumInverse(List<NestedInteger> nestedList) {
31         if (nestedList == null || nestedList.size() == 0) return 0;
32         int h = helper(nestedList);
33         return getSum(nestedList, h);
34     }
35 
36     private int getSum(List<NestedInteger> list, int layer) {
37         int sum = 0;
38         if (list == null || list.size() == 0) return sum;
39         for (NestedInteger n : list) {
40             if (n.isInteger())
41                 sum += n.getInteger() * layer;
42             else
43                 sum += getSum(n.getList(), layer - 1);
44         }
45         return sum;
46     }
47 
48     private int helper(List<NestedInteger> list) {
49         if (list == null || list.size() == 0) return 0;
50         int max = 0;
51         for (NestedInteger n : list) {
52             if (n.isInteger())
53                 max = Math.max(max, 1);
54             else
55                 max = Math.max(max, helper(n.getList()) + 1);
56         }
57         return max;
58     }
59 }

 

 1 public class Solution {
 2     public int depthSumInverse(List<NestedInteger> nestedList) {
 3         if (nestedList == null) return 0;
 4         int[] height = new int[1];
 5         height(nestedList, height, 1);
 6         return getSum(nestedList, height[0]);
 7     }
 8     
 9     private int getSum(List<NestedInteger> nestedList, int height) {
10         int sum = 0;
11         for (NestedInteger ni : nestedList) {
12             if (ni.isInteger()) {
13                 sum += ni.getInteger() * height;
14             } else {
15                 sum += getSum(ni.getList(), height - 1);
16             }
17         }
18         return sum;
19     }
20     
21     private void height(List<NestedInteger> nestedList, int[] height, int currentHeight) {
22         if (nestedList == null) return;
23         for (NestedInteger ni : nestedList) {
24             if (ni.isInteger()) {
25                 height[0] = Math.max(height[0], currentHeight);
26             } else {
27                 height(ni.getList(), height, currentHeight + 1);
28             }
29          }
30     }
31 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5724476.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值