easy and clean java ac solution


  • 0
    Z
    public class Solution 
    {
        public int depthSum(List<NestedInteger> nestedList) 
        {
            if (nestedList == null || nestedList.size() == 0)
    	{
    	   return 0;
    	}
    		
    	return helper(nestedList, 1);
        }
    
        public int helper(List<NestedInteger> input, int weight)
        {
    	int res = 0;
    		
    	for (NestedInteger i : input)
    	{
    	   if (i.isInteger())
    	   {
    		res = res + i.getInteger() * weight;
    	   }
    	   else
    	   {
    		 res += helper(i.getList(), weight+1);
    	   }
    	}
    		
    	return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.