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

 /**
* // 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 res = 0; // iterate list
for(int i=0; i<nestedList.size(); i++)
res += getSum(nestedList.get(i), 1); return res;
} public int getSum(NestedInteger ele, int depth)
{
int sum = 0; // if ele is integer, return its value * depth;
if(ele.isInteger())
return depth * ele.getInteger(); // if ele is a list, iterate list recursively call function;
for(int i=0; i<ele.getList().size(); i++)
sum += getSum(ele.getList().get(i), depth + 1); return sum;
}
}

参考资料:

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

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

LeetCode 339. Nested List Weight Sum (嵌套列表重和)$的更多相关文章

  1. [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. ...

  2. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  3. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

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

  4. 【leetcode】339. Nested List Weight Sum

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

  5. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

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

  6. [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二

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

  7. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  8. 【LeetCode】339. Nested List Weight Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...

  9. [LeetCode] Nested List Weight Sum 嵌套链表权重和

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

随机推荐

  1. hadoop-2.6.0源码编译问题汇总

    在上一篇文章中,介绍了hadoop-2.6.0源码编译的一般流程,因个人计算机环境的不同, 编译过程中难免会出现一些错误,下面是我编译过程中遇到的错误. 列举出来并附上我解决此错误的方法,希望对大家有 ...

  2. [04] Cookie概念和基本使用

    1.Cookie是什么 Cookie,中文名称为"小型文本文件"或"小甜饼",指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密). 很多网站 ...

  3. System.getProperty()参数大全

    System.getProperty()获取Java各种配置属性,参数如下: Java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vend ...

  4. Saltstack批量编译部署nginx(多模块)

    最近一直在研究saltstack的同步文件和批量执行命令,随着架构的变大,批量部署的需求也变得明显起来了,我需要用一条命令就部署好nginx和tomcat,并且符合我所有的环境需求,可以直接投入生产环 ...

  5. jmeter测试HTTP请求

    HTTP超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.(详情参考看一下百科) HTTP发送请求有GE ...

  6. js 各种循环的区别与用法(for in,forEach,for of)

    1,forEach循环   不能跳过或者终止循环 const a = ["a","ss","cc"] a.dd="11" ...

  7. AngularJS系列-翻译官网

    公司之前一直用的Web前台框架是Knockout,我们通常直接叫ko,有看过汤姆大叔的KO系列,也有在用,发现有时候用得不太顺手.本人是会WPF的,所以MVVM也是比较熟悉的,学ko也是很快就把汤姆大 ...

  8. vue学习之vue基本功能初探

    vue学习之vue基本功能初探: 采用简洁的模板语法将声明式的将数据渲染进 DOM: <div id="app"> {{ message }} </div> ...

  9. HDFS概述(1)————HDFS架构

    概述 Hadoop分布式文件系统(HDFS)是一种分布式文件系统,用于在普通商用硬件上运行.它与现有的分布式文件系统有许多相似之处.然而,与其他分布式文件系统的区别很大.HDFS具有高度的容错能力,旨 ...

  10. 记XDCTF的misc之旅---base64隐写

    bWFpbigpe2ludCBpLG5bXT17KCgoMSA8PDEpPDwgKDE8PDEpPDwoMTw8Cm==ICAgICAgIDEpPDwoMTw8KDE+PjEpKSkrKCgxPDwx ...