[抄题]:

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)

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

对新的数据结构完全没有思路啊

[一句话思路]:

对列表List<随便什么东西>可以调用isInteger()和getInteger()取数,getList()取列表方法

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

[画图]:

[一刷]:

  1. 忘记乘以depth了,用?:的时候要该做的运算还是要做的,不能忘了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

对列表List<随便什么东西>可以调用isInteger()和getInteger()取数,getList()取列表方法

[复杂度]:Time complexity: O(n) Space complexity: O(n)

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

[关键模板化代码]:

for (NestedInteger e : list) {
ans += e.isInteger() ? e.getInteger() * depth : helper(e.getList(), depth + 1);
}

对于list那么长的NestedInteger 数据类型

[其他解法]:

[Follow Up]:

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

包络型整数

364. Nested List Weight Sum II 权重相反-想得出来大概意思,写不出来。还是写得太少

690. Employee Importance 同上

[代码风格] :

class Solution {
public int depthSum(List<NestedInteger> nestedList) {
return helper(nestedList, 1);
} public int helper(List<NestedInteger> list, int depth) {
int ans = 0;
for (NestedInteger e : list) {
ans += e.isInteger() ? e.getInteger() * depth : helper(e.getList(), depth + 1);
}
return ans;
}
}

嵌套列表的加权和 · Nested List Weight Sum的更多相关文章

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

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

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

  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 Nested List Weight Sum

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

  6. Nested List Weight Sum I & II

    Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list ...

  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

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

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

随机推荐

  1. HDFS(二)

    HDFS的I/O主要是三个方面: 一致性 HDFS在一致性上面主要是通过校验和(checksum)来实现:从client发起写入的时候会校验一下文件内容,但是发生在pipeline的最后一个节点的时候 ...

  2. centos6 查看SELinux状态 关闭SELinux

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.在这种访问控制体系的限制下,进程只能访问那 ...

  3. 老齐python-基础7(文件操作、迭代)

    在python3中,没有file这个内建类型了(python2中,file是默认类型) 1.读文件 创建文件,130.txt 并在里面输入 learn python http://qiwsir.git ...

  4. vs2015安装ORACLE的DbFirst

    不说DbFirst好在哪里,它和ModelFirst,CodeFirst都各有各的好,由于对于已经存在的一个大型的业务库,使用EntityFramework的更倾向于DbFirst,因为好多同事已经习 ...

  5. 1-3 superset数据模型

    在models.py中大部分的class对应数据库中的表,那么我们就从AuditMixinNullable这个类开始我们的模型解析. AuditMixin:这个类是FAB(Flask  AppBuil ...

  6. NumberUtils、ArrayUtils和RandomUtils工具类用法

    一.NumberUtils工具类 /*1.NumberUtils.isNumber():判断字符串是否是数字*/ NumberUtils.isNumber("5.96");//结果 ...

  7. SQL Server数据库常用的T-SQL命令

    1. 查看数据库的版本 select @@version 2.查看数据库所在机器操作系统参数 exec master..xp_msver 3. 查看数据库启动的参数 sp_configure 4.查看 ...

  8. 数据结构与算法JavaScript描述——栈

    栈就是和列表类似的一种数据结构,它可用来解决计算机世界里的很多问题. 栈是一种高效的数据结构,因为数据只能在栈顶添加或删除,所以这样的操作很快,而且容易实现. 栈的使用遍布程序语言实现的方方面面,从表 ...

  9. java代码--------随机输出100个随机数,要求每行10个数

    总结:不敢爱你么开口 package com.sads; ///实现随机输出100个数字,数字是0到9之间,每行输出10个 public class Wss { public static void ...

  10. [Java][Web]解决 Request 的乱码

    解决 get 提交的乱码 (手工处理) String username = request.getParameter("username"); username = new Str ...