LeetCode 2022. Convert 1D Array Into 2D Array in F#
URL
2022. Convert 1D Array Into 2D Array
Code
let construct2DArray (original: int list) (m: int) (n: int) : int[,] =
let rec construct2DArray' original (acc: int[,]) =
match original with
| [] -> acc
| (i, h) :: t ->
acc.[i / n, i % n] <- h
construct2DArray' t acc
if List.length original <> m * n then
Array2D.zeroCreate 0 0
else
let acc = Array2D.zeroCreate m n
construct2DArray' (List.indexed original) acc