← 返回 linkedin 的题目列表Nested List Weight Sum
类型:online_judge
linkedin
Given a nested integer list, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- the list elements may also be integers or other lists. Instead of the regular weighted sum, I suppose to give integers located in deeper level more weight than those on the shallow level.
Input
A nested integer list nestedList. Elements could be either an integer or list.
Output
The weighted sum of all integers.
Example
Input: nestedList = [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1s at weight 2, each 2 * 1 + 2 = 10
Input: nestedList = [1,[4,[6]]]
Output: 17
Explanation: 1 at weight 3, 4 at 2, and 6 at 1, 1 * 3 + 4 * 2 + 6 * 1 = 17
Example
Input
[[1,1],2,[1,1]]