嵌套列表的加权和 · Nested List Weight Sum
[抄题]:
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);
}
对于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的更多相关文章
- [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. ...
- [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. ...
- [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. ...
- 【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 ...
- LeetCode Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 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 ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- [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. ...
随机推荐
- list.ForEach的用法
Templist.ForEach(o => { var isSel = ReviewerFileRelationService.Where(s => s.PackageFileId == ...
- Mock&Spring集成
Mock&Spring集成 常规Mock单元测试 请参考上一篇文档Mock mock框架的功能性对比 http://jmockit.github.io/MockingToolkitCompar ...
- PES包格式
PES是Packetized Elementary Stream的简称,是将原始ES流打包后形成的,再将PES经过不同的打包方式可以组成MPEG program stream 和 MPEG trans ...
- python--logging库学习_第三波
本文介绍如何写一个Python日志类,用来输出不同级别的日志信息到本地文件夹下的日志文件里.为什么需要日志输出呢,我们需要记录我们测试脚本到底做了什么事情,最好的办法是写事件监听.这个事件监听,对我们 ...
- appium+python自动化28-name定位
前言 appium1.5以下老的版本是可以通过name定位的,新版本从1.5以后都不支持name定位了 name定位报错 1.最新版appium V1.7用name定位,报错: selenium.co ...
- PAT甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
- combiner中使用状态模式
mapreduce中的combine过程 hadoop的map过程执行完成后,每一个map都可能会产生大量的本地输出,Combiner的作用就是对map端的输出先做一次合并,减少在map和reduce ...
- 【POJ】2329 Nearest number - 2(搜索)
题目 传送门:QWQ 分析 在dp分类里做的,然而并不会$ O(n^3) $ 的$ dp $,怒写一发搜索. 看起来是$ O(n^4) $,但仔细分析了一下好像还挺靠谱的? poj挂了,没在poj交, ...
- 阶段性总结(PHP-Session)
PHP Session PHP session 变量用于存储关于用户会话(session)的信息,或者更改用户会话(session)的设置.Session 变量存储单一用户的信息,并且对于应用程序中的 ...
- [Cpp primer] Library string Type
In order to use string type, we need to include the following code #include<string> using std: ...