← 返回 linkedin 的题目列表Nested Weighted Sum Variation
类型:online_judge
Given a nested list of integers nestedList, each element is either an integer or a list of elements, which may also be integers or other lists. Calculate the weighted sum, where the weight is the square of its depth, and return the sum.
Example
Example 1:
Input: nestedList = [[1,1],2,[1,1]]
Output: 34
Explanation: The weighted depth of each integer is: 1*(2^2) + 1*(2^2) + 2*(1^2) + 1*(2^2) + 1*(2^2) = 4 + 4 + 2 + 4 + 4 = 18.
Example 2:
Input: nestedList = [1,[4,[6]]]
Output: 49
Explanation: The weighted depth of each integer is: 1*(1^2) + 4*(2^2) + 6*(3^2) = 1 + 16 + 36 = 53.
Example
Input
nestedList = [[1,1],2,[1,1]]