• LeetCode 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 + 4*2 + 6*3 = 27)


    题目标签:Depth First Search

      这道题目给了我们一个嵌套的list,在这个list里,每一个element可以是一个integer,又可以是一个list,这个list里还可以继续有list,可以无限套。所以要用到depth first search。如果能走到最里面一层呢,要利用recursive function,一层一层递归下去直到它是一个integer了,就可以返回了。所以需要另外一个function getSum。首先iterate nestedList, 把每一个element 加起来。 为了得到这个element的值,我们要把它代入getSum function, 如果这个element 是integer 直接return。 如果这个element 是一个list,那么利用相同的方法,设一个sum 把它每一个element 加起来,为了得到每一个element,把每一个element代入getSum,记得这里要把depth + 1。因为我们代入了下一层depth。

    Java Solution:

    Runtime beats 6.27% 

    完成日期:07/09/2017

    关键词:Depth First Search

    关键点:利用递归function来实现depth first search

     1 /**
     2  * // This is the interface that allows for creating nested lists.
     3  * // You should not implement it, or speculate about its implementation
     4  * public interface NestedInteger {
     5  *
     6  *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
     7  *     public boolean isInteger();
     8  *
     9  *     // @return the single integer that this NestedInteger holds, if it holds a single integer
    10  *     // Return null if this NestedInteger holds a nested list
    11  *     public Integer getInteger();
    12  *
    13  *     // @return the nested list that this NestedInteger holds, if it holds a nested list
    14  *     // Return null if this NestedInteger holds a single integer
    15  *     public List<NestedInteger> getList();
    16  * }
    17  */
    18 public class Solution 
    19 {
    20     public int depthSum(List<NestedInteger> nestedList) 
    21     {
    22         int res = 0;
    23         
    24         // iterate list
    25         for(int i=0; i<nestedList.size(); i++)
    26             res += getSum(nestedList.get(i), 1);
    27         
    28         return res;
    29     }
    30     
    31     public int getSum(NestedInteger ele, int depth)
    32     {
    33         int sum = 0;
    34         
    35         // if ele is integer, return its value * depth;
    36         if(ele.isInteger())
    37             return depth * ele.getInteger();
    38         
    39         // if ele is a list, iterate list recursively call function;
    40         for(int i=0; i<ele.getList().size(); i++)
    41             sum += getSum(ele.getList().get(i), depth + 1);
    42         
    43         
    44         return sum;
    45     }
    46 }

    参考资料:

    http://www.cnblogs.com/grandyang/p/5340305.html

    LeetCode 算法题目列表 - LeetCode Algorithms Questions List

  • 相关阅读:
    BZOJ 3677 连珠线
    BZOJ 3676 回文串
    BZOJ 3675 序列分割
    BZOJ 4013 实验比较
    BZOJ 4011 落忆枫音
    使用Google BBR加速 VPS
    OSX编译安装Python3及虚拟开发环境Virtualenv
    OSX安装Mysql8.0
    OpenSSL编程之摘要
    OpenCA搭建
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7143871.html
Copyright © 2020-2023  润新知