-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixrotationpossible.java
More file actions
45 lines (45 loc) · 1.23 KB
/
matrixrotationpossible.java
File metadata and controls
45 lines (45 loc) · 1.23 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
class Solution {
public boolean findRotation(int[][] mat, int[][] target) {
int n = mat.length;
for (int k = 0; k < 4; k++) {
if (isEqual(mat, target)) {
return true;
}
rotate90(mat);
}
return false;
}
private void rotate90(int[][] mat) {
int n = mat.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
for (int i = 0; i < n; i++) {
reverseRow(mat[i]);
}
}
private void reverseRow(int[] row) {
int left = 0, right = row.length - 1;
while (left < right) {
int temp = row[left];
row[left] = row[right];
row[right] = temp;
left++;
right--;
}
}
private boolean isEqual(int[][] mat, int[][] target) {
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
if (mat[i][j] != target[i][j]) {
return false;
}
}
}
return true;
}
}