Skip to content

Commit 1397da6

Browse files
authored
Create Determine Whether Matrix Can Be Obtained By Rotation.java
1 parent fdaa82a commit 1397da6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public boolean findRotation(int[][] mat, int[][] target) {
3+
for (int i = 0; i < 4; i++) {
4+
if (isEqual(mat, target)) {
5+
return true;
6+
}
7+
rotate(mat);
8+
}
9+
return false;
10+
}
11+
12+
private boolean isEqual(int[][] mat, int[][] target) {
13+
for (int i = 0; i < mat.length; i++) {
14+
for (int j = 0; j < mat[i].length; j++) {
15+
if (mat[i][j] != target[i][j]) {
16+
return false;
17+
}
18+
}
19+
}
20+
return true;
21+
}
22+
23+
private void rotate(int[][] mat){
24+
for(int i = 0; i < mat.length; i++){
25+
for(int j = i; j < mat[0].length; j++){
26+
int temp = mat[i][j];
27+
mat[i][j] = mat[j][i];
28+
mat[j][i] = temp;
29+
}
30+
}
31+
for(int i = 0; i < mat[0].length / 2; i++){
32+
for(int j = 0; j < mat.length; j++){
33+
int temp = mat[j][i];
34+
mat[j][i] = mat[j][mat[0].length - i - 1];
35+
mat[j][mat[0].length - i - 1] = temp;
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)