-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path48-rotate-image.cpp
39 lines (35 loc) · 1.26 KB
/
48-rotate-image.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
// class Solution {
// public:
// void rotate(vector<vector<int>>& matrix) {
// int left = 0, right = size(matrix) - 1;
// while(left < right) {
// auto top = left, bottom = right;
// for(int i = 0; i < right - left; i++) {
// auto topLeft = matrix[top][left+i];
// matrix[top][left+i] = matrix[bottom-i][left];
// matrix[bottom-i][left] = matrix[bottom][right-i];
// matrix[bottom][right-i] = matrix[top+i][right];
// matrix[top+i][right] = topLeft;
// }
// left++;
// right--;
// }
// }
// };
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int left = 0, right = size(matrix) - 1;
while (left < right) {
auto top = left, bottom = right;
for ( int i = 0; i < (right-left); i++) {
auto cache = matrix[top][left + i];
matrix[top][left + i] = matrix[bottom - i][left];
matrix[bottom - i][left] = matrix[bottom][right - i];
matrix[bottom][right - i] = matrix[top + i][right];
matrix[top + i][right] = cache;
}
left++; right--;
}
}
};