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

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.

Example 1:

Input: [[1,1],2,[1,1]]
Output: 8
Explanation: Four 1's at depth 1, one 2 at depth 2.

Example 2:

Input: [1,[4,[6]]]
Output: 17
Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17.

这道题是之前那道 Nested List Weight Sum 的拓展,与其不同的是,这里的深度越深,权重越小,和之前刚好相反。但是解题思路没有变,还可以用 DFS 来做,由于遍历的时候不知道最终的 depth 有多深,则不能遍历的时候就直接累加结果,博主最开始的想法是在遍历的过程中建立一个二维数组,把每层的数字都保存起来,然后最后知道了 depth 后,再来计算权重和,比如题目中给的两个例子,建立的二维数组分别为:

[[1,1],2,[1,1]]:

1 1 1 1
2

[1,[4,[6]]]:

1
4
6

这样我们就能算出权重和了,参见代码如下:

解法一:

class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int res = ;
vector<vector<int>> all;
for (auto &a : nestedList) {
helper(a, , all);
}
for (int i = (int)all.size() - ; i >= ; --i) {
for (int j = ; j < all[i].size(); ++j) {
res += all[i][j] * ((int)all.size() - i);
}
}
return res;
}
void helper(NestedInteger& ni, int depth, vector<vector<int>>& all) {
vector<int> t;
if (depth < all.size()) t = all[depth];
else all.push_back(t);
if (ni.isInteger()) {
t.push_back(ni.getInteger());
all[depth] = t;
} else {
for (auto &a : ni.getList()) {
helper(a, depth + , all);
}
}
}
};

其实上面的方法可以简化,由于每一层的数字不用分别保存,每个数字分别乘以深度再相加,跟每层数字先相加起来再乘以深度是一样的,这样只需要一个一维数组就可以了,只要把各层的数字和保存起来,最后再计算权重和即可:

解法二:

class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int res = ;
vector<int> v;
for (auto &a : nestedList) {
helper(a, , v);
}
for (int i = (int)v.size() - ; i >= ; --i) {
res += v[i] * ((int)v.size() - i);
}
return res;
}
void helper(NestedInteger& ni, int depth, vector<int>& v) {
if (depth >= v.size()) v.resize(depth + );
if (ni.isInteger()) {
v[depth] += ni.getInteger();
} else {
for (auto &a : ni.getList()) {
helper(a, depth + , v);
}
}
}
};

下面这个方法就比较巧妙了,由史蒂芬大神提出来的,这个方法用了两个变量 unweighted 和 weighted,非权重和跟权重和,初始化均为0,然后如果 nestedList 不为空开始循环,先声明一个空数组 nextLevel,遍历 nestedList 中的元素,如果是数字,则非权重和加上这个数字,如果是数组,就加入 nextLevel,这样遍历完成后,第一层的数字和保存在非权重和 unweighted 中了,其余元素都存入了 nextLevel 中,此时将 unweighted 加到 weighted 中,将 nextLevel 赋给 nestedList,这样再进入下一层计算,由于上一层的值还在 unweighted 中,所以第二层计算完将 unweighted 加入 weighted 中时,相当于第一层的数字和被加了两次,这样就完美的符合要求了,这个思路又巧妙又牛B,大神就是大神啊,参见代码如下:

解法三:

class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int unweighted = , weighted = ;
while (!nestedList.empty()) {
vector<NestedInteger> nextLevel;
for (auto a : nestedList) {
if (a.isInteger()) {
unweighted += a.getInteger();
} else {
nextLevel.insert(nextLevel.end(), a.getList().begin(), a.getList().end());
}
}
weighted += unweighted;
nestedList = nextLevel;
}
return weighted;
}
};

下面这种算法是常规的 BFS 解法,利用上面的建立两个变量 unweighted 和 weighted 的思路,大体上没什么区别:

解法四:

class Solution {
public:
int depthSumInverse(vector<NestedInteger>& nestedList) {
int unweighted = , weighted = ;
queue<vector<NestedInteger>> q;
q.push(nestedList);
while (!q.empty()) {
int size = q.size();
for (int i = ; i < size; ++i) {
vector<NestedInteger> t = q.front(); q.pop();
for (auto a : t) {
if (a.isInteger()) unweighted += a.getInteger();
else if (!a.getList().empty()) q.push(a.getList());
}
}
weighted += unweighted;
}
return weighted;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/364

类似题目:

Nested List Weight Sum

Array Nesting

参考资料:

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

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83655/JAVA-AC-BFS-solution

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83641/No-depth-variable-no-multiplication

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/114195/Java-one-pass-DFS-solution-mathematically

https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83649/Share-my-2ms-intuitive-one-pass-no-multiplication-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二的更多相关文章

  1. [LeetCode] Nested List Weight Sum 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嵌套列表加权和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】364. Nested List Weight Sum II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  6. LeetCode 339. Nested List Weight Sum (嵌套列表重和)$

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

  7. 364. Nested List Weight Sum II 大小反向的括号加权求和

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

  8. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...

  9. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

随机推荐

  1. git 创建标签 tag

    1. git tag <name>就可以打一个新标签 加上-a参数来创建一个带备注的tag,备注信息由-m指定.如果你未传入-m则创建过程系统会自动为你打开编辑器让你填写备注信息. git ...

  2. QGIS中查看PostGIS空间数据库中的影像

    在QGIS中的Browser中是无法显示PostGIS空间数据库中的影像 要找到影像显示打开"Database" –> "DB Manager" 右击-- ...

  3. 在Linux系统中运行并简单的测试RabbitMq容器

    以前使用的是Windows下面的RabbitMq,需要先安装 Erlang的语言环境等,这次直接在Linux中的Docker容器来测试一下 1:docker配置RabbitMq的指令 docker r ...

  4. DevExpress的TreeList的常用属性设置以及常用事件

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  5. 百度地图分布图(百度地图api司机位置实时定位分布图)

    就类似于我们使用共享单车app的时候,可以看到我们周围的空闲单车分布.e代驾在后台管理系统需求里也有此功能,目的是为了实时看到目标城市下的所有司机状态. 一.controller //controll ...

  6. java斐波那契数列的顺序输出

    斐波那契数列,即1.1.2.3.5......,从第三个数开始包括第三个数,都为这个数的前两个数之和,而第一第二个数都为1. 下面是java输出斐波那契数列的代码: import java.util. ...

  7. libtool编译

    1.充分利用共享库的能力.libtool 是一个通用库支持脚本 2.我们可以认为libtool是gcc的一个抽象,也就是说,它包装了gcc或者其他的任何编译器,用户无需知道细节,只要告诉libtool ...

  8. Scrum 冲刺第二篇

    我们是这次稳了队,队员分别是温治乾.莫少政.黄思扬.余泽端.江海灵 一.会议 1.1  26号站立式会议照片: 1.2  昨天已完成的事情 团队成员 任务内容 黄思扬 Web 端首页.内容管理页开发. ...

  9. 章节十一、6-操作集合里面的Web元素

    以下演示操作以该网站为例:https://learn.letskodeit.com/p/practice 一.如何操作多个元素(把多个元素放到集合容器中然后操作它们) 列如我们需要操作这些单选框:: ...

  10. 爬虫---Beautiful Soup 反反爬虫事例

    前两章简单的讲了Beautiful Soup的用法,在爬虫的过程中相信都遇到过一些反爬虫,如何跳过这些反爬虫呢?今天通过知乎网写一个简单的反爬中 什么是反爬虫 简单的说就是使用任何技术手段,阻止别人批 ...