Problem List

Convert 1D Array Into 2D Array

May 31, 2025Go array, matrix, simulationeasy

Problem

Reflections

It's just math

Go Solution
func construct2DArray(original []int, m int, n int) [][]int {
    if m * n != len(original) {
        return [][]int{}
    }

    res := [][]int{}
    for i := 0; i < m; i++ {
        res = append(res, original[n*i:i*n+n])
    }
    return res
}

LeetCode Problem Link