-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0048.cpp
More file actions
32 lines (31 loc) · 705 Bytes
/
0048.cpp
File metadata and controls
32 lines (31 loc) · 705 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
28
29
30
31
32
class Solution {
public:
void rotate(vector<vector<int>> &matrix) {
int n = matrix.size();
if (n <= 1) {
return;
}
for (int level = 0; level <= n / 2 - 1; level++) {
for (int start = level; start <= n - 2 - level; start++) {
trans(matrix, n, level, start);
}
}
return;
}
void trans(vector<vector<int>> &matrix, int n, int i, int j) {
int element = matrix[i][j];
int ni = j;
int nj = n - 1 - i;
while (ni != i || nj != j) {
int t = matrix[ni][nj];
matrix[ni][nj] = element;
element = t;
int ti = ni;
int tj = nj;
ni = tj;
nj = n - 1 - ti;
}
matrix[i][j] = element;
return;
}
};