博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
339. Nested List Weight Sum
阅读量:4538 次
发布时间:2019-06-08

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

/*     * 339. Nested List Weight Sum     * 2016-7-10 by Mingyang     * 这就是很简单的dfs了,不详谈,count+1不是count++     */    public int depthSum(List
nestedList) { if(nestedList==null||nestedList.size()==0) return 0; return depthSumHelper(nestedList,1); } public int depthSumHelper(List
nestedList,int count){ int res=0; for(NestedInteger nest:nestedList){ if(nest.isInteger()){ res+=count*nest.getInteger(); }else{ List
temp=nest.getList(); res+=depthSumHelper(temp,count+1); } } return res; } // This is the interface that allows for creating nested lists. // You should not implement it, or speculate about its implementation public interface NestedInteger { // @return true if this NestedInteger holds a single integer, rather than a nested list. public boolean isInteger(); // @return the single integer that this NestedInteger holds, if it holds a single integer // Return null if this NestedInteger holds a nested list public Integer getInteger(); // @return the nested list that this NestedInteger holds, if it holds a nested list // Return null if this NestedInteger holds a single integer public List
getList(); }

 

转载于:https://www.cnblogs.com/zmyvszk/p/5659237.html

你可能感兴趣的文章
面向接口编程详解(二)——编程实例
查看>>
解决java.lang.NoClassDefFoundError: org/apache/log4j/Level
查看>>
端口号
查看>>
mysql for macOS安装
查看>>
jquery与checkbox的checked属性的问题
查看>>
HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)
查看>>
java 格式化字符串
查看>>
[.Net]轻量ORM——Dapper
查看>>
语言基础
查看>>
C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)
查看>>
C#中字符串转换成枚举类型的方法
查看>>
psplash
查看>>
git的安装和简单使用
查看>>
Airplace平台
查看>>
TinyOS实例介绍
查看>>
我是怎么定义微服务平台?
查看>>
python random
查看>>
互联网技术
查看>>
input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法
查看>>
【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2)...
查看>>