Given a multi-dimensional array arr and a depth n, return a flattened version of that array.
A multi-dimensional array is a recursive data structure that contains integers or other multi-dimensional arrays.
給予一個多維度的陣列 arr 和一個代表陣列深度的 n,回傳一個展開化的陣列。 一個多為的陣列是包含整數或其他維度陣列的遞迴數據結構。
A flattened array is a version of that array with some or all of the sub-arrays removed and replaced with the actual elements in that sub-array. This flattening operation should only be done if the current depth of nesting is less than n. The depth of the elements in the first array are considered to be 0.
Please solve it without the built-in Array.flat method.
一個展開陣列是該陣列的版本,其中刪除了部分或全部的子陣列,並替換掉該子陣列中的實際元素。 只有在目前巢狀的深度小於 n 的情況下,才應該執行這個展開化操作。 第一個陣列中元素的深度被視為0。 請不要使用內建的 Array.flat 方法來完成它。
Example 1:
Input arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 0 Output [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
Example 2:
Input arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 1 Output [1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
Example 3:
Input arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]] n = 2 Output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
solution:
首先建立一個空的陣列 res,用來存放展開後的結果。
定義ㄧ個內部的遞迴函式 helper 來接受對應的參數,且需還原的巢狀陣列由 0 開始計算到指定的 n。
在 helper 內部遍歷 arr 中的每個元素,注意如果確認陣列的屬性則會回傳 object(console.log(typeof [1, 2, 3]) //object),
以此為遞迴的條件搭配指定的深度來將陣列中的元素一一拆解出來。
Code 1: BigO(n)
var flat = function (arr, n) { const res = [] function helper(arr, depth) { for (const val of arr) { if (typeof val === "object" && depth < n) { helper(val, depth + 1) } else { res.push(val) } } return res; } return helper(arr, 0); };
FlowChart:
Example 1
console.log(flat([1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]], 0)) n = 0 1 number //[ 1 ] 2 number //[ 1, 2 ] 3 number //[ 1, 2, 3 ] [ 4, 5, 6 ] object [ 1, 2, 3, [ 4, 5, 6 ] ] [ 7, 8, [ 9, 10, 11 ], 12 ] object [ 1, 2, 3, [ 4, 5, 6 ], [ 7, 8, [ 9, 10, 11 ], 12 ] ] [ 13, 14, 15 ] object [ 1, 2, 3, [ 4, 5, 6 ], [ 7, 8, [ 9, 10, 11 ], 12 ], [ 13, 14, 15 ] ]
Example 2
console.log(flat([1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]], 1)) n = 1 1 number //[ 1 ] 2 number //[ 1, 2 ] 3 number //[ 1, 2, 3 ] [ 4, 5, 6 ] object 4 number //[ 1, 2, 3, 4 ] 5 number //[ 1, 2, 3, 4, 5 ] 6 number //[ 1, 2, 3, 4, 5, 6 ] [ 7, 8, [ 9, 10, 11 ], 12 ] object 7 number //[ 1, 2, 3, 4, 5, 6, 7 ] 8 number //[ 1, 2, 3, 4, 5, 6, 7, 8 ] [ 9, 10, 11 ] object, depth < n //1 < 1 [ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 10, 11 ] ] 12 number //[ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 10, 11 ], 12 ] [ 13, 14, 15 ] object 13 number //[ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 10, 11 ], 12, 13 ] 14 number //[ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 10, 11 ], 12, 13, 14 ] 15 number //[ 1, 2, 3, 4, 5, 6, 7, 8, [ 9, 10, 11 ], 12, 13, 14, 15 ]
Example 3
console.log(flat([[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]], 2)) n = 2 [ 1, 2, 3 ] object 1 number //[ 1 ] 2 number //[ 1, 2 ] 3 number //[ 1, 2, 3 ] [ 4, 5, 6 ] object 4 number //[ 1, 2, 3, 4 ] 5 number //[ 1, 2, 3, 4, 5 ] 6 number //[ 1, 2, 3, 4, 5, 6 ] [ 7, 8, [ 9, 10, 11 ], 12 ] object 7 number //[ 1, 2, 3, 4, 5, 6, 7 ] 8 number //[ 1, 2, 3, 4, 5, 6, 7, 8 ] [ 9, 10, 11 ] object 9 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 10 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] 11 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ] 12 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] [ 13, 14, 15 ] object 13 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ] 14 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ] 15 number //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]