亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

364. Nested List Weight SumII

xeblog / 2741人閱讀

摘要:題目解答這一題其實(shí)挺的,如果說(shuō)第一道題的關(guān)鍵是記錄層次,那么這一題的關(guān)鍵是把這一層的傳到下一層去,代碼如下關(guān)鍵點(diǎn)在于把上一層的傳到下一層去,這樣的話,接下來(lái)還有幾層,每一層都會(huì)加上這個(gè)也就等于乘以了它的層數(shù)

題目:
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.

Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.

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

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

解答:
這一題其實(shí)挺tricky的,如果說(shuō)第一道題的關(guān)鍵是記錄層次,那么這一題的關(guān)鍵是把這一層的integer sum傳到下一層去,代碼如下:

public int DFS(List nestedList, int intSum) {
    //關(guān)鍵點(diǎn)在于把上一層的integer sum傳到下一層去,這樣的話,接下來(lái)還有幾層,每一層都會(huì)加上這個(gè)integer sum,也就等于乘以了它的層數(shù)
    List nextLevel = new ArrayList<>();
    int listSum = 0;
    for (NestedInteger list : nestedList) {
        if (list.isInteger()) {
            intSum += list.getInteger();
        } else {
            nextLevel.addAll(list.getList());
        }
    }
    listSum = nextLevel.isEmpty() ? 0 : DFS(nextLevel, intSum);
    return listSum + intSum;
}

public int depthSumInverse(List nestedList) {
    return DFS(nestedList, 0);
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64916.html

相關(guān)文章

  • [LeetCode] 339. Nested List Weight Sum

    Problem 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 othe...

    騫諱護(hù) 評(píng)論0 收藏0
  • [Leetcode] Two Sum, 3Sum,4Sum,4SumII,3Sum Closet

    摘要:解題思路題目要求兩個(gè)數(shù)和等于,返回其題目說(shuō)明不會(huì)有重復(fù)情況,所以我們一旦發(fā)現(xiàn)符合情況的,就可以直接結(jié)束循環(huán)并返回。特殊情況就是正好等于,那肯定是最接近的情況,直接返回即可。 Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific ta...

    EddieChan 評(píng)論0 收藏0
  • 【劉杰良】使用RPC接口新建EOS賬戶 - 實(shí)戰(zhàn)

    摘要:適用于最新的前言最近在研究的,但是由于官方文檔的不夠詳盡,新建賬號(hào)這一個(gè)操作就折騰了一個(gè)多星期?;侍觳回?fù)有心人,終于調(diào)通了新建賬號(hào),代幣轉(zhuǎn)賬也輕松解決。 適用于最新的 EOS Dawn 4.0/4.1 前言 最近在研究 EOS 的 RPC API,但是由于官方API文檔的不夠詳盡,新建賬號(hào)(new account)這一個(gè)操作就折騰了一個(gè)多星期。皇天不負(fù)有心人,終于調(diào)通了新建賬號(hào),代幣轉(zhuǎn)...

    Little_XM 評(píng)論0 收藏0
  • Sass 學(xué)習(xí)筆記

    摘要:有利于版權(quán)等關(guān)鍵信息的保留。變量后加上則變?yōu)槿肿兞?。字符串運(yùn)算符根據(jù)左邊的字符判斷最終結(jié)構(gòu)是否有引號(hào)。若使用,則兩個(gè)類必須同時(shí)使用,增加維護(hù)負(fù)擔(dān)。一組重用的使用引入,可攜帶參數(shù)。 1. 什么是Sass css預(yù)處理器,幫助你書(shū)寫(xiě)更簡(jiǎn)單、可維持的css。 2. Sass的特征 變量(variable)幫助你存儲(chǔ)需要重復(fù)使用的值; 嵌套(nesting)讓你書(shū)寫(xiě)更少的選擇器; par...

    lingdududu 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<