-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path566.cpp
More file actions
27 lines (27 loc) · 732 Bytes
/
Copy path566.cpp
File metadata and controls
27 lines (27 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>> &nums, int r, int c) {
int n = nums.size();
if (n == 0)
return nums;
int m = nums[0].size();
if (n * m != r * c)
return nums;
vector<vector<int>> matrix;
vector<int> temp;
int x = 0, y = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
temp.push_back(nums[i][j]);
++y;
if (y == c) {
++x;
y = 0;
matrix.push_back(temp);
temp.clear();
}
}
}
return matrix;
}
};