-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrotate-image.cpp
More file actions
31 lines (28 loc) · 1022 Bytes
/
rotate-image.cpp
File metadata and controls
31 lines (28 loc) · 1022 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
//https://leetcode.com/problems/rotate-image/
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n=matrix.size();
int left = 0, right = n-1;
int top = 0, bottom = n-1;
while(left<right && top < bottom){
for(int i=0; i<(right-left);i++){
//use reverse order
//save top left
int temp = matrix[top][left+i];
//move bottom left to top left
matrix[top][left+i] = matrix[bottom-i][left];
//move bottom right to bottom left
matrix[bottom-i][left] = matrix[bottom][right-i];
//move top right to bottom right
matrix[bottom][right-i] = matrix[top+i][right];
//move top left to top right
matrix[top+i][right] = temp;
}
left++;
right--;
top++;
bottom--;
}
}
};