Determine Whether Matrix Can Be Obtained By Rotation LeetCode solution in Java

1
class Solution {
2
public boolean findRotation(int[][] mat, int[][] target) {
3
if (mat == target) return true;
4
int n = mat.length;
5
int[] res[] = new int[n][n];
6
for (int i = 0; i < n; i++) { // clockwise 90
7
for (int j = 0; j < n; j++) {
8
res[i][j] = mat[n - 1 - j][i];
9
}
10
}
11

12
int[] res2[] = new int[n][n];
13
for (int i = 0; i < n; i++) { // clockwise 180
14
for (int j = 0; j < n; j++) {
15
res2[i][j] = res[n - 1 - j][i];
16
}
17
}
18

19
int[] res3[] = new int[n][n];
20
for (int i = 0; i < n; i++) { // clockwise 270
21
for (int j = 0; j < n; j++) {
22
res3[i][j] = res2[n - 1 - j][i];
23
}
24
}
25

26
// compare to 90,180,270 and itself
27
if (Arrays.deepEquals(target, res)
28
|| Arrays.deepEquals(target, res2)
29
|| Arrays.deepEquals(target, res3)
30
|| Arrays.deepEquals(target, mat)) {
31
return true;
32
}
33
return false;
34
}
35
}
36

37
// Arrays.deepEquals() use for matrix

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0