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. ...
随机推荐
- shell脚本命令,一些你在书上找不到的命令。
1.!$<!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串.如:你可能会这样: $mkdir mydir$mv mydir yourdir$cd yourdir 可以改成: $mkd ...
- 上传文件复用代码【fileUpload】
这是使用了FileUpload上传组件的,解决了中文乱码问题了,并且删除了临时文件的. 使用了一个Book对象做示范 private Book uploadData(HttpServletReques ...
- C++中const几中用法
转载自:http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html 1. const修饰普通变量和指针 const修饰变量,一般 ...
- StringBuffer类的构造方法
public StringBuffer():无参构造方法 public StringBuffer(int capacity):指定容量的字符串缓冲区对象(默认是16个字符) public String ...
- 使用apache反向代理tomacat
起源 在大部分的生产环境中,基本上使用的都是java程序,从而促进了各种应用程序中间件的产生,在这里大概有几种,tomcat作为最著名的开源servlet容器,jboss也是开源的,而且有管理界面,主 ...
- js如何获取客户端IP
1.在HTML页面里面引入<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> 2.获取 ...
- [js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解
路径在canvas绘图中,经常被用到,是一个非常重要的概念. 比如:我们要在canvas画出3条直线,要求用不同的颜色加以区分. <style> body { background: #0 ...
- 深入理解计算机系统chapter1
---恢复内容开始--- 预处理器+编译器+汇编器+链接器=编译系统 运行hello程序 操作系统: 无论是在单核还是多核系统中,一个CPU看上去都在并发的执行多个进程,这是通过处理器在进程间切换来实 ...
- java集合系列——List集合之Stack介绍(五)
1.Stack的简介 Stack 类表示后进先出(LIFO)的对象堆栈.它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈.它提供了通常的 push 和 pop 操作,以及取堆栈顶点的 ...
- 打印ASCII码
总时间限制:1000ms内存限制:65536kB 描述 输入一个除空格以外的可见字符(保证在函数scanf中可使用格式说明符%c读入),输出其ASCII码. 输入 一个除空格以外的可见字符. 输出 一 ...