[抄题]:

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. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  2. 虚拟主机wordpress文件上传大小限制更改

    默认的wp文件上传的大小都是2M 登录阿里云进入控制面板找到你的虚拟机实例 点击管理 改成10M,最大也就是10,虚拟机的睾丸之处.保存,去页面新媒体添加可以看到最大限制为10M了

  3. php mysql apache字符集(二) (转)

    1 MYSQL中的字符集概念  Mysql的字符集里有两个概念,一个是"Character set(字符集)",另一个是"Collations".1.1 Col ...

  4. MOSS 2013研究系列---修改默认Logo

    开发SharePoint2013 的时候,系统里面有一个“SharePoint” 的logo,客户很少不满意,我们的系统不能出现产品的名称,如下图: 咋么修改呢,咨询了广大网友,给出了一个解决方案: ...

  5. shell常用测试命令

    预定义变量: 预定义变量是由Bash程序预先定义好的一类特殊变量,用户只能使用预定义变量,而不能创建新的预定义变量,也不能直接为预定义变量赋值.预定义比变量使用"$"符合和另一个符 ...

  6. Linux的POSIX线程属性

    创建POSIX线程的函数为 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routin ...

  7. SAE部署Django1.6+MySQL

    [解决]SAE部署Django1.6+MySQL 终于可以舒口气了,今天大部分时间都在搞这个,很是蛋疼,网上资料良莠不齐,我不信这个之前没人做过,但是他们确实分享的不够好. 废话不多说,还是记录一下今 ...

  8. FU-A 分包

    FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法 [原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包 ...

  9. 004:MySQL数据库体系结构

    目录 一. MySQL数据库体系结构 1.MySQL数据库体系结构介绍 1 数据库定义 2 数据库实例 2. MySQL体系结构 1 单进程多线程结构 2 存储引擎的概念 3 体系结构图 4 逻辑存储 ...

  10. 003:MySQL账号创建授权以及Workbench

    目录 一. 权限管理 1."用户 + IP"的概念 2. 用户权限管理 3. 基本操作 4. 撤销权限 5.授权和创建用户 二. MySQL模拟角色 三. Workbench与Ut ...