[抄题]:

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. hadoop之 hadoop用途方向

    hadoop是什么?Hadoop是一个开源的框架,可编写和运行分不是应用处理大规模数据,是专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式.Hadoop=HDFS(文 ...

  2. poj 2187 Beauty Contest——旋转卡壳

    题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...

  3. 数据结构与算法JavaScript描述——栈的使用

    有一些问题特别适合用栈来解决.本节就介绍几个这样的例子.   1) 数制间的相互转换 可以利用栈将一个数字从一种数制转换成另一种数制.假设想将数字n 转换为以b 为基数的数字,实现转换的算法如下. 使 ...

  4. java代码--------实现位运算符不用乘除法啊

    总结:<<:乘法 >>:除法 package com.mmm; public class dfd { public static void main(String[] args ...

  5. EasyUI 左,右(上、下)布局

    左,右(上.下)布局 <body class="easyui-layout"> <div data-options="region:'west',col ...

  6. iRedMail的搭建过程记录

    iRedMail的搭建和注意事项 经过一段时间的折腾,终于将iRedMail搭建起来了,下面介绍一下搭建的过程,以及注意事项. 注意事项:  1. iRedMail不支持重复安装,如果安装错误,请重置 ...

  7. (转) C#中Timer使用及解决重入(多线程同时调用callback函数)问题

    原文链接: http://www.cnblogs.com/hdkn235/archive/2014/12/27/4187925.html

  8. 使用mysql-connector-java出现的错误

    如果你使用的是mysql-connector-java6.*版本,并使用c3p0连接池的话,就可能出错.因为最近在使用Maven构建项目,想着换成最新的版本试试,就是用了个mysql-connecto ...

  9. Thread pools & Executors

    Thread pools & Executors Run your concurrent code in a performant way All about thread pools # H ...

  10. open the flashback

    1.打开flashback: 关闭数据库 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为flashback依赖Media recovery,所以在打 ...