← 返回 meta 的题目列表Nested List Weight Sum
类型:online_judge
Implement a function to calculate the weighted sum of a nested list. First, you need to define a NestedInteger class with the following interfaces:
isInteger(): Returns true if this NestedInteger holds a single integer, rather than a nested list.
getInteger(): Returns the single integer that this NestedInteger holds, if it holds a single integer. Returns None if this NestedInteger holds a nested list.
getList(): Returns the nested list that this NestedInteger holds, if it holds a nested list. Returns None if this NestedInteger holds a single integer.
Implement a function depthSum that takes a List[NestedInteger] as parameter and returns the total sum as the depth increases. The deeper the integer, the more weight it gets. Here are the test cases:
Input: [[1,1],2,[1,1]], Output: 10. Explanation: 1 * 3 + 1 * 3 + 2 * 2 + 1 * 3 + 1 * 3 = 10.
Input: [1,[4,[6]]], Output: 27. Explanation: 1 * 1 + 4 * 2 + 6 * 3 = 27.
Assume the maximum depth does not exceed 50, and the integer range is between -100 and 100.
Example
Input
[[1,1],2,[1,1]]