题目

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

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

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

也就是说,在本题中,叶子的权重为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. 基于token的会话保持机制

    session简介 做过Web开发的程序员应该对Session都比较熟悉,Session是一块保存在服务器端的内存空间,一般用于保存用户的会话信息. 用户通过用户名和密码登陆成功之后,服务器端程序会在 ...

  2. 【Spring注解驱动开发】使用@Autowired@Qualifier@Primary三大注解自动装配组件,你会了吗?

    写在前面 [Spring专题]停更一个多月,期间在更新其他专题的内容,不少小伙伴纷纷留言说:冰河,你[Spring专题]是不是停更了啊!其实并没有停更,只是中途有很多小伙伴留言说急需学习一些知识技能, ...

  3. 【luogu1352】没有上司的舞会 - 树形DP

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  4. 总结关于Ubuntu 安装 Docker 配置相关问题及解决方法

    总结关于Ubuntu 安装 Docker 配置相关问题及解决方法 Tomcat 示例 软件镜像(xx安装程序)----运行镜像----产生一个容器(正在运行的软件,运行的xx): 步骤: 1.搜索镜像 ...

  5. 实战分享丨MySQL 与Django版本匹配相关经验

    摘要:关于MySQL 与Django版本匹配相关知识的经验分享. run: (env) E:\PythonPro\PyDjangoProDemo011\xuanyuaniotpro>python ...

  6. 笔记:CSS基础

    一.CSS(层叠式样式表),决定页面怎么显示元素 1.引入方式: 行内样式,在当前标签元素中直接使用 style 的属性. 内嵌方式,在<head>中写样式: 外链式,<link&g ...

  7. ubuntu apt 相关命令

    sudo apt-get update  更新源sudo apt-get install package 安装包sudo apt-get remove package 删除包sudo apt-cach ...

  8. html的鼠标双击,单击,移上,离开,事件捕捉,JavaScript

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. selenium定位方法(一)

    selenium定位方法-(一) 1.定位页面元素的方式(By类的方法) 1)id定位:通过页面元素的id属性值来定位一个页面元素       注意事项:如果每次刷新网页之后元素的id属性值都不同,说 ...

  10. 通过DatabaseMetaData数据库元信息类,获取特定数据库的元信息

    数据库版本:mysql8.0.18 ide:idea 2019.3 可以看到代码中连接的数据库为course_select,是一个学生的选课系统的数据库 然后通过DatabaseMetaData的ge ...