-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0048_RotateImage.cpp
More file actions
55 lines (52 loc) · 1.54 KB
/
Copy path0048_RotateImage.cpp
File metadata and controls
55 lines (52 loc) · 1.54 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fmt/ranges.h>
#include <vector>
using namespace std;
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
// [A] erase & insert
const int n = static_cast<int>(matrix.size());
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
const int element = matrix[i][0];
matrix[j].push_back(element);
matrix[i].erase(matrix[i].begin());
}
}
// [B] swap and reverse
// for (int i = 0; i < matrix.size() - 1; i++) {
// for (int j = i + 1; j < matrix.size(); j++) {
// if (i != j) {
// swap(matrix[i][j], matrix[j][i]);
// }
// }
// }
// for (auto &row : matrix) {
// ranges::reverse(row);
// }
}
static void printMatrix(vector<vector<int>> &matrix)
{
for (auto &row : matrix) {
fmt::print("{}\n", fmt::join(row, ", "));
}
}
};
int main()
{
Solution sol;
const vector<vector<int>> test1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
const vector<vector<int>> test2 = {{5, 1, 9, 11},
{2, 4, 8, 10},
{13, 3, 6, 7},
{15, 14, 12, 16}};
for (auto test : {test1, test2}) {
fmt::print("Before:\n");
Solution::printMatrix(test);
sol.rotate(test);
fmt::print("After:\n");
Solution::printMatrix(test);
}
}