-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedListWeightSum.java
More file actions
30 lines (27 loc) · 944 Bytes
/
Copy pathNestedListWeightSum.java
File metadata and controls
30 lines (27 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.List;
/**
* 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)
*/
public class NestedListWeightSum {
public int depthSum(List<NestedInteger> nestedList) {
return depthSum(nestedList, 1);
}
private int depthSum(List<NestedInteger> nestedList, int depth) {
if (nestedList == null) {
return 0;
}
int sum = 0;
for (NestedInteger nestedInteger : nestedList) {
if (nestedInteger.isInteger()) {
sum += nestedInteger.getInteger() * depth;
} else {
sum += depthSum(nestedInteger.getList(), depth + 1);
}
}
return sum;
}
}