Skip to content

Commit 956cb72

Browse files
committedJan 7, 2025
Add solution #2625
1 parent e794a63 commit 956cb72

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
2621|[Sleep](./2621-sleep.js)|Easy|
384384
2622|[Cache With Time Limit](./2622-cache-with-time-limit.js)|Medium|
385385
2623|[Memoize](./2623-memoize.js)|Medium|
386+
2625|[Flatten Deeply Nested Array](./2625-flatten-deeply-nested-array.js)|Medium|
386387
2626|[Array Reduce Transformation](./2626-array-reduce-transformation.js)|Easy|
387388
2627|[Debounce](./2627-debounce.js)|Medium|
388389
2629|[Function Composition](./2629-function-composition.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 2625. Flatten Deeply Nested Array
3+
* https://leetcode.com/problems/flatten-deeply-nested-array/
4+
* Difficulty: Medium
5+
*
6+
* Given a multi-dimensional array arr and a depth n, return a flattened version of that array.
7+
*
8+
* A multi-dimensional array is a recursive data structure that contains integers or other
9+
* multi-dimensional arrays.
10+
*
11+
* A flattened array is a version of that array with some or all of the sub-arrays removed and
12+
* replaced with the actual elements in that sub-array. This flattening operation should only
13+
* be done if the current depth of nesting is less than n. The depth of the elements in the
14+
* first array are considered to be 0.
15+
*
16+
* Please solve it without the built-in Array.flat method.
17+
*/
18+
19+
/**
20+
* @param {Array} arr
21+
* @param {number} depth
22+
* @return {Array}
23+
*/
24+
var flat = function(arr, n) {
25+
if (n === 0) return arr;
26+
const result = [];
27+
for (let i = 0; i < arr.length; i++) {
28+
result.push(...(Array.isArray(arr[i]) ? flat(arr[i], n - 1) : [arr[i]]));
29+
}
30+
return result;
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.