
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
History is littered with hundreds of conflicts over the future of a community, group, location or business that were "resolved" when one of the parties stepped ahead and destroyed what was there. With the original point of contention destroyed, the debates would fall to the wayside. Archive Team believes that by duplicated condemned data, the conversation and debate can continue, as well as the richness and insight gained by keeping the materials. Our projects have ranged in size from a single volunteer downloading the data to a small-but-critical site, to over 100 volunteers stepping forward to acquire terabytes of user-created data to save for future generations.
The main site for Archive Team is at archiveteam.org and contains up to the date information on various projects, manifestos, plans and walkthroughs.
This collection contains the output of many Archive Team projects, both ongoing and completed. Thanks to the generous providing of disk space by the Internet Archive, multi-terabyte datasets can be made available, as well as in use by the Wayback Machine, providing a path back to lost websites and work.
Our collection has grown to the point of having sub-collections for the type of data we acquire. If you are seeking to browse the contents of these collections, the Wayback Machine is the best first stop. Otherwise, you are free to dig into the stacks to see what you may find.
The Archive Team Panic Downloads are full pulldowns of currently extant websites, meant to serve as emergency backups for needed sites that are in danger of closing, or which will be missed dearly if suddenly lost due to hard drive crashes or server failures.
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:
Example 2:
这道题是之前那道 Nested List Weight Sum 的拓展,与其不同的是,这里的深度越深,权重越小,和之前刚好相反。但是解题思路没有变,还可以用 DFS 来做,由于遍历的时候不知道最终的 depth 有多深,则不能遍历的时候就直接累加结果,博主最开始的想法是在遍历的过程中建立一个二维数组,把每层的数字都保存起来,然后最后知道了 depth 后,再来计算权重和,比如题目中给的两个例子,建立的二维数组分别为:
[[1,1],2,[1,1]]:
1 1 1 1
2
[1,[4,[6]]]:
1
4
6
这样我们就能算出权重和了,参见代码如下:
解法一:
其实上面的方法可以简化,由于每一层的数字不用分别保存,每个数字分别乘以深度再相加,跟每层数字先相加起来再乘以深度是一样的,这样只需要一个一维数组就可以了,只要把各层的数字和保存起来,最后再计算权重和即可:
解法二:
下面这个方法就比较巧妙了,由史蒂芬大神提出来的,这个方法用了两个变量 unweighted 和 weighted,非权重和跟权重和,初始化均为0,然后如果 nestedList 不为空开始循环,先声明一个空数组 nextLevel,遍历 nestedList 中的元素,如果是数字,则非权重和加上这个数字,如果是数组,就加入 nextLevel,这样遍历完成后,第一层的数字和保存在非权重和 unweighted 中了,其余元素都存入了 nextLevel 中,此时将 unweighted 加到 weighted 中,将 nextLevel 赋给 nestedList,这样再进入下一层计算,由于上一层的值还在 unweighted 中,所以第二层计算完将 unweighted 加入 weighted 中时,相当于第一层的数字和被加了两次,这样就完美的符合要求了,这个思路又巧妙又牛B,大神就是大神啊,参见代码如下:
解法三:
下面这种算法是常规的 BFS 解法,利用上面的建立两个变量 unweighted 和 weighted 的思路,大体上没什么区别:
解法四:
Github 同步地址:
#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 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: