原题

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的更多相关文章

  1. 【LeetCode】339. Nested List Weight Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 dfs 日期 题目地址:https://leetcod ...

  2. 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  3. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  4. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  5. 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. ...

  6. [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. ...

  7. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  8. 339. Nested List Weight Sum

    https://leetcode.com/problems/nested-list-weight-sum/description/ Given a nested list of integers, r ...

  9. 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...

随机推荐

  1. python基础之内置模块(二)

    configparser configparser用来对特定格式的文件进行解析处理,比如ha-proxy,rsync,samba配置文件等等均可.来个简单的文件先看下: [section1] #节点 ...

  2. hadoop第一次面到hr(品友互动)

    第一次“北漂” 准备了一个星期的Hadoop,把林子雨老师的视频刷了一遍,翻出了好久没用的小本本,密密麻麻的记了一大堆.刷了网上能找到的Hadoop的所有面试题(这个真的很重要) 然后,启程,北上,还 ...

  3. sublime Text3文件路径提示功能

    1>启动sublime Text3编辑器: 2> 点开菜单“preferences”——“package control”: 3>在弹出的下拉框中选择“install package ...

  4. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

  5. react 表单组件 异步渲染,值不会生效,需要手动改变对应组件的显示值

    楼主遇到这个问题已经是第二次,上次我们项目经理给帮忙解决了一次,这次又遇到了,刚开始不知道是这个问题,上次做的笔记也没找到了,估计又忘记了,这里抽三分钟记录一下, 当然这里这样做有个前提:就是你已经和 ...

  6. Zuul1与Spring Cloud Gateway对比

    一.API网关 1.1 Zuul1简介 1.2 Spring Cloud Gateway简介 二.对比 2.0 产品对比 2.1 性能对比 2.1.1 低并发场景 2.1.2 高并发场景 2.1.3 ...

  7. (2) laravel App目录结构说明

    应用的核心代码位于 app 目录下,默认情况下,该目录位于命名空间 App 下, 并且被 Composer 通过 PSR-4自动载入标准 自动加载. app 目录下包含多个子目录,如Console.H ...

  8. MySQL安装时出现的问题

    mysql正常安装结束之后需要连接你所安装的数据库的时候出现下面的错误: Client does not support authentication protocol requested by se ...

  9. controller中获取当前项目物理绝对路径

    用HttpServletRequest request获取 request.getSession().getServletContext().getRealPath(""); 上面 ...

  10. Python中的并行编程速度

    这里主要想记录下今天碰到的一个小知识点:Python中的并行编程速率如何? 我想把AutoTool做一个并行化改造,主要目的当然是想提高多任务的执行速度.第一反应就是想到用多线程执行不同模块任务,但是 ...