File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 383
383
2621|[ Sleep] ( ./2621-sleep.js ) |Easy|
384
384
2622|[ Cache With Time Limit] ( ./2622-cache-with-time-limit.js ) |Medium|
385
385
2623|[ Memoize] ( ./2623-memoize.js ) |Medium|
386
+ 2625|[ Flatten Deeply Nested Array] ( ./2625-flatten-deeply-nested-array.js ) |Medium|
386
387
2626|[ Array Reduce Transformation] ( ./2626-array-reduce-transformation.js ) |Easy|
387
388
2627|[ Debounce] ( ./2627-debounce.js ) |Medium|
388
389
2629|[ Function Composition] ( ./2629-function-composition.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments