[LeetCode] Nested List Weight Sum 嵌套链表权重和
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.
Example 1:
Input: [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1's at depth 2, one 2 at depth 1.
Example 2:
Input: [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27.
这道题定义了一种嵌套链表的结构,链表可以无限往里嵌套,规定每嵌套一层,深度加1,让我们求权重之和,就是每个数字乘以其权重,再求总和。那么我们考虑,由于嵌套层数可以很大,所以我们用深度优先搜索DFS会很简单,每次遇到嵌套的,递归调用函数,一层一层往里算就可以了,我最先想的方法是遍历给的嵌套链表的数组,对于每个嵌套链表的对象,调用getSum函数,并赋深度值1,累加起来返回。在getSum函数中,首先判断其是否为整数,如果是,则返回当前深度乘以整数,如果不是,那么我们再遍历嵌套数组,对每个嵌套链表再调用递归函数,将返回值累加起来返回即可,参见代码如下:
解法一:
class Solution {
public:
int depthSum(vector<NestedInteger>& nestedList) {
int res = ;
for (auto a : nestedList) {
res += getSum(a, );
}
return res;
}
int getSum(NestedInteger ni, int level) {
int res = ;
if (ni.isInteger()) return level * ni.getInteger();
for (auto a : ni.getList()) {
res += getSum(a, level + );
}
return res;
}
};
但其实上面的方法可以优化,我们可以把给的那个嵌套链表的一维数组直接当做一个嵌套链表的对象,然后调用递归函数,递归函数的处理方法跟上面一样,只不过用了个三元处理使其看起来更加简洁了一些:
解法二:
class Solution {
public:
int depthSum(vector<NestedInteger>& nestedList) {
return helper(nestedList, );
}
int helper(vector<NestedInteger>& nl, int depth) {
int res = ;
for (auto a : nl) {
res += a.isInteger() ? a.getInteger() * depth : helper(a.getList(), depth + );
}
return res;
}
};
参考资料:
https://leetcode.com/problems/nested-list-weight-sum/
https://leetcode.com/discuss/94956/2ms-easy-to-understand-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Nested List Weight Sum 嵌套链表权重和的更多相关文章
- [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]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: 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】339. Nested List Weight Sum
原题 Given a nested list of integers, return the sum of all integers in the list weighted by their dep ...
- [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] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- 嵌套列表的加权和 · Nested List Weight Sum
[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...
随机推荐
- 学习SpringMVC——国际化+上传+下载
每个星期一道菜,这个星期也不例外~~~ 一个软件,一个产品,都是一点点开发并完善起来的,功能越来越多,性能越来越强,用户体验越来越好……这每个指标的提高都需要切切实实的做点东西出来,好比,你的这个产品 ...
- javascript的变量作用域--对比js、php和c的for循环
为什么要写这篇文章呢?主要是给自己提个醒,js的水很深,需要小心点儿才能趟过去,更何况自己不是专业人士,那就得更加小心了. 看下面的js代码: <!DOCTYPE html> <ht ...
- 使用 Windows Phone Toolkit 的 Tilt 效果
上一篇文章分享了如何使控件具有摁下的效果(在WindowsPhone中使控件具有Tilt效果),实现方式是在项目中添加新的类文件,其实,如果项目引用了Windows Phone Toolkit,那么就 ...
- JavaScript 垃圾回收
在公司经常会听到大牛们讨论时说道内存泄露神马的,每每都惊羡不已,最近精力主要用在了Web 开发上,读了一下<JavaScript高级程序设计>(书名很唬人,实际作者写的特别好,由浅入深)了 ...
- c# socket
好久没有写CS端代码,今天有空复习一下SOCKET. 功能说明: 1.服务端向客户端发送信息 2.客户端向服务端发送信息 效果如下图: 服务端代码: Socket serverSocket = new ...
- 设计模式(十二)享元模式(Flyweight Pattern)
一.引言 在软件开发过程,如果我们需要重复使用某个对象的时候,如果我们重复地使用new创建这个对象的话,这样我们在内存就需要多次地去申请内存空间了,这样可能会出现内存使用越来越多的情况,这样的问题是非 ...
- ASP.NET用QQ,网易发送邮件以及添加附件
教程:ASP.NET用QQ,网易发送邮件以及添加附件 这是我用QQ邮箱出现的异常: 命令顺序不正确. 服务器响应为:Error: need EHLO and AUTH first !无法从传输连接中读 ...
- springmvc的数据校验
springmvc的数据校验 在Web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对数据进行验证,输入验证分为客户端验证与服务器端验证. 客户端验证主要通过javaScript脚本 ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- Angular的自定义指令以及实例
本文章已收录于: AngularJS知识库 分类: javascript(55) http://www.cnblogs.com/xiaoxie53/p/5058198.html 前面的文章介 ...