mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Initial commit
This commit is contained in:
29
Easy/reshape-the-matrix/solution.cpp
Normal file
29
Easy/reshape-the-matrix/solution.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<std::vector<int>>
|
||||
matrixReshape(std::vector<std::vector<int>> &mat, int r, int c) {
|
||||
int m = mat.size(), n = mat[0].size();
|
||||
if (m * n == r * c) {
|
||||
std::vector<std::vector<int>> rslt(r, std::vector<int>(c));
|
||||
|
||||
for (int i = 0, k = 0, l = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
rslt[k][l] = mat[i][j];
|
||||
|
||||
if (l < c) {
|
||||
++l;
|
||||
}
|
||||
|
||||
if (l == c) {
|
||||
l = 0;
|
||||
++k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rslt;
|
||||
} else {
|
||||
return mat;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user