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)

這道題要用草稿紙拿例子演示幾次,才能發(fā)現(xiàn)level應(yīng)該在遍歷到整數(shù)的時(shí)候乘锈候,而在遍歷到list的時(shí)候不乘。并且在遍歷到list的時(shí)候,調(diào)用遞歸函數(shù)要記得level + 1.

/**
 * // 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 level){
        int sum = 0;
        for (int i = 0; i < nestedList.size(); i++){
            if (nestedList.get(i).isInteger()){
                sum += level * nestedList.get(i).getInteger();
            } else {
                List<NestedInteger> list = nestedList.get(i).getList();
                sum += helper(list, level + 1);
            }
        }
        return sum;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖元旬,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異守问,居然都是意外死亡匀归,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門耗帕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)穆端,“玉大人,你說(shuō)我怎么就攤上這事仿便√鍐” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵嗽仪,是天一觀的道長(zhǎng)荒勇。 經(jīng)常有香客問(wèn)我,道長(zhǎng)闻坚,這世上最難降的妖魔是什么沽翔? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮窿凤,結(jié)果婚禮上仅偎,老公的妹妹穿的比我還像新娘。我一直安慰自己雳殊,他們只是感情好哨颂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著相种,像睡著了一般。 火紅的嫁衣襯著肌膚如雪品姓。 梳的紋絲不亂的頭發(fā)上寝并,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音腹备,去河邊找鬼衬潦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛植酥,可吹牛的內(nèi)容都是我干的镀岛。 我是一名探鬼主播弦牡,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼漂羊!你這毒婦竟也來(lái)了驾锰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤走越,失蹤者是張志新(化名)和其女友劉穎椭豫,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旨指,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡赏酥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了谆构。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片裸扶。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖搬素,靈堂內(nèi)的尸體忽然破棺而出呵晨,到底是詐尸還是另有隱情,我是刑警寧澤蔗蹋,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布何荚,位于F島的核電站,受9級(jí)特大地震影響猪杭,放射性物質(zhì)發(fā)生泄漏餐塘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一皂吮、第九天 我趴在偏房一處隱蔽的房頂上張望戒傻。 院中可真熱鬧,春花似錦蜂筹、人聲如沸需纳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)不翩。三九已至,卻和暖如春麻裳,著一層夾襖步出監(jiān)牢的瞬間口蝠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工津坑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留妙蔗,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓疆瑰,卻偏偏與公主長(zhǎng)得像眉反,于是被迫代替她去往敵國(guó)和親昙啄。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

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