[LeetCode]364. 加权嵌套序列和 II (DFS)
题目
给一个嵌套整数序列,请你返回每个数字在序列中的加权和,它们的权重由它们的深度决定。
序列中的每一个元素要么是一个整数,要么是一个序列(这个序列中的每个元素也同样是整数或序列)。
与 前一个问题 不同的是,前一题的权重按照从根到叶逐一增加,而本题的权重从叶到根逐一增加。
也就是说,在本题中,叶子的权重为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)的更多相关文章
- [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. ...
- [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. ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- [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. ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 2016级算法第四次上机-G.ModricWang的序列问题 II
1021 ModricWang的序列问题II 思路 此题与上一题区别不是很大,只是增加了一个长度限制,当场通过的人数就少了很多. 大体解题过程与上一题相同.区别在于对\(f[]\) 的操作.没有长度限 ...
- 【python】Leetcode每日一题-反转链表 II
[python]Leetcode每日一题-反转链表 II [题目描述] 给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- [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 ...
随机推荐
- 实体类转xml
看项目中需要实体类转xml,大家是拼接的.感觉可以利用反射实现.于是写了下 代码如下 package com.kevin.util; import org.springframework.util.O ...
- 实现直方图均衡化(java+opencv)
什么是直方图均衡化? 直方图均衡化是一种简单有效的图像增强技术,通过改变图像的直方图来改变图像中各像素的灰度,主要用于增强动态范围偏小的图像的对比度.原始图像由于其灰度分布可能集中在较窄的区间,造成图 ...
- JavaScript学习系列博客_4_JavaScript中的数据类型
JavaScript中有6种数据类型 一.基本数据类型 - String 字符串 JS中的字符串需要使用引号引起来双引号或单引号都行 但是要注意的是某种引号嵌套使用的话,需要加上 \ 转义.比如说我们 ...
- get customer attribute option
Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAl ...
- Magento add product attribute and assign to all group
$attributes = array( 'product_type' => array( 'type' => 'int', 'input' => 'select', 'source ...
- Leecode统计子串个数(java)
/** 获取一个字符串在另一个字符串中出现的次数.判断str2在str1中出现的次数 */ public class StringExer2 { public static void main(Str ...
- MS建模mmt
一.流程:1.定义好每个原子力场类型:Edit Sets(分组) 2.加H(一般是仅与Al-O八面体中的Al相连的O原子).给H分组 3.“make P1”.校核化学式(正确与否) 4.扩成超晶胞(N ...
- MySQL进阶之MySQL索引以及索引优化
本文配合B站学习视频BV1es411u7we使用效果更佳. 1. MySQL版本 主流版本:5.x版 5.0 - 5.1:早期产品的延续,升级维护 5.4 - 5.x:MySQL整合了三方公司的新存储 ...
- idea创建mybatis的逆向工程:generator
1在工程的目录下配置mybatis-generator插件 <build> <plugins> <!-- mybatis逆向工程 --> <plugin> ...
- android开发之java代码中字符串对比忽略大小写。java程序员必回,可用来比对验证码等问题
比如:字符串a与字符串B对比,只需调用 a.equalsIgnoreCase(B);即可.很简单的一个方法.忽略大小写.