题目

给一个嵌套整数序列,请你返回每个数字在序列中的加权和,它们的权重由它们的深度决定。

序列中的每一个元素要么是一个整数,要么是一个序列(这个序列中的每个元素也同样是整数或序列)。

与 前一个问题 不同的是,前一题的权重按照从根到叶逐一增加,而本题的权重从叶到根逐一增加。

也就是说,在本题中,叶子的权重为1,而根拥有最大的权重。

示例 1:

输入: [[1,1],2,[1,1]]

输出: 8

解释: 四个 1 在深度为 1 的位置, 一个 2 在深度为 2 的位置。

示例 2:

输入: [1,[4,[6]]]

输出: 17

解释: 一个 1 在深度为 3 的位置, 一个 4 在深度为 2 的位置,一个 6 在深度为 1 的位置。 13 + 42 + 6*1 = 17。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/nested-list-weight-sum-ii

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

使用两遍DFS,第一遍计算出最大深度,第二遍计算加权和。

PS:若是深度计算方式相反,则只需一遍DF计算加权和。

代码

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class Solution {
private int maxDepth=0; public int depthSumInverse(List<NestedInteger> nestedList) {
updateMaxDepth(nestedList,1);
return getDepthSum(nestedList,1);
} private void updateMaxDepth(List<NestedInteger> nestedList,int depth){
for(NestedInteger nestedInteger:nestedList){
if(nestedInteger.isInteger()){
maxDepth=depth>maxDepth?depth:maxDepth;
}
else {
updateMaxDepth(nestedInteger.getList(),depth+1);
}
}
} private int getDepthSum(List<NestedInteger> nestedList,int depth){
int sum=0;
for(NestedInteger nestedInteger:nestedList){
if(nestedInteger.isInteger()){
sum+=(maxDepth-depth+1) * nestedInteger.getInteger();
}
else{
sum+=getDepthSum(nestedInteger.getList(),depth+1);
}
}
return sum;
}
}

[LeetCode]364. 加权嵌套序列和 II (DFS)的更多相关文章

  1. [leetcode]364. Nested List Weight Sum II嵌套列表加权和II

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  2. [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  3. LeetCode 364. Nested List Weight Sum II

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...

  4. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  5. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  6. 2016级算法第四次上机-G.ModricWang的序列问题 II

    1021 ModricWang的序列问题II 思路 此题与上一题区别不是很大,只是增加了一个长度限制,当场通过的人数就少了很多. 大体解题过程与上一题相同.区别在于对\(f[]\) 的操作.没有长度限 ...

  7. 【python】Leetcode每日一题-反转链表 II

    [python]Leetcode每日一题-反转链表 II [题目描述] 给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 ...

  8. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  9. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

随机推荐

  1. Html5与CSS3(选择器)

    <!-- 作者:offline 时间:2018-03-21 描述:1.全选择器 *{属性1:属性值2:属性2:属性值2:...:} 2.元素(标签)选择器 标签名{属性1:属性值2:属性2:属性 ...

  2. Azure Kubernetes Service 入门

    一,引言 上一节,我们使用Azure CLI 创建了Azure Resource Group 和 Azure Container Registry 资源,并且将本地的一个叫 “k8s.net.demo ...

  3. gorilla/mux 的学习

    原文链接:gorilla/mux的学习 源代码: package main import ( "encoding/json" "fmt" "githu ...

  4. c++右值引用和转移构造函数

    int &&i = ; //i绑定到了右值1 int b = ; cout << i << endl; //输出1 i = b; cout << i ...

  5. 基于函数的I/O操作(头文件stdio.h)

    基于函数库的I/O是C语言标准库的功能,基于系统级I/O函数实现. 系统级I/O函数对文件的标识是文件描述符,C语言标准库中对文件的标识是指向FILE结构的指针.在头文件cstdio或stdio.h中 ...

  6. python numpy数组操作

    数组的创建 import numpy as np arr1 = np.array([3,10,8,7,34,11,28,72]) arr2 = np.array(((8.5,6,4.1,2,0.7), ...

  7. python3.6和pip3:Ubuntu下安装升级与踩坑之路

    本文以Ubuntu16.x系统为例,演示如何安装python3.6和相应环境.安装Python3的机器必须要能访问外网才能进行如下操作! 1. 安装方式 在Ubuntu下安装python有两种方式: ...

  8. 区块链入门到实战(17)之以太坊(Ethereum) – 是什么

    以太坊的作用:构建基于区块链的分布式应用. 以太坊是什么:可编程的虚拟币. 以太坊(Ethereum)是一个可编程的虚拟币,它是一个基于公共区块链的分布式计算平台,可用于构建基于区块链的分布式应用. ...

  9. Java BigDecimal使用指南

    提起BigDecimal,相信大家都使用过,之所以总结这篇呢,是因为最近发现项目中使用的不是太规范,在某些场景下甚至出现代码抛出异常的情况, 所以就总结了这篇,希望大家在使用时,可以少踩一些坑. 1. ...

  10. npm自定义上传

    npm login # 输入用户名 密码 邮箱 # 要到我们包的根目录里面 npm publish 上传到npm官网命令