博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode] 364. Nested List Weight Sum II 解题报告
阅读量:3673 次
发布时间:2019-05-21

本文共 2786 字,大约阅读时间需要 9 分钟。

题目链接: https://leetcode.com/problems/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  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:
Given the list [[1,1],2,[1,1]], return 8. (four 1's at depth 1, one 2 at depth 2)

Example 2:
Given the list [1,[4,[6]]], return 17. (one 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17)

思路: 和之前的一题不同在于这题结点的权值是越靠近根部越高, 而在叶子结点则越低. 所以在找到最大深度之前你是无法计算的, 也就是说我们可以将每个值和他的深度边搜索边存起来, 并且计算最大深度是多少. 最后将所有的点遍历完之后就得到了所有需要的信息. 这时就可以根据最大深度和每一个点的深度来计算加权值了.

代码如下:

/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { *   public: *     // Return true if this NestedInteger holds a single integer, rather than a nested list. *     bool isInteger() const; * *     // Return the single integer that this NestedInteger holds, if it holds a single integer *     // The result is undefined if this NestedInteger holds a nested list *     int getInteger() const; * *     // Return the nested list that this NestedInteger holds, if it holds a nested list *     // The result is undefined if this NestedInteger holds a single integer *     const vector
&getList() const; * }; */class Solution {public: void DFS(vector
& nestedList, int depth) { maxDepth = max(maxDepth, depth); for(auto val: nestedList) { if(!val.isInteger()) DFS(val.getList(), depth+1); else nums.push_back(make_pair(val.getInteger(), depth)); } } int depthSumInverse(vector
& nestedList) { if(nestedList.size() ==0) return 0; DFS(nestedList, 1); for(auto val: nums) result+= (maxDepth-val.second+1)*val.first; return result; }private: vector
> nums; int maxDepth = 0, result = 0;};

还有一种更为简单的方法,利用每一层将当前整数加起来,然后往后遍历多一层就将前面已经加过的数再加一遍.非常巧妙!

class Solution {public:    int depthSumInverse(vector
& nestedList) { int unweighted = 0, weighted = 0; while(nestedList.size()) { vector
nextLevel; for(auto val: nestedList) { if(val.isInteger()) unweighted += val.getInteger(); else for(auto v: val.getList()) nextLevel.push_back(v); } weighted += unweighted; nestedList = nextLevel; } return weighted; }};

转载地址:http://zglbn.baihongyu.com/

你可能感兴趣的文章
Python之旅12:线程、进程和协程
查看>>
MySQL基础(一)
查看>>
urllib的简单使用与HTTP 错误的列表
查看>>
SQLite
查看>>
HTML基础
查看>>
CSS基础篇
查看>>
Redis基础
查看>>
Javascript
查看>>
DOM操作
查看>>
jQuery
查看>>
MYSQL进阶(二):
查看>>
Linux网络编程学习总结
查看>>
Linux进程和线程
查看>>
ubuntu18.04之安装星际译王
查看>>
2018年deepin-wine QQ、微信等最完美解决方案
查看>>
服务端应用安全——加密算法与随机数
查看>>
zookeeper学习笔记
查看>>
设计模式实践与总结(持续更新中)
查看>>
TCP拆包、滑动窗口通俗理解
查看>>
JavaFX开发使用经验
查看>>