-
Notifications
You must be signed in to change notification settings - Fork 45
/
Rotate_Image.cpp
36 lines (29 loc) · 1.26 KB
/
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
void RotateInnerSquare(vector<vector<int>>& matrix, int lowestIndex, int highestIndex);
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int highestIndex = matrix.size() - 1;
int lowestIndex = 0;
RotateInnerSquare(matrix, lowestIndex, highestIndex);
}
};
// Recursive function starts by rotating outermost square, then moving inward
void RotateInnerSquare(vector<vector<int>>& matrix, int lowestIndex, int highestIndex){
if(highestIndex < 1)
return;
int store; // store value to it isn't overwritten
int replace; // value to replace
// [row][col]
// rotates 4 values each iteration
for(int i = 0; i < highestIndex - lowestIndex; i++){
store = matrix[lowestIndex + i][highestIndex];
replace = matrix[highestIndex][highestIndex - i];
matrix[lowestIndex + i][highestIndex] = matrix[lowestIndex][lowestIndex + i];
matrix[highestIndex][highestIndex - i] = store;
store = replace;
replace = matrix[highestIndex - i][lowestIndex];
matrix[highestIndex - i][lowestIndex] = store;
matrix[lowestIndex][lowestIndex + i] = replace;
}
RotateInnerSquare(matrix, lowestIndex + 1, highestIndex - 1);
}