二刷364. Nested List Weight Sum II

Medium
大哥呀,你有些L家的tag题目虽然做过但是时间有点久远了呀,比如这道两个月前做的,做起来很生疏呀

这道题思路是对的,卡在了求最深深度上. 我看到人家这一段code就觉得字字如金

for (NestedInteger ni : nestedList){
            if (ni.isInteger()){
                max = Math.max(max, 1);
            } else {
                max = Math.max(max, getTotalDepth(ni.getList()) + 1);
            }
}

求个最深深度,我知道只保留最大的, 但就是没想到怎么保留,妈蛋调用一个Math.max保留大的;需要继续call recursion的List记得比较的时候要加一。因为继续call的话就是往[]里面继续找[],比如[[]],深度要increment by 1 each time call recursive method.

接下来的部分就Nested List Weight Sum很像了,只是深度参数从最大深度开始,每次调用recursion时那个深度参数递减.

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *     // Constructor initializes an empty nested list.
 *     public NestedInteger();
 *
 *     // Constructor initializes a single integer.
 *     public NestedInteger(int value);
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // Set this NestedInteger to hold a single integer.
 *     public void setInteger(int value);
 *
 *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
 *     public void add(NestedInteger ni);
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */

class Solution {
    public int depthSumInverse(List<NestedInteger> nestedList) {
        if (nestedList == null || nestedList.size() == 0){
            return 0;
        }
        int totalDepth = getTotalDepth(nestedList);
        return getSumHelper(nestedList, totalDepth);
    }
    
    private int getSumHelper(List<NestedInteger> nestedList, int curt){
        if (nestedList == null || nestedList.size() == 0){
            return 0;
        }
        int sum = 0;   
        for (NestedInteger ni : nestedList){
            if (ni.isInteger()){
                sum += ni.getInteger() * curt;
            } else {
                sum += getSumHelper(ni.getList(), curt - 1);
            }
        }
        return sum;        
    }
    
    
    private int getTotalDepth(List<NestedInteger> nestedList){
        if (nestedList == null || nestedList.size() == 0){
            return 0;
        }
        int max = 0;
        for (NestedInteger ni : nestedList){
            if (ni.isInteger()){
                max = Math.max(max, 1);
            } else {
                max = Math.max(max, getTotalDepth(ni.getList()) + 1);
            }
        }
        return max;
    }

}
    原文作者:greatfulltime
    原文地址: https://www.jianshu.com/p/86507bf652ac
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞