339. Nested List Weight Sum

Given a nested list of integers, return the sum of all integers in the list weighted by their depth.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Example 1:
Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)

Example 2:
Given the list [1,[4,[6]]], return 27. (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 42 + 63 = 27)

一刷:
題解荞估,用recursion求解咳促,子函數(shù)內(nèi)帶入自己的depth

/**
 * // 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 {
    public int depthSum(List<NestedInteger> nestedList) {
        int depth = 1;
        int res = 0;
        for(NestedInteger nested : nestedList){
            res += count(nested, depth);
        }
        return res;
    }
    
    private int count(NestedInteger nested, int depth){
        if(nested.isInteger()) return nested.getInteger()*depth;
        
        List<NestedInteger> lists = nested.getList();
        depth++;
        int res = 0;
        for(NestedInteger list : lists){
            res += count(list, depth);
        }
        return res;
    }
}

二刷
recursion

/**
 * // 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 {
    public int depthSum(List<NestedInteger> nestedList) {
        return helper(nestedList, 1);
    }
    
    private int helper(List<NestedInteger> nestedList, int depth){
        int res = 0;
        for(NestedInteger nest : nestedList){
            if(nest.isInteger()) res += nest.getInteger()*depth;
            else{
                List<NestedInteger> list = nest.getList();
                res += helper(list, depth+1);
            }
        }
        return res;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末稚新,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子跪腹,更是在濱河造成了極大的恐慌褂删,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件尺迂,死亡現(xiàn)場(chǎng)離奇詭異笤妙,居然都是意外死亡冒掌,警方通過(guò)查閱死者的電腦和手機(jī)噪裕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)股毫,“玉大人膳音,你說(shuō)我怎么就攤上這事×逦埽” “怎么了祭陷?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)趣席。 經(jīng)常有香客問(wèn)我兵志,道長(zhǎng),這世上最難降的妖魔是什么宣肚? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任想罕,我火速辦了婚禮,結(jié)果婚禮上霉涨,老公的妹妹穿的比我還像新娘按价。我一直安慰自己,他們只是感情好笙瑟,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布楼镐。 她就那樣靜靜地躺著,像睡著了一般往枷。 火紅的嫁衣襯著肌膚如雪框产。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,696評(píng)論 1 312
  • 那天错洁,我揣著相機(jī)與錄音秉宿,去河邊找鬼。 笑死墓臭,一個(gè)胖子當(dāng)著我的面吹牛蘸鲸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播窿锉,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼酌摇,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼膝舅!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起窑多,我...
    開(kāi)封第一講書(shū)人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤仍稀,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后埂息,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體技潘,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年千康,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了享幽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拾弃,死狀恐怖值桩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情豪椿,我是刑警寧澤奔坟,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站搭盾,受9級(jí)特大地震影響咳秉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鸯隅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一澜建、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滋迈,春花似錦霎奢、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至碍彭,卻和暖如春晤硕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背庇忌。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工舞箍, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人皆疹。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓疏橄,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子捎迫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

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

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)晃酒。 張土汪:刷leetcod...
    土汪閱讀 12,748評(píng)論 0 33
  • Given a nested list of integers, return the sum of all in...
    我是你的果果呀閱讀 173評(píng)論 0 0
  • LeetCode 339 Nested List Weight Sum =====================...
    ShuiLocked閱讀 501評(píng)論 0 0
  • 分析:【簡(jiǎn)單思路】設(shè)置一個(gè)depth變量,每遇一次左括號(hào)加1窄绒,遇一次右括號(hào)減1贝次。然后再設(shè)置一個(gè)sum自增變量。在加...
    keaidelele閱讀 499評(píng)論 0 49
  • 昨天冬冬哥和嫂子的小寶貝依依降生了彰导,求子的不易讓我覺(jué)得這個(gè)孩子會(huì)帶來(lái)更多更多的愛(ài)蛔翅。三姨從早上就緊張的吃不下飯,感...
    桃子tz閱讀 301評(píng)論 0 0