We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c42f953 commit 8266ba8Copy full SHA for 8266ba8
scripts/algorithms/R/Reshape the Matrix/Reshape the Matrix.cpp
@@ -1,18 +1,23 @@
1
class Solution {
2
public:
3
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
4
- int m = mat.size(), n = mat[0].size();
5
- if(m*n != r*c)
+ if(mat.size()* mat[0].size()!= r * c) {
6
return mat;
7
- vector<vector<int>> res(r, vector<int>(c));
8
-
9
- int k = 0, l = 0;
10
- for(int i = 0; i < m; i++)
11
- for(int j = 0; j < n; j++) {
12
- res[k][l++] = mat[i][j];
13
- if(l == c) k++,l = 0;
+ }
+ vector<vector<int>>v(r,vector<int>(c));
+ int k = 0;
+ int l = 0;
+
+ for(int i = 0; i < mat.size(); i++) {
+ for(int j = 0; j < mat[0].size(); j++) {
+ if(l == v[0].size()) {
14
+ l = 0;
15
+ k++;
16
17
18
+ v[k][l++] = mat[i][j];
19
}
- return res;
20
21
+ return v;
22
-};
23
+};
0 commit comments