← 返回 linkedin 的题目列表Sum of a Multidimensional Array
类型:online_judge
Implement a function that returns the sum of all numeric elements in a multidimensional array.
The input is a nested structure (e.g., a 2D array is an array of arrays, a 3D array is an array of arrays of arrays, etc.). The depth is not fixed.
Requirements:
Return the sum of all numeric elements
Handle empty arrays correctly
Support arbitrary nesting depth
Examples:
Input: [[1, 2], [3, 4]] Output: 10
Input: [1, [2, [3]], 4] Output: 10
Constraints (assumed):
Total number of items (including nested containers) ≤ 2 * 10^5
Numeric elements fit in 32-bit signed integer range
State the time and space complexity and provide runnable code.
Example
Input
[[1,2],[3,4]]
Output
10