【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 ...
随机推荐
- 【创业】2B创业历程
http://www.woshipm.com/chuangye/2800111.html http://www.woshipm.com/chuangye/2803240.html http://www ...
- Java中使用队列Queue
示例代码: Queue<Integer> queue = new LinkedList<Integer>(); for (int i = 1; i <= 100; i + ...
- PowerDesigner的安装和数据库创建
PowerDesigner安装方法: http://dev.firnow.com/course/3_program/java/javajs/20090908/174375.html 安装完这2个软件 ...
- OC入门笔记
1OC概述OC主要负责UI界面:C语言和C++可以用于图形处理.OC是一门面向对象的语言.C语言是面向过程的.比C++简单很多以C语言为基础,完全兼容C语言.OC语言中的所有事物都是对象,都有isa指 ...
- vue路由传参的三种方式
方式一 通过query方式传参 这种情况下 query传递的参数会显示在url后面 this.$router.push({ path: '/detail', query: { id: id } }) ...
- Hystrix多个线程池切换执行超时带来的问题(图解)
线程池切换带来的超时问题 上图有什么问题: Controller的Hystrx线程池已经到了超时时间,而FeignClient的Hystrx线程池还没到超时时间. 场景: Controller ...
- python3.6-Yelp/elastalert0.2.1-elk7.2.0邮件加企业微信告警
0.修改时区(前提条件已经安装好elk7.2) rm -f /etc/localtimecp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimetimed ...
- php5.6安装及php-fpm优化配置
1,安装依赖包: yum install -y gcc gcc-c++ zlib zlib-devel pcre pcre-devel gd libjpeg libjpeg-devel libpn ...
- 2、1 昨天讲列表缓存,为了让列表更新,我们需要在增、删、改方法之前加 @CacheEvict(value="list",allEntries = true)
package com.bw.service; import java.util.List; import javax.annotation.Resource; import org.springfr ...
- Lamda