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 + "\t" + i + "," + j);
            retArr[rIndex][cIndex] = nums[i][j];
            // System.out.println(nums[i][j]);
        }
    }
    return retArr;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9255658.html

时间: 2024-11-09 10:08:50

566. Reshape the Matrix - LeetCode的相关文章

LeetCode 566. Reshape the Matrix (重塑矩阵)

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

LeetCode - 566. Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

566. Reshape the Matrix

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #323333; background-color: #f5f5f5 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px

566. Reshape the Matrix矩阵重排

[抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and

leetcode 566 Reshape the Matrix 重塑矩阵

参考:https://www.cnblogs.com/grandyang/p/6804753.html 注意:复习容器的定义方法?? class Solution { public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int m=nums.size();//m为nums行数 int n=nums[0].size(); //n为num

[LeetCode] Reshape the Matrix 矩阵重塑

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

leetcode算法:Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

(leetcode题解)Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

[LeetCode] Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep