[抄题]:

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.

Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.

Example 1:
Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2)

Example 2:
Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

NestedInteger是单独的一种结构。可以是Integer,也可以是带括号的。所以有.getInteger().getList()方法

[一句话思路]:

不加权的次数多了之后,自然就变成加权的了

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. for循环里面会处理每个元素的,不用再用while
  2. 每一层剥皮的时候,把剩下的都加进去,用array的addAll方法

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

NestedInteger是单独的一种结构。可以是Integer,也可以是带括号的。所以有.getInteger().getList()方法

[复杂度]:Time complexity: O(dfs的for循环 没法计算) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
//cc
if (nestedList.isEmpty()) return 0; //ini:
int weighted = 0, unweighted = 0; while (!nestedList.isEmpty()) {
//new array nextLevel
List<NestedInteger> nextLevel = new ArrayList<NestedInteger>(); //for loop
for (NestedInteger ni : nestedList) {
//if integer, add to unweighted
if (ni.isInteger()) {
unweighted += ni.getInteger();
}else
//else, addAll to nextLevel
nextLevel.addAll(ni.getList());
}
//add to unweighted;
weighted += unweighted;
nestedList = nextLevel;
} return weighted;
}
}

364. Nested List Weight Sum II 大小反向的括号加权求和的更多相关文章

  1. [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. ...

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

  3. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  4. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  5. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...

  6. [LeetCode] 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_Medium tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  8. Leetcode: Nested List Weight Sum II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  9. 嵌套列表的加权和 · Nested List Weight Sum

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

随机推荐

  1. poj2279——Mr. Young's Picture Permutations

    Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...

  2. 使用FileZilla连接时超时,无法连接到服务器

    更改一下加密方式,就是不用TLS,用相对不安全方式的(可选项)  腾讯云就是这样的,

  3. lua qt測試成功

    用luabind寫了一個qt的簡單binding 測試成功

  4. JavaScript获取mp4文件MIME编码格式,用于判读是否是h.264,解决在线播放只有声音问题

    测试网址:https://gpac.github.io/mp4box.js/test/filereader.html js库:mp4box.js 不能在线播放的:audio/mp4; codecs=& ...

  5. Java同步学习(持续更新)

    在需要考虑线程安全性的场合,可以考虑以下五种方式来实现线程的安全性: 1.同步方法 即有synchronized关键字修饰的方法. 由于java的每个对象都有一个内置锁,当用此关键字修饰方法时,   ...

  6. Android App启动速度优化

    解决在桌面上点击APP图标后经过一两秒后才显示页面,以及App启动后主界面显示过慢问题 一.应用的启动方式 1.冷启动:当启动应用时,后台没有该应用的进程,这时系统会首先会创建一个新的进程分配给该应用 ...

  7. Python装饰器的调用过程

    在Python学习的过程中,装饰器是比较难理解的一个应用.本人也在学习期间也遇到很多坑,现将装饰器的基本调用过程总结一下. 首先,装饰器用到了“闭包”,而“闭包”是学习装饰器的基础,所以在讲装饰器之前 ...

  8. 5、Zookeeper命令操作

    一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息.用户在客户端可以通过telnet或n ...

  9. inception 自动化sql审核

    ##概念: Inception是一款自动化运维的利器,有别与现在各个公司使用的方式,使用Inception,将会给DBA带来最大的便利性,将DBA从繁冗的工作中解放出来,做一些更多的自动化工作,或者从 ...

  10. 如何在idea里面新建一个maven项目,然后在这个maven项目里创建多个子模块

    如何在idea里面配置maven我这里就不多说了 先新建一个maven项目作为总的管理项目 不用勾选什么,直接下一步 这样子一个普通的maven项目就创建成功了. 因为这个项目是用来管理多个子模块的, ...