-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotateimage.java
More file actions
49 lines (38 loc) · 1.31 KB
/
Copy pathrotateimage.java
File metadata and controls
49 lines (38 loc) · 1.31 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
public class rotateimage {
public static void rotate(int[][] matrix) {
int n = matrix.length;
for (int row = 0; row < n / 2; row++) {
for (int col = row; col < n - row - 1; col++) {
int temp = matrix[row][col];
matrix[row][col] = matrix[col][n - 1 - row];
matrix[col][n - 1 - row] = temp;
temp = matrix[row][col];
matrix[row][col] = matrix[n - 1 - row][n - 1 - col];
matrix[n - 1 - row][n - 1 - col] = temp;
temp = matrix[row][col];
matrix[row][col] = matrix[n - 1 - col][row];
matrix[n - 1 - col][row] = temp;
}
}
}
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Original Matrix:");
printMatrix(matrix);
rotate(matrix);
System.out.println("Rotated Matrix:");
printMatrix(matrix);
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}