Answers for "reshape the matrix"

0

reshape the matrix

vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
    int n = mat.size();
    int m = mat[0].size();
    if(n*m!=r*c)return mat;
    //Create a matrix of size r*c
    vector<vector<int>> ans(r, vector<int>(c,0));
    int x=0,y=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            ans[x][y] = mat[i][j];
            y++;
            if(y==c){
                y=0;
                x++;
            }
        }
    }
    return ans;
}
Posted by: Guest on February-02-2022

Code answers related to "reshape the matrix"

Browse Popular Code Answers by Language