【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 ...
随机推荐
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- 定时杀死warn进程
6 6 * * * /root/wz/mysqlRestart.sh #MySQL restart7 6 * * * /bin/sh /home/warn/kill_.sh8 6 * * * / ...
- MySQL从.ibd文件中恢复数据
首先,在MySQL命令行下执行如下命令可以查看MySQL中存放数据的位置: show global variables like "%datadir%"; 我这里的执行结果: +- ...
- Zabbix部分监控指标
MySQL请求流量带宽.MySQL响应流量带宽.CPU使用率.内存利用率.网卡流量等.
- 【OpenGL学习】 四种绘制直线的算法
我是用MFC框架进行测试的,由于本人也没有专门系统学习MFC框架,代码若有不足之处,请指出. 一,先来一个最简单的DDA算法 DDA算法全称为数值微分法,基于微分方程来绘制直线. ①推导微分方程如下: ...
- 【POJ - 3268 】Silver Cow Party (最短路 Dijkstra算法)
Silver Cow Party Descriptions 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x, ...
- Asp.Net文件的上传和下载
aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="上传和下载文件. ...
- vue报错:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
在.vue文件中引入了 element-ui 的 table 和 pagination 组件后,报错:Component template should contain exactly one roo ...
- csu 1901: 赏赐 OR 灾难
1901: 赏赐 OR 灾难 Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 86 ...
- oracle学习笔记day1
oracle数据库前言 oracle sqllesson1 Selecting Rowslesson2 Sorting & Limiting Selected Rowslesson3 Sing ...