[LeetCode] Nested List Weight Sum 嵌套链表权重和
2016-03-31 10:57
501 查看
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:
Given the list
Example 2:
Given the list
这道题定义了一种嵌套链表的结构,链表可以无限往里嵌套,规定每嵌套一层,深度加1,让我们求权重之和,就是每个数字乘以其权重,再求总和。那么我们考虑,由于嵌套层数可以很大,所以我们用深度优先搜索DFS会很简单,每次遇到嵌套的,递归调用函数,一层一层往里算就可以了,我最先想的方法是遍历给的嵌套链表的数组,对于每个嵌套链表的对象,调用getSum函数,并赋深度值1,累加起来返回。在getSum函数中,首先判断其是否为整数,如果是,则返回当前深度乘以整数,如果不是,那么我们再遍历嵌套数组,对每个嵌套链表再调用递归函数,将返回值累加起来返回即可,参见代码如下:
解法一:
但其实上面的方法可以优化,我们可以把给的那个嵌套链表的一维数组直接当做一个嵌套链表的对象,然后调用递归函数,递归函数的处理方法跟上面一样,只不过用了个三元处理使其看起来更加简洁了一些:
解法二:
参考资料:
https://leetcode.com/discuss/94956/2ms-easy-to-understand-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Example 1:
Given the list
[[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)
Example 2:
Given the list
[1,[4,[6]]], return 27. (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 = 0; for (auto a : nestedList) { res += getSum(a, 1); } return res; } int getSum(NestedInteger ni, int level) { int res = 0; if (ni.isInteger()) return level * ni.getInteger(); for (auto a : ni.getList()) { res += getSum(a, level + 1); } return res; } };
但其实上面的方法可以优化,我们可以把给的那个嵌套链表的一维数组直接当做一个嵌套链表的对象,然后调用递归函数,递归函数的处理方法跟上面一样,只不过用了个三元处理使其看起来更加简洁了一些:
解法二:
class Solution { public: int depthSum(vector<NestedInteger>& nestedList) { return helper(nestedList, 1); } int helper(vector<NestedInteger>& nl, int depth) { int res = 0; for (auto a : nl) { res += a.isInteger() ? a.getInteger() * depth : helper(a.getList(), depth + 1); } return res; } };
参考资料:
https://leetcode.com/discuss/94956/2ms-easy-to-understand-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
相关文章推荐
- 解决inline-block属性带来的标签间间隙问题
- 5.linux内核模块基础,内核模块学习
- SetLocalTime设置本地时间
- 页面刷新两次为什么取不到值
- jquery之抛物线式购物车
- AndroidUI设计思想:程序易用性
- 代码篇——Datagrid客户端假分页的代码
- java23中设计模式——结构模式——Adaptor(适配器) 3ff8
- osv main启动流程分析
- 多线程原理
- spp_solver
- iOS - 自定义启动图
- 大话数据结构读书笔记系列(五)串
- lightoj 1169 - Monkeys on Twin Tower 简单DP
- 基于 openssl + vs2013 https客户端 Demo
- JAVA多线程机制有两种实现方式
- 快的打车架构实践
- android中的帧动画
- 基本数据类型:8种基本类型、对应的包装类、引用数据类型及与String 之间的转换。
- SEO优化方式