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:

Input: [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1's at depth 2, one 2 at depth 1.

Example 2:

Input: [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27.
 

题意:

嵌套列表加权和

Solution1: BFS(level order traversal) 是最快的

code

 class Solution {
public int depthSum(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null){return 0;}
// initialize
int result = 0;
int depth = 1;
// put each item of list into the queue
Queue<NestedInteger> queue = new LinkedList<>(nestedList);
while(!queue.isEmpty()){
// depends on different depth, queue size is changeable
int size = queue.size();
for(int i = 0; i < size; i++){
NestedInteger n = queue.poll();
if(n.isInteger()){
result += n.getInteger() * depth;
}else{
// put each item of list into the queue
queue.addAll(n.getList());
}
}
depth++;
}
return result;
}
}

注意:   put each item of list into the queue 的写法有:

Queue<NestedInteger> queue = new LinkedList<>(nestedList); 
for(NestedInteger n :nestedList){
    queue.add(n)
}
 
queue.addAll(nestedList)
 

Solution2: DFS

code

 class Solution {
public int depthSum(List<NestedInteger> nestedList) {
if(nestedList == null) return 0;
return dfs(nestedList, 1);
} private int dfs(List<NestedInteger> nestedList, int depth){
int result = 0;
for(NestedInteger n : nestedList ){
if(n.isInteger()){
result = result + n.getInteger()*depth;
}else{
result = result + dfs(n.getList() , depth+1);
}
}
return result ;
}
}

[leetcode]339. Nested List Weight Sum嵌套列表加权和的更多相关文章

  1. LeetCode 339. Nested List Weight Sum

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

  2. 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. ...

  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. 在C#中如何判断线程当前所处的状态

    在C#中,线程对象Thread使用ThreadState属性指示线程状态,它是带Flags特性的枚举类型对象.   ThreadState 为线程定义了一组所有可能的执行状态.一旦线程被创建,它就至少 ...

  2. py-day2-3 python 字典

    # 字典的value可以是任何值 test = {'k1':'v1', 'k2':True, 'k3':[1,2,3,4,{'t1':'v2','t2':'v3'},5,6], 'k4':(88,99 ...

  3. Webservices部署在IIS6.0上的一个小问题

    部署方式还是跟网站的部署方式一样,可是通过localhost访问一直提示400(bad request)错误. 可以在iis上预览到.在vs上引用的时候怎么都预览不到. 换个思路,把localhost ...

  4. jmeter分布式、linux运行

    一.jmeter分布式压测(多台电脑一起压测) 1.有多台电脑,每台电脑上都有jmeter,而且这几台电脑都互相能ping通 2.在我的电脑的jmeter,bin目录下,修改jmeter.proper ...

  5. 转wave 文件解析

    转 1 WAVE 文件格式分析 WAVE 文件作为多媒体中使用的声音波形文件格式之一,它是以RIFF(Resource Interchange File Format)格式为标准的.每个WAVE文件的 ...

  6. docker 批量删除

    杀死所有正在运行的容器docker kill $(docker ps -a -q) 删除所有已经停止的容器docker rm $(docker ps -a -q) 删除所有未打 dangling 标签 ...

  7. DevExpress的42种窗体样式

    在Winform环境下DevExpress标题栏皮肤 第一步:引用DLL文件,安装DevExpress后在引用>程序集>扩展: DevExpress.BonusSkins.v12.2.dl ...

  8. hadoop的hdfs中的javaAPI操作

    package cn.itcast.bigdata.hdfs; import java.net.URI; import java.util.Iterator; import java.util.Map ...

  9. 使用httpClient发送post请求连接restful接口

    public static String httpPost(String url,String arg){ InputStream is; BufferedReader br; StringBuild ...

  10. python大法好——面向对象

    python大法好——面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没 ...