-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage2.java
More file actions
42 lines (37 loc) · 1.11 KB
/
RotateImage2.java
File metadata and controls
42 lines (37 loc) · 1.11 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
import java.util.Arrays;
/**
* Leetcode problem #48, Rotate Image
* https://leetcode.com/problems/rotate-image/
* This method uses transpose and then reflect
*/
public class RotateImage2 {
public static void main(String[] args) {
int[][] matrix = { { 5, 1, 9, 11 }, { 2, 4, 8, 10 }, { 13, 3, 6, 7 }, { 15, 14, 12, 16 } };
rotate(matrix);
System.out.println(Arrays.deepToString(matrix));
}
public static void rotate(int[][] matrix) {
transpose(matrix);
reflect(matrix);
}
public static void transpose(int[][] mat) {
int n = mat.length;
for (int i=0; i < n; i++) {
for (int j=i+1; j < n; j++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
}
public static void reflect(int[][] mat) {
int n = mat.length;
for (int i=0; i<n; i++) {
for (int j=0; j < n/2; j++) {
int temp = mat[i][j];
mat[i][j] = mat[i][n-j-1];
mat[i][n-j-1] = temp;
}
}
}
}