We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4b1a6e6 commit 9bebaa6Copy full SHA for 9bebaa6
339. Nested List Weight Sum.java
@@ -16,19 +16,21 @@
16
* }
17
*/
18
public class Solution {
19
- public int helper(List<NestedInteger> nl, int depth) {
+ public int getSum(List<NestedInteger> nestedList, int depth) {
20
int sum = 0;
21
- for (int i = 0; i < nl.size(); i++) {
22
- if (nl.get(i).isInteger()) {
23
- sum += nl.get(i).getInteger() * depth;
+
+ for (NestedInteger i : nestedList) {
+ if (i.isInteger()) {
24
+ sum += depth * i.getInteger();
25
} else {
- sum += helper(nl.get(i).getList(), depth+1);
26
+ sum += getSum(i.getList(), depth+1);
27
}
28
29
30
return sum;
31
32
33
public int depthSum(List<NestedInteger> nestedList) {
- return helper(nestedList, 1);
34
+ return getSum(nestedList, 1);
35
36
0 commit comments