-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRotate_Image.cpp
More file actions
38 lines (32 loc) · 917 Bytes
/
Copy pathRotate_Image.cpp
File metadata and controls
38 lines (32 loc) · 917 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
33
34
35
36
37
38
/*
Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
*/
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
//from outer to innner
for (int i = 0; i < n/2; i++) {
for (int j = 0; j < n - i * 2 - 1; j++) { //length of side
int r = i, c = i + j;
int nr, nc;
int next = matrix[r][c];
int save;
for (int k = 0; k < 4; k++) {
//move A[r][c] --> A[c][n-1-r]
nr = c;
nc = n - 1 - r;
save = matrix[nr][nc];
matrix[nr][nc] = next;
next = save;
r = nr;
c = nc;
}
}
}
}
};