[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. ...
随机推荐
- MySQL Execution Plan--EXPLAIN用法
MySQL Explain新用法: --使用EXPLAIN来查看语句的最终执行计划 语法:EXPLAIN [EXTENDED] SELECT select_options --在MYSQL .7版本后 ...
- 算法笔记 3.2 codeup1934 找X
#include <stdio.h> ; int a[maxn]; int main(void){ int n; while(scanf("%d", &n)!= ...
- 在C#中如何判断线程当前所处的状态
在C#中,线程对象Thread使用ThreadState属性指示线程状态,它是带Flags特性的枚举类型对象. ThreadState 为线程定义了一组所有可能的执行状态.一旦线程被创建,它就至少 ...
- debian系统下apache2开启ssi功能
SSI (Server Side Include)的 html 文件扩展名 (.shtml), 通常称为"服务器端嵌入"或者叫"服务器端包含"说白了就是类似其他 ...
- SVN 撤回已提交的代码
1. TortoiseSVN -----> Show log 2.右键点击你想撤回的提交 -> Revert changes from this revision ----->Rev ...
- 报错:Exception in thread "main" com.typesafe.config.ConfigException$UnresolvedSubstitution
报错现象: 报错原因: pom文件中的jar包太高,可以降低jar包的版本号. 报错解决: 我将2.11换成了2.10,即可解决. <dependency> <groupId> ...
- google的protobuf简单介绍
google的protobuf是一种轻便高效的结构化数据存储格式,在通信协议和数据存储等领域中使用比较多.protobuf对于结构中的每个成员,会提供set系列函数和get系列函数. 但是,对于使用来 ...
- MOBA英雄AI设计分享
转自:http://www.gamelook.com.cn/2018/07/333877 文/wataloo 1 设计概要 1.1 设计原则和目的 英雄AI的目的主要有: 1.新手过渡局,让玩家刚 ...
- C#操作wps、excel
比如打开表格,如下 object openEt() { ]; Type wpsAppName; string progID = "KET.Application";// " ...
- Kafka和的安装与配置
本文主要介绍Kafka的安装与配置: 集群规划 datanode1 datanode2 datanode3 zk zk zk kafka kafka kafka kafka jar包下载地址 http ...