[抄题]:
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()取列表方法
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 忘记乘以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); }
[其他解法]:
[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; } }