嵌套列表的加权和 · 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. ...
随机推荐
- bzoj 4816 [Sdoi2017]数字表格——反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4816 \( ans=\prod\limits_{d=1}^{n}f[d]^{\sum\lim ...
- 安装Zookeeper(集群版)
一.环境介绍(3台虚拟机) IP Hostname 192.168.2.14 javaweb04 192.168.2.15 javaweb05 192.168.2.16 javaweb06 二.配置文 ...
- volatile浅析
volatile的意思是不稳定的.易变的,但在多线程中却跟字面意思一毛钱关系没有.作为一个变量修饰符,它主要有两个作用:一个是告诉大家,该变量是一个在多个线程之间均可见的变量:另一个是告诉java虚拟 ...
- Python——你应该知道这些
1. Python的出生 1989年 Guido van Rossum开始编写Python语言编辑器(龟叔为了打发无聊的圣诞节) 1991年 第一个Python编译器诞生(正式诞生) 1994年 Py ...
- xdebug : Debug session was finished without being paused
一.当调试模式出现说路径不匹配的时候,需要检查当前请求的URL和设置断点的是否在同样的位置 Debug session was finished without being paused It may ...
- VS2003在vista/win7下搜索会出现僵死
1. VS2003在vista下搜索关键词的时候会出现僵死的问题的解决方案: VS2003快捷方式右击选中属性->兼容性页签 : 选中用兼容模式运行这个程序,下拉框中用windows xp2 ...
- 2017百度之星初赛B-1002(HDU-6115)
一.思路 这题“看似”比较难搞的一点是,一个节点上有多个办公室,这怎么求?其他的,求树中任意两个节点的距离(注意:没有最远或最最进这一说法,因为树上任意两个节点之间有且仅有一条路径.不然就有回路了,对 ...
- node的express中间件之session
虽然session与cookie是分开保存的.但是session中的数据经过加密处理后默认保存在一个cookie中.因此在使用session中间件之前必须使用cookieParser中间件. app. ...
- 请描述一下 cookies,sessionStorage和localStorage的区别?
cookie在浏览器和服务器间来回传递. sessionStorage和localStorage不会sessionStorage和localStorage的存储空间更大:sessionStorage和 ...
- Python Tkinter编程
声明:主要是为了自己方便,所以把别人的教程搬到这里来,没有其他的意思. 如果有侵犯您的权益,请联系我QQ:3121922008 我会在第一时间妥善处理,抱歉. 还有其他的一些搜集的资源连接放在http ...