-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage48.java
More file actions
31 lines (28 loc) · 1.01 KB
/
Copy pathRotateImage48.java
File metadata and controls
31 lines (28 loc) · 1.01 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
public class RotateImage48 {
public static void rotate(int[][] matrix) {
if(matrix.length == 0 || matrix[0].length == 0 || matrix.length != matrix[0].length || matrix.length == 1) return;
int temp = 0;
int n = matrix.length;
for(int i = 0; i < (n + 1) / 2; i++) {
for(int j = i; j < n - 1 - i; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = temp;
}
}
}
public static void main(String[] args) {
int[][] b = new int[][]{{1,2,3}, {4,5,6}, {7,8,9}};
int[][] m = new int[][]{{1,2,3,1},{4,5,6,2},{7,8,9,4},{5,7,3,7}};
rotate(m);
rotate(b);
for(int[] n : b){
for(int a : n){
System.out.print(a);
}
System.out.print("\n");
}
}
}