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为nums列数
        vector<vector<int>> res(r, vector<int>(c));  //讲真这个定义不太搞懂??
        if(m*n!=r*c) return nums;
        for(int i=0;i<r;i++)               //目标是res, 所以要按照r c循环打印。
            for(int j=0;j<c;j++)
            {
                int k=c*i+j;              //拉直
                res[i][j]=nums[k/n][k%n];  //取行数(除以行数n取整),列数(取余)
            }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zb-ml/p/8748084.html

时间: 2024-11-13 04:30:19

leetcode 566 Reshape the Matrix 重塑矩阵的相关文章

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 - 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

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

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

[LeetCode] 566. Reshape the Matrix_Easy

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