[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 ...
随机推荐
- 【AHOI 2015】 小岛 - 最短路
描述 西伯利亚北部的寒地,坐落着由 N 个小岛组成的岛屿群,我们把这些小岛依次编号为 1 到 N . 起初,岛屿之间没有任何的航线.后来随着交通的发展,逐渐出现了一些连通两座小岛的航线.例如增加一条在 ...
- LNK2005 连接错误解决办法 2009-10-30 12:13
nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@ ...
- 技术分享丨数据仓库的建模与ETL实践技巧
摘要:如何搭建数据仓库,在这个过程中都应该遵循哪些方法和原则,项目实践中有哪些技巧. 一.数据仓库的“心脏” 首先来谈谈数据模型.模型是现实世界特征的模拟和抽象,比如地图.建筑设计沙盘,飞机模型等等. ...
- 如何去除List集合中的重复元素?
一.问题由来 在实际开发的时候,我们经常会碰到这么一个问题:一个集合容器里面有很多重复的对象,里面的对象没有主键,或者说忽略主键,根据业务的需求,我们需要根据条件筛选出没有重复的对象. 二.去重操作 ...
- 面试官最爱的 volatile 关键字,这些问题你都搞懂了没?
前言 volatile相关的知识点,在面试过程中,属于基础问题,是必须要掌握的知识点,如果回答不上来会严重扣分的哦. volatile关键字基本介绍 volatile可以看成是synchronized ...
- unity探索者之socket传输protobuf字节流(三)
版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/6986474.html 上一篇讲到了数据的处理,这一篇主要讲使用多线程收发消息 // ...
- 笔记:安装VM Tools、vim编辑器、压缩包、Linux用户管理
一.VM Tools安装 1.作用:方便我们在虚拟机和宿主机之间复制数据或移动文件等. 2.安装步骤: step1:在菜单栏找到虚拟机---->找到安装vm tools ,点击: step2:进 ...
- 【测试技术分享】Liunx常用操作命令集合
Linux命令 ls 查看文件目录内容 ls -lha l:详细信息 h:人性化显示 a:查看隐藏目录 ls -目录名 查看指定目录 d rwx rwx rwx d:文件夹 -:文件 rwx:拥有 ...
- c++: internal compiler error: Killed (program cc1plus)
转自https://blog.csdn.net/qq_27148893/article/details/88936044 这是在开发板上编译opencv的时候报了一个错,主要是在编译过程中,内存不够造 ...
- 微信小程序发送订阅消息(之前是模板消息)
之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息. 小程序端 index.wxml <button bindtap="send"> ...