Reshape the Matrix

 https://leetcode.com/problems/reshape-the-matrix/

https://leetcode.com/problems/reshape-the-matrix/discuss/1317149/C%2B%2B-Easy-Implementation%3A-For-Beginners

class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
        int m=mat.size();//col
        int n=mat[0].size();//row
        if(m*n != r*c){
            return mat;
        }
        if(m==r && n==c){
            return mat;
        }
         vector<vector<int>> ans(r, vector<int>(c));
        for(int i = 0; i < m*n; i++)
            ans[i / c][i % c] = mat[i / n][i % n];
        return ans;
        
    }
};

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!