-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixReshape.cpp
38 lines (36 loc) · 1015 Bytes
/
matrixReshape.cpp
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
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m = nums.size(), n = nums[0].size();
if (m * n != r * c) {
return nums;
}
vector<vector<int>> ans(r, vector<int>(c));
vector<int> row(c);
int x = 0, y = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
row[y++] = nums[i][j];
if (y == c) {
ans[x++] = row;
y = 0;
}
}
}
return ans;
}
};
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m = nums.size(), n = nums[0].size();
if (m * n != r * c) {
return nums;
}
vector<vector<int>> ans(r, vector<int>(c));
for (int x = 0; x < m * n; ++x) {
ans[x / c][x % c] = nums[x / n][x % n];
}
return ans;
}
};