[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.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Example 1:
Input: [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1's at depth 2, one 2 at depth 1.
Example 2:
Input: [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27.
题意:
嵌套列表加权和
Solution1: BFS(level order traversal) 是最快的





code
class Solution {
public int depthSum(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null){return 0;}
// initialize
int result = 0;
int depth = 1;
// put each item of list into the queue
Queue<NestedInteger> queue = new LinkedList<>(nestedList);
while(!queue.isEmpty()){
// depends on different depth, queue size is changeable
int size = queue.size();
for(int i = 0; i < size; i++){
NestedInteger n = queue.poll();
if(n.isInteger()){
result += n.getInteger() * depth;
}else{
// put each item of list into the queue
queue.addAll(n.getList());
}
}
depth++;
}
return result;
}
}
注意: put each item of list into the queue 的写法有:
Queue<NestedInteger> queue = new LinkedList<>(nestedList);
for(NestedInteger n :nestedList){
queue.add(n)
}
queue.addAll(nestedList)
Solution2: DFS
code
class Solution {
public int depthSum(List<NestedInteger> nestedList) {
if(nestedList == null) return 0;
return dfs(nestedList, 1);
}
private int dfs(List<NestedInteger> nestedList, int depth){
int result = 0;
for(NestedInteger n : nestedList ){
if(n.isInteger()){
result = result + n.getInteger()*depth;
}else{
result = result + dfs(n.getList() , depth+1);
}
}
return result ;
}
}
[leetcode]339. Nested List Weight Sum嵌套列表加权和的更多相关文章
- 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. ...
- [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】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] 364. Nested List Weight Sum II_Medium tag:DFS
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 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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...
- [LeetCode] Nested List Weight Sum 嵌套链表权重和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- UEFI+GPT双硬盘安装Win10+Ubuntu16.04双系统
转载请注明出处:http://www.cnblogs.com/willnote/p/6725594.html 安装环境 SSD+HDD双盘,Win10安装在SSD里,HDD分出来60G安装Ubuntu ...
- pandas Dataframe 取某行
In [1]: df = DataFrame(randn(5,2),index=range(0,10,2),columns=list('AB')) In [2]: df Out[2]: A B 0 1 ...
- Flutter 学习资料
Flutter 学习资料: 学习资料 网址 Flutter 中文网 https://flutterchina.club/ <Flutter实战>电子书 https://book.flutt ...
- windows下apache httpd2.4.26集群完整搭建例子:下载、启动、tomcat集群例子
第一部分——下载 1.1: 百度搜索apache httpd关键字,第一个链接既是官方下载地址.如果这一步不会,此篇文章不适合你阅读,请自行停止. 1.2:进入官网http://httpd.apach ...
- studio--常见设置
13.Butterknife插件:zelezny 12.android studio怎么设置打开项目时打开项目列表? 11.stuido 代码背景颜色设置为护眼模式 ======== 13.But ...
- 5、Zookeeper命令操作
一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息.用户在客户端可以通过telnet或n ...
- .NET自动化测试工具链:Selenium+NUnit+ExtentReport
Selenium可以执行UI的交互,ExtentReport用来生成测试报告,NUnit是我熟悉的基础测试框架,当然你也可以用MSTest.Xunit来代替.Selenium.NUnit没啥好讲的,网 ...
- (转)查看SQLServer最耗资源时间的SQL语句
原文地址:https://www.cnblogs.com/My-Dream/p/6270308.html 1.找出执行时间最长的10条SQL(适用于SQL SERVER 2005及其以上版本) SEL ...
- bresenham 算法生成直线
struct Point{ Point() { posx = 0; posy = 0; } Point(int x, int y) { posx = x; posy = y; } int posx; ...
- 205. jetcache:你需要知道的小技巧
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...