嵌套列表的加权和 · 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. ...
随机推荐
- Linux之 find之 ctime,atime,mtime
在Linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime atime 最后一次访问时间, 如 ls, more 等, 但 chmod, chow ...
- Oracle 11gR2 RAC集群服务启动与关闭总结
引言:这写篇文章的出处是因为我的一名学生最近在公司搭建RAC集群,但对其启动与关闭的顺序和原理不是特别清晰,我在教学工作中也发现了很多学员对RAC知识了解甚少,因此我在这里就把RAC里面涉及到的最常用 ...
- Errors running builder 'DeploymentBuilder' on project ' 解决方法
此问题一般发生在Myeclipse 保存文件并自动部署时候. Errors occurred during the build. Errors running builder 'DeploymentB ...
- 安卓5.0宣告了ARM平台全面进入64位时代
安卓5.0宣告了ARM平台全面进入64位时代 2014年10月份,安卓5.0正式版发布了,安卓5.0支持64位CPU,安卓5.0全面启用ART运行模式,在程序安装的时候,进行预编译,新的运行环境能够使 ...
- Django 组件-用户认证
用户认证 auth模块 from django.contrib import auth 1.1 .authenticate() 提供了用户认证,即验证用户名以及密码是否正确,一般需要username ...
- 阻塞队列之五:LinkedBlockingQueue
一.LinkedBlockingQueue简介 LinkedBlockingQueue是一个使用链表完成队列操作的阻塞队列.链表是单向链表,而不是双向链表.采用对于的next构成链表的方式来存储对象. ...
- Java-Runoob-高级教程:Java 序列化
ylbtech-Java-Runoob-高级教程:Java 序列化 1.返回顶部 1. Java 序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包 ...
- 029:高可用之MHA
高可用之MHA 一.MHA 简介 MHA(Master High Availability)是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能 ...
- verilog 之数字电路 寄存器,触发器。
我一直听说没有由code到circuit就只是入门了.实在没办法了.我想了一招,一个一个的写,然后看RTL,然后分析.这是第一篇. 1.触发器. 没有复位,置位.posedge clk 是触发沿时钟. ...
- SQL分组查询及聚集函数的使用
今天要做一个查询统计功能,一开始有点犯难,上午尝试大半天才写出统计sql语句,才发现自己sql分组查询及聚集函数没学好:其实就是group by子句和几个聚集函数,熟练使用统计功能很简单.在此总结下今 ...