leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0566.cpp (522B)


      1 class Solution {
      2   public:
      3     vector<vector<int>> matrixReshape(vector<vector<int>> &mat, int r, int c) {
      4         int n = mat.size(), m = mat[0].size();
      5         if (m * n != r * c) return mat;
      6         vector<vector<int>> nmat(r, vector<int>(c));
      7 
      8         int x = 0, y = 0;
      9         for (int i = 0; i < n; i++) {
     10             for (int j = 0; j < m; j++) {
     11                 nmat[x][y] = mat[i][j];
     12                 y++;
     13                 x += y / c;
     14                 y %= c;
     15             }
     16         }
     17 
     18         return nmat;
     19     }
     20 };