[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.
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
类似题目:
参考资料:
https://leetcode.com/problems/nested-list-weight-sum-ii/
https://leetcode.com/problems/nested-list-weight-sum-ii/discuss/83655/JAVA-AC-BFS-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Nested List Weight Sum II 嵌套链表权重和之二的更多相关文章
- [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嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- 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. ...
- LeetCode Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- [LeetCode] Nested List Weight Sum 嵌套链表权重和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- 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. ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://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 ...
随机推荐
- CentOS7使用firewalld打开关闭防火墙与端口
1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disab ...
- Base-64 字符数组或字符串的长度无效等问题解决方案
项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...
- .Net语言 APP开发平台——Smobiler学习日志:Poplist控件的正确打开方式以及如何快速实现
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...
- dbutils基本使用
dbutils的查询,主要用到的是query方法,增加,修改和删除都是update方法,update方法就不讲了 只要创建ResultSetHandler接口不同的实现类对象就可以得到想要的查询结果, ...
- java变量类型
一.局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它的方 ...
- VS2010中dumpbin工具的使用
用VS2010生成的.obj文件..lib库..dll库..exe执行文件,如果想查看其中这些文件或库包含了哪些函数以及相关的信息(符号清单),可以通过VS2010自带的dumpbin工具来完成. d ...
- getRequestDispatcher()与sendRedirect()的区别
1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ; response.sendRedirect()是重新定向,前后页面不是一个request ...
- Css3新特性应用之视觉效果
一.单侧阴影 box-shadow属性的应用,格式:h-shadow v-shadow blur spread color inset属性取值介绍 h-sahdow:水平阴影的位置,允许负值 v-sh ...
- js 轮播效果
<!--图片轮播 Start--> <div class="pics-ul"> ...
- 使用jquery.qrcode生成二维码(转)
jQuery 的 qrcode 插件就可以在浏览器端生成二维码图片. 这个插件的使用非常简单: 1.首先在页面中加入jquery库文件和qrcode插件. <script type=" ...