[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 ...
随机推荐
- JavaScript基础-04-对象、函数
对象 1. 对象:使用基本数据类型的数据,创建的变量都是独立的,不能成为一个整体 对象属于一个复合数据类型,在对象中可以保存多个不同数据类型的属性. 对象的分类: (1)内建对象:由ES ...
- 简谈DFS
所谓DFS就是“不撞南墙不回头”的一种搜索.其时间复杂度为O(V+E). 能算出从起点到终点的全部路径,在算法执行的过程中需要一个visit[vi]数组来维护每个结点的访问情况,这样就能避免重复访问. ...
- kafka-clients 1.0 内部响应接口文档
AddOffsetsToTxnResponse version:0 name type defaultValue docString throttle_time_ms INT32 0 Duration ...
- leetcode刷题记录——树
递归 104.二叉树的最大深度 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- SCAFFOLD: Stochastic Controlled Averaging for On-Device Federated Learning
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 以下是对本文关键部分的摘抄翻译,详情请参见原文. arXiv:1910.06378v1 [cs.LG] 14 Oct 2019 Abstr ...
- 【译】Database Profiling with Visual Studio
你是否在排查运行缓慢的 web 应用程序时怀疑是数据库层造成的?以前排查数据库层需要特定的工具,现在可以使用 Visual Studio 的 Performance Explorer 中的数据库分析工 ...
- antd-vue中table行高亮效果实现
[方式一]:通过设置customRow达到目的,点击时遍历所有行设置为正常颜色,把当前行设置为特殊颜色(高亮色) HTML: <a-table ref="table" siz ...
- Git使用感悟
前言 分支介绍 我们现在开发的分支一般是这样的(基于上面那张图片的): master:上线用的 dev:开发用的 featature_xxx:开发用的 test:测试用的 hotfix:修复bug的 ...
- CPU有个禁区,内核权限也无法进入!
神秘项目 我是CPU一号车间的阿Q,是的,我又来了. 最近一段时间,我几次下班约隔壁二号车间虎子,他都推脱没有时间,不过也没看见他在忙个啥. 前几天,我又去找他,还是没看到他人,却意外地在他桌上发现了 ...
- Python 装饰器填坑指南 | 最常见的报错信息、原因和解决方案
本文为霍格沃兹测试学院学员学习笔记. Python 装饰器简介 装饰器(Decorator)是 Python 非常实用的一个语法糖功能.装饰器本质是一种返回值也是函数的函数,可以称之为“函数的函数”. ...