566. Reshape the Matrix - LeetCode

Posted okokabcd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了566. Reshape the Matrix - LeetCode相关的知识,希望对你有一定的参考价值。

Question

566. Reshape the Matrix

技术分享图片

Solution

题目大意:给一个二维数组,将这个二维数组转换为r行c列

思路:构造一个r行c列的二维数组,遍历给出二给数组nums,合理转换原数组和目标数组的行列转换。

Java实现:

public int[][] matrixReshape(int[][] nums, int r, int c) {
    int originalR = nums.length;
    int originalC = nums[0].length;
    if (originalR * originalC < r * c) {
        return nums;
    }
    int[][] retArr = new int[r][c];
    int count = -1;
    for (int i = 0; i < originalR; i++) {
        for (int j = 0; j < originalC; j++) {
            count++;
            int rIndex = count / c;
            int cIndex = count % c;
            // System.out.println(rIndex + "," + cIndex + "	" + i + "," + j);
            retArr[rIndex][cIndex] = nums[i][j];
            // System.out.println(nums[i][j]);
        }
    }
    return retArr;
}

以上是关于566. Reshape the Matrix - LeetCode的主要内容,如果未能解决你的问题,请参考以下文章

566. Reshape the Matrix

566. Reshape the Matrix

566. Reshape the Matrix

566. Reshape the Matrix

566. Reshape the Matrix

566. Reshape the Matrix