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
/**
* // 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 (嵌套列表重和)$的更多相关文章
- [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. ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- [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. ...
- 【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 ...
- [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. ...
- [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. ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- 【LeetCode】339. Nested List Weight Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...
- [LeetCode] Nested List Weight Sum 嵌套链表权重和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- 枚举类TimeUnit
枚举类TimeUnit 全路径为 java.util.concurrent.TimeUnit TimeUnit 主要用于通知基于时间的方法如何解释给定的计时参数 举例如下 如果 lock 不可用,则以 ...
- Unity GameObject Class
GameObject Note : gameObject 指的是当前挂着的对象. class in UnityEngine / Inherits from:Object Descriptio ...
- webpack + vue + node 打造单页面(入门篇)
1.node下载地址:http://nodejs.cn/download/,安装完成检查node和npm版本 2.淘宝镜像 : npm install cnpm -g --registry=https ...
- css控制div强制换行
div{white-space:nowrap;} 自动换行 div{ word-wrap: break-word; word-break: normal; } 强制英文单词断行 div{word-br ...
- append、extend与insert的区别
最近在自学Python语言,看到向列表增加更多数据时被append(),extend(),insert()方法绕晕了. 作为编程0基础的小白,觉得有必要自己再梳理一遍: 1.append()方法是指在 ...
- InnoDB Undo Log
简介 Undo Log包含了一系列在一个单独的事务中会产生的所有Undo Log记录.每一个Undo Log记录包含了如何undo事务对某一行修改的必要信息.InnoDB使用Undo Log来进行事务 ...
- 非常有用的css使用总结
积小流以成江海,很多东西你不总结就不是你的东西 常用css总结: /*设置字体*/ @font-face { font-family: 'myFont'; src: url('../font/myFo ...
- Erlang游戏服设计总结
这主要是一年多来,个人从事Erlang游戏服开发中对一些事情的思考. 想到哪说到哪,没有条理可言. 欢迎讨论. 通常Erlang游戏服务的设计涉及到的东东包括如下: 任务系统 活动系统 公会系统 玩法 ...
- sql server作业实现数据同步
作业介绍 SQL SERVER的作业是一系列由SQL SERVER代理按顺序执行的指定操作.作业可以执行一系列活动,包括运行Transact-SQL脚本.命令行应用程序.Microsoft Acti ...
- 发布系统Git使用指南 - the Git Way to Use Git
发布系统Git使用指南 --the Git Way to Use Git 背景 有文章曾归纳,Git是一套内容寻址文件系统,意思是,Git的核心是存储键值对^[1]^.显然,这样的形式不利于普通人 ...