-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixRotation.java
More file actions
60 lines (46 loc) · 1.41 KB
/
Copy pathMatrixRotation.java
File metadata and controls
60 lines (46 loc) · 1.41 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
59
60
public class MatrixRotation {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Original Matrix:");
printMatrix(matrix);
rotate90Clockwise(matrix);
System.out.println("Matrix after 90-degree clockwise rotation:");
printMatrix(matrix);
}
public static void rotate90Clockwise(int[][] matrix) {
int length = matrix.length;
for (int row = 0; row < length; row++) {
for (int column = row; column < length; column++) {
int temp = matrix[row][column];
matrix[row][column] = matrix[column][row];
matrix[column][row] = temp;
}
}
for (int index = 0; index < length; index++) {
reverseRow(matrix[index]);
}
}
private static void reverseRow(int[] row) {
int start = 0;
int end = row.length - 1;
while (start < end) {
int temp = row[start];
row[start] = row[end];
row[end] = temp;
start++;
end--;
}
}
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}