-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotateMatrix_90.java
More file actions
56 lines (42 loc) · 1.05 KB
/
rotateMatrix_90.java
File metadata and controls
56 lines (42 loc) · 1.05 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
public class rotateMatrix_90{
static void rotate_clockwise(int[][] M, int n){
for(int x=0; x < n/2; x++){
for(int y=x; y<n-1-x; y++){
int top = M[x][y];
M[x][y] = M[n-1-y][x];
M[n-1-y][x] = M[n-1-x][n-1-y];
M[n-1-x][n-1-y] = M[y][n-1-x];
M[y][n-1-x] = top;
}
}
}
static void rotate_anticlockwise(int[][] M, int n){
for(int x=0; x < n/2; x++){
for(int y=x; y<n-1-x; y++){
int top = M[x][y];
M[x][y] = M[y][n-1-x];
M[y][n-1-x] = M[n-1-x][n-1-y];
M[n-1-x][n-1-y] = M[n-1-y][x];
M[n-1-y][x] = top;
}
}
}
static void print(int[][] M, int n){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.print(M[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args){
int[][] M = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}};
int n = M.length;
System.out.println("Clockwise 90 degree rotate");
rotate_clockwise(M,n);
print(M,n);
System.out.println("Anti-Clockwise 90 degree rotate");
rotate_anticlockwise(M,n);
print(M,n);
}
}