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)


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