Advertisement
nikunjsoni

566

Jul 5th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
  4.         int row = mat.size(), col = mat[0].size();
  5.         if(r*c != row*col)
  6.             return mat;
  7.        
  8.         vector<vector<int>> nmat;
  9.         for(int i=0; i<r; i++){
  10.             vector<int> tmp;
  11.             for(int j=0; j<c; j++){
  12.                 int idx = i*c+j;
  13.                 tmp.push_back(mat[idx/col][idx%col]);
  14.             }
  15.             nmat.push_back(tmp);
  16.         }
  17.         return nmat;
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement