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. Python 输出

    普通的输出 生活中的“输出”: 软件中的“输出”: python中变量的输出: print('hello world') 格式化输出 占位符% print('Hello,%s' % 'Python') ...

  2. ubuntu server资料

    2.改变键盘布局 sudo dpkg-reconfigure keyboard-configuration 或sudo vim /etc/default/keyboard,修改XKBLAYOUT变量的 ...

  3. java File处理

    /**************************************************************************************工具类********** ...

  4. 在IntelliJ IDEA中使用VIM

    IdeaVim(下载)插件可以让你在IntelliJ IDEA中键盘敲的飞起. 安装 打开IDEA的设置,在Plugins里,你可以选择在线搜索Vim安装,当然如果不行,就可以选择单独下载后安装,以下 ...

  5. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  6. [UE4]Tile View

    一.Tile View也属于List View,Tile View以小方格的形式展示子控件. 二.Tile View.Entry Height.Tile View.Entry Width设置每个Til ...

  7. ado.net调用返回多结果集的存储过程

  8. Oracle 唯一 约束(unique constraint) 与 索引(index) 关系说明

    一. 官网对Unique Constraints说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/datainte.h ...

  9. Android 开发 框架系列 EventBus 事件总线

    介绍 GitHub:https://github.com/greenrobot/EventBus 先聊聊EventBus 线程总线是干什么的,使用环境,优点.缺点. 干什么的? 一句话,简单统一数据传 ...

  10. python字符串前面的r/u/b的意义 (笔记)

    u/U:表示unicode字符串 : 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. r/R:非转义的原始字符串: 与普通字符相比,其他相对特殊的字符,其中可能包含 ...