博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]339. Nested List Weight Sum嵌套列表加权和
阅读量:4338 次
发布时间:2019-06-07

本文共 2245 字,大约阅读时间需要 7 分钟。

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

1 class Solution { 2    public int depthSum(List
nestedList) { 3 // corner case 4 if(nestedList == null){
return 0;} 5 // initialize 6 int result = 0; 7 int depth = 1; 8 // put each item of list into the queue 9 Queue
queue = new LinkedList<>(nestedList);10 while(!queue.isEmpty()){11 // depends on different depth, queue size is changeable 12 int size = queue.size(); 13 for(int i = 0; i < size; i++){14 NestedInteger n = queue.poll();15 if(n.isInteger()){16 result += n.getInteger() * depth;17 }else{18 // put each item of list into the queue19 queue.addAll(n.getList());20 }21 }22 depth++;23 } 24 return result;25 }26 }

注意:   put each item of list into the queue 的写法有:

Queue
queue = new LinkedList<>(nestedList);

 

for(NestedInteger n :nestedList){    queue.add(n)}
 
queue.addAll(nestedList)

 

 

Solution2: DFS

code

1 class Solution { 2     public int depthSum(List
nestedList) { 3 if(nestedList == null) return 0; 4 return dfs(nestedList, 1); 5 } 6 7 private int dfs(List
nestedList, int depth){ 8 int result = 0; 9 for(NestedInteger n : nestedList ){10 if(n.isInteger()){11 result = result + n.getInteger()*depth;12 }else{13 result = result + dfs(n.getList() , depth+1);14 } 15 }16 return result ;17 }18 }

 

转载于:https://www.cnblogs.com/liuliu5151/p/9828259.html

你可能感兴趣的文章
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>