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. WebSocket是什么原理?为什么可以实现持久连接?

    作者:Ovear 链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  2. Pytho, struct处理二进制(pack和unpack)

    [转]Python使用struct处理二进制(pack和unpack用法) Leave a reply 转载自:http://www.cnblogs.com/gala/archive/2011/09/ ...

  3. 20175202 《Java程序设计》第四周学习总结

    20175202 <Java程序设计>第四周学习总结 第五章学习内容 1.子类的继承性: (1)子类与父类在同一包中的继承性:子类自然地继承了其父类中不是private的成员变量作为自己的 ...

  4. python学习路程1

    常用的转义字符还有: \n 表示换行 \t 表示一个制表符 \\ 表示 \ 字符本身 任务 请将下面两行内容用Python的字符串表示并打印出来: Python was started in 1989 ...

  5. C语言----管道

    一.管道的概念 管道是一种队列类型的数据结构,它的数据从一端输入,另一端输出.管道最常见的应用是连接两个进程的输入输出,即把一个进程的输出编程另一个进程的输入.shell中存在专门的管道运算符&quo ...

  6. 依赖、耦合、解耦、控制反转(IOC)、依赖注入(DI)

    随着net的深入学习,出现了很多概念性的东西需要理解,现在统一记录一下. 1.依赖:现阶段在任何一个有请求作用的系统,都会出现A类调用B类的情况,这时候A类就依赖于B类,A类和B类存在依赖关系. 2. ...

  7. 在Release版本下使用VLD

    前提 同Debug版本在VC中配置好VLD的相关信息,拷贝 Visual Leak Detector\bin\Win32目录下所有的文件和vld.ini到工程目标路径下. 强制检测 在程序入口处的cp ...

  8. mysql优化-数据库优化、SQL优化

    我有一张表w1000,里面有1000万条数据,这张表结构如下:CREATE TABLE `w1000` ( `id` varchar(36) NOT NULL, `name` varchar(10) ...

  9. IIC时序详解

    Verilog IIC通信实验笔记 Write by Gianttank 我实验的是 AT24C08的单字节读,单字节写,页读和页写,在高于3.3V系统中他的通信速率最高400KHZ的,我实验里用的是 ...

  10. U3D学习14-一阶段学习总结

    一.UGUI界面拖拽 1.物品类中继承以下5个接口 命名空间: UnityEngine.EventSystem; IBeingDragHandler (OnBeingDrag) IDragHandle ...