-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateMatrix.java
48 lines (40 loc) · 1.38 KB
/
RotateMatrix.java
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
import java.util.Arrays; // for using Arrays.deepToString
public class RotateMatrix {
public static int[][] rotateRight(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] result = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][rows - i - 1] = matrix[i][j];
}
}
return result;
}
public static int[][] rotateLeft(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] result = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[cols - j - 1][i] = matrix[i][j];
}
}
return result;
}
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3, 4, 5, 6},
{7, 8, 9, 0, 1, 2},
{3, 4, 5, 6, 7, 8},
{9, 0, 1, 2, 3, 4},
};
int[][] matrix2 = {
{1, 2, 3, 4, 5, 6},
};
System.out.println(Arrays.deepToString(rotateLeft(matrix1))); // [[9, 3, 7, 1], [0, 4, 8, 2], [1, 5, 9, 3], [2, 6, 0, 4], [3, 7, 1, 5], [4, 8, 2, 6]]
System.out.println(Arrays.deepToString(rotateRight(matrix1))); // [[6, 2, 8, 4], [5, 1, 7, 3], [4, 0, 6, 2], [3, 9, 5, 1], [2, 8, 4, 0], [1, 7, 3, 9]]
System.out.println(Arrays.deepToString(rotateLeft(matrix2))); // [[1], [2], [3], [4], [5], [6]]
System.out.println(Arrays.deepToString(rotateRight(matrix2))); // [[6], [5], [4], [3], [2], [1]]
}
}