1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { const int prevR = nums.size(), prevC = nums[0].size(); if (prevR * prevC < r * c) return nums; vector<vector<int>> ans(r, vector<int>(c)); int idx = 0; for (int i = 0; i < prevR; ++i) for (int j = 0; j < prevC; ++j) { int currR = idx / c, currC = idx % c; ans[currR][currC] = nums[i][j]; ++idx; } return ans; } };
|