【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 ...
随机推荐
- tk.mybatis 报错:tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiLogMapper ...
- LeetCode_69. Sqrt(x)
69. Sqrt(x) Easy Implement int sqrt(int x). Compute and return the square root of x, where x is guar ...
- SQL Server中的加密
参考文献: 细说SQL Server中的加密 Transparent Data Encryption (TDE) Database Encryption Key (DEK) management SQ ...
- Nginx负载均衡中4层代理和7层代理对比
1.4层代理和7层代理什么意思? 这里的层是OSI 7层网络模型,OSI 模型是从上往下的,越底层越接近硬件,越往上越接近软件,这七层模型分别是物理层.数据链路层.网络层.传输层.会话层.表示层.应用 ...
- 关于PADS的一些概念和实用技巧(二)
关于PADS的一些概念和实用技巧(二) 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 1. 关于制作part 首先在logic中绘制CAE封装,在保存元件时 ...
- 给引入页面的js和css资源加上版本号,防止浏览器缓存资源
最近因为在做前端开发的相关工作,每次发布新版本以后,不到5分钟,测试童鞋一个接一个的抱怨说BUG根本就没有修改,这个时候你说的最多的话就是“清缓存!!清页面缓存!!你没有清缓存!!你清理了页面缓存就对 ...
- 机器学习第二节_pandas_数据操作
今天打个卡, 还不错,学到20课了, 简单的把pandas的操作过一遍, 这没有numpy学的好 1. 读取csv文件 import pandasfood_info = pandas.read_csv ...
- JRE和JDK的作用和区别
JVM(Java Virtual Machine)是一个虚拟的用于执行bytecode字节码的“虚拟计算机”JRE(Java Runtime Environment):Java 虚拟机.库函数 ...
- [转帖]一个关于国密SM4的故事
一个关于国密SM4的故事 https://www.cnblogs.com/ouyida3/p/10053862.html SM1 硬件SM2 非对称加密SM3 hash算法SM4 对称加密 一个关于国 ...
- 公式编辑器之 AxMath(18)
1. 使用教程 视频教程,一共有18集,每集都比较短. >> 视频教程链接:B站,速度快,清晰 2. 破解软件下载链接 >> 下载链接:复制链接到迅雷或IDM下载很快 3. M ...