Skip to content

Latest commit

 

History

History
20 lines (18 loc) · 489 Bytes

2022.将一维数组转变为二维数组.md

File metadata and controls

20 lines (18 loc) · 489 Bytes

方法一:模拟

class Solution {
    public int[][] construct2DArray(int[] original, int m, int n) {
        if (m * n != original.length) return new int[0][0];
        int rowIdx = 0, colIdx = 0;
        int[][] res = new int[m][n];
        for (int i = 0; i < original.length; i++) {
            res[rowIdx][colIdx] = original[i];
            if (++colIdx == n) {
                colIdx = 0;
                rowIdx++;
            }
        }
        return res;
    }
}