三刷339. Nested List Weight Sum

Easy
是L家tag題,又是DFS就比較想刷,然后算是bug free吧

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @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();
 *
 *     // @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();
 * }
 */
public class Solution {
    int sum = 0;
    public int depthSum(List<NestedInteger> nestedList) {
        if (nestedList == null || nestedList.size() == 0){
            return 0;
        }
        dfsHelper(nestedList, 1);
        return sum;
    }
    
    private void dfsHelper(List<NestedInteger> niList, int level){
        for (NestedInteger ni : niList){
            if (ni.isInteger()){
                sum += ni.getInteger() * level;
            } else {
                dfsHelper(ni.getList(), level + 1);
            }
        }                                
    }
}

看看提交記錄,最近狀態(tài)是比較好,進(jìn)步也是實(shí)實(shí)在在的.


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,585評(píng)論 25 708
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,779評(píng)論 0 33
  • 在Android開(kāi)發(fā)中少不了和日志打交道,而在大型項(xiàng)目中都是分模塊的,大公司基本上都是一個(gè)人負(fù)責(zé)一個(gè)模塊,可能還有...
    秋水旗飛閱讀 3,907評(píng)論 0 24
  • 女主角①: 姓名:冰琳雪夢(mèng) 原名:日奈森亞夢(mèng) 性格:(陷害前
    冰琳末羽閱讀 407評(píng)論 0 0
  • 文/蒹葭 一失足成千古恨…… 如果有來(lái)生,老王絕對(duì)會(huì)戒酒。 老王好酒,打小就喝酒。他經(jīng)常說(shuō)自己是“當(dāng)代酒仙。” 0...
    一只蒹葭閱讀 1,503評(píng)論 44 51