-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate image.java
More file actions
58 lines (52 loc) · 1.28 KB
/
Rotate image.java
File metadata and controls
58 lines (52 loc) · 1.28 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
/*
[1,2,3],
[4,5,6],
[7,8,9]
mirror around secondary diagonal
[9, 6, 3],
[8, 5, 2],
[7, 4, 1]
mirror horizontally around middle line
[7, 4, 1],
[8, 5, 2],
[9, 6, 3]
*/
/*
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
mirror around secondary diagonal
[16, 7, 10, 11],
[12, 6, 8, 9],
[14, 3, 4, 1],
[15, 13, 2, 5]
mirror horizontally around middle line
...
*/
public void rotate(int[][] matrix) {
mirrorSecondaryDiagonal(matrix);
mirrorHorizontally(matrix);
}
private static void mirrorSecondaryDiagonal(int[][] matrix) {
// flip second diagonaly
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix.length - i - 1; j++) {
int current = matrix[i][j];
matrix[i][j] = matrix[matrix.length - j - 1][matrix.length - i - 1];
matrix[matrix.length - j - 1][matrix.length - i - 1] = current;
}
}
}
// flip horizontally
private static void mirrorHorizontally(int[][] matrix) {
for(int i = 0; i < matrix.length / 2; i++) {
for(int j = 0; j < matrix.length; j++) {
int current = matrix[i][j];
matrix[i][j] = matrix[matrix.length - i - 1][j];
matrix[matrix.length - i - 1][j] = current;
}
}
}
}