forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedListWeightSum.java
More file actions
25 lines (20 loc) · 590 Bytes
/
NestedListWeightSum.java
File metadata and controls
25 lines (20 loc) · 590 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
import java.util.List;
/**
* https://leetcode.com/articles/nested-list-weight-sum/
*/
public class NestedListWeightSum {
public int depthSum(List<NestedInteger> nestedList) {
return depthSum(nestedList, 1);
}
private int depthSum(List<NestedInteger> nestedList, int depth) {
int sum = 0;
for (NestedInteger nest : nestedList) {
if (nest.isInteger()) {
sum += depth * nest.getInteger();
} else {
sum += depthSum(nest.getList(), depth + 1);
}
}
return sum;
}
}