【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:
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 + 42 + 63 = 27)
解析
嵌套数组加权和
给出一个数组,该数组的元素可能是一个数字,也可能是一个数组
最外层数组权重是1,向内一层权重+1,
求给出数组的权重和
解题思路
递归求和,第一层权重为1,判断该元素是int还是数组;若是数组,直接数字乘以权重;若是数组,递归调用方法,权重+1
我的解法
因为没有现成的数据结构,所以需要自己定义一个
/**
* 自定义嵌套数组
* Created by Administrator on 2017/7/19.
*/
public class NestedInteger {
//包含一个整数
private Integer integer;
//以及一个数组
private List<NestedInteger> nestedIntegers;
//初始化Int
public NestedInteger(Integer integer) {
this.integer = integer;
}
//初始化数组
public NestedInteger(List<NestedInteger> nestedIntegers) {
this.nestedIntegers = nestedIntegers;
}
//判断是否int
public Boolean isInteger() {
if (integer != null && nestedIntegers == null) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
//判断是否数组
public Boolean isNestedIntger() {
if (integer == null && nestedIntegers != null) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
//Getter Setter
public Integer getInteger() {
return integer;
}
public void setInteger(Integer integer) {
this.integer = integer;
this.nestedIntegers = null;
}
public List<NestedInteger> getNestedIntegers() {
return nestedIntegers;
}
public void setNestedIntegers(List<NestedInteger> nestedIntegers) {
this.nestedIntegers = nestedIntegers;
this.integer = null;
}
}
然后再递归实现求和
/**
* 339. Nested List Weight Sum
* 嵌套数组加权和
*/
public class NestedListWeightSum {
public static int getWeightSum(List<NestedInteger> nestedIntegers, int weight) {
if (nestedIntegers == null || nestedIntegers.size() <= 0 || weight <= 0) {
return 0;
}
int weightSum = 0;
for (int i = 0; i < nestedIntegers.size(); i++) {
if (nestedIntegers.get(i) != null && nestedIntegers.get(i).isInteger()) {
weightSum += nestedIntegers.get(i).getInteger() * weight;
} else {
weightSum += getWeightSum(nestedIntegers.get(i).getNestedIntegers(), weight + 1);
}
}
return weightSum;
}
}
【leetcode】339. Nested List Weight Sum的更多相关文章
- 【LeetCode】339. Nested List Weight Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 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]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】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 339. Nested List Weight Sum
https://leetcode.com/problems/nested-list-weight-sum/description/ Given a nested list of integers, r ...
- 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...
随机推荐
- tp5.1 model 方法下的like语句查询
$where_like = ['title','like','%' . $_GET['title'] . '%']; $result_list = $this->model->where( ...
- python获取昨日日期
获取昨日日期oneday = datetime.timedelta(days=1) 一天 day = datetime.datetime.strptime(self.date,'%Y-%m-%d') ...
- 【OpenGL学习】 四种绘制直线的算法
我是用MFC框架进行测试的,由于本人也没有专门系统学习MFC框架,代码若有不足之处,请指出. 一,先来一个最简单的DDA算法 DDA算法全称为数值微分法,基于微分方程来绘制直线. ①推导微分方程如下: ...
- 《C语言程序设计》学习笔记(二)
第八章 函数 函数的基本概念 定义:函数由函数名.参数和函数体组成. 函数定义的一般形式: 类型说明符 函数名(形式参数声明) { [说明与定义部分] 语句: } 说明: 1.类型说明符用来说明函数的 ...
- 移动架构-AOP面向切面编程
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点, ...
- python从hello world开始 - python基础入门(3)
万丈高楼平地起,编程亦如此.改变世界是结果,坚持努力学习改bug是过程,hello world是开始,所有语言均是如此. 一.使用pycharm创建第一个hello world 项目 1.Create ...
- Asp.Net Core文件上传
文件上传功能在实际开发中经常使用,在 .Net Core中,文件上传接收类型不再使用 HttpPostedFile 或 HttpFileCollection来接收,而是使用 IFormFile 或 I ...
- Soda Machine【差分+离散化】
题目链接:https://ac.nowcoder.com/acm/contest/1106/A 题目大意: 1.一条长1e9的线段,每个节点都可以上色.给出n次操作,每次操作将[l, r]区间内的节点 ...
- typescript无法识别vue中的$refs
例如:vue-fullscreen <template> <div class="Test"> <fullscreen ref="fulls ...
- vue-cli webpack打包后加载资源的路径问题
vue项目,访问打包后的项目,输入路径后,页面加载空白.这时会有两类问题,都是路径问题. 1.一个是css,js,ico等文件加载不到,是目录里少了dist 打开页面时一片空白 解决办法: confi ...