/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/**
* Given a nested list of integers, returns the sum of all integers in the list weighted by their depth
* For example, given the list {{1,1},2,{1,1}} the function should return 10 (four 1's at depth 2, one 2 at depth 1)
* Given the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3)
*/
class Ideone
{
public static void main
(String[] args
) {
// {{1,1},2,{1,1}}
List<Object> parent1 = new ArrayList<Object>();
List<Object> child1 = new ArrayList<Object>();
child1.add(1);
child1.add(1);
parent1.add(child1);
parent1.add(2);
List<Object> child3 = new ArrayList<Object>();
child3.add(1);
child3.add(1);
parent1.add(child3);
System.
out.
println(getSum
(parent1,
1));
// {1,{4,{6}}}
List<Object> parent2 = new ArrayList<Object>();
parent2.add(1);
List<Object> child11 = new ArrayList<Object>();
child11.add(4);
List<Object> child111 = new ArrayList<Object>();
child111.add(6);
child11.add(child111);
parent2.add(child11);
System.
out.
println(getSum
(parent2,
1)); }
private static int getSum
(Object list,
int depth
){ if(list == null) return 0;
int sum = 0;
for(Object nestedList
:(ArrayList
<Object
>)list
){ if(nestedList.
getClass() == ArrayList.
class){ sum += getSum(nestedList, depth+1);
}else{
sum += getSum(nestedList, depth);
}
}
}else{
System.
out.
println("CurrentSum => " + sum
+ " integer => " + list
+ " Depth => " + depth
); }
return sum;
}
/**
* This is the interface that allows for creating nested lists. You should not implement it, or speculate about its implementation
*/
public interface NestedInteger
{
// Returns true if this NestedInteger holds a single integer, rather than a nested list
public boolean isInteger();
// Returns the single integer that this NestedInteger holds, if it holds a single integer
// Returns null if this NestedInteger holds a nested list
// Returns the nested list that this NestedInteger holds, if it holds a nested list
// Returns null if this NestedInteger holds a single integer
public List<NestedInteger> getList();
}
}