-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage.java
More file actions
33 lines (28 loc) · 924 Bytes
/
Copy pathRotateImage.java
File metadata and controls
33 lines (28 loc) · 924 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
class RotateImage {
public void rotate(int[][] matrix) {
/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
int s = 0,
e = matrix.length - 1;
while(s < e){ //Swap the Full rows ie 1st and Last row
int[] temp = matrix[s];
matrix[s] = matrix[e];
matrix[e] = temp;
s++;
e--;
}
//Now Just swap the symmetry
for(int i = 0; i < matrix.length; i++){
for(int j = i+1; j < matrix[i].length; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
}