forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1886.java
82 lines (78 loc) · 2.45 KB
/
_1886.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.fishercoder.solutions;
public class _1886 {
public static class Solution1 {
public boolean findRotation(int[][] mat, int[][] target) {
int m = mat.length;
int n = mat[0].length;
for (int i = 0; i < m; i++) {
int j = 0;
for (; j < n; j++) {
if (mat[i][j] != target[i][j]) {
break;
}
}
if (j < n) {
break;
} else if (i == m - 1) {
return true;
}
}
//rotate 90 degrees once
for (int i = 0, k = n - 1; i < m; i++, k--) {
int j = 0;
for (; j < n; j++) {
if (mat[i][j] != target[j][k]) {
break;
}
}
if (j < n) {
break;
} else if (i == m - 1) {
return true;
}
}
int[][] rotated = new int[m][n];
for (int i = 0, k = n - 1; i < m; i++, k--) {
for (int j = 0; j < n; j++) {
rotated[j][k] = mat[i][j];
}
}
//rotate 90 degrees the second time
for (int i = 0, k = n - 1; i < m; i++, k--) {
int j = 0;
for (; j < n; j++) {
if (rotated[i][j] != target[j][k]) {
break;
}
}
if (j < n) {
break;
} else if (i == m - 1) {
return true;
}
}
int[][] rotated2 = new int[m][n];
for (int i = 0, k = n - 1; i < m; i++, k--) {
int j = 0;
for (; j < n; j++) {
rotated2[j][k] = rotated[i][j];
}
}
//rotate 90 degrees the third time
for (int i = 0, k = n - 1; i < m; i++, k--) {
int j = 0;
for (; j < n; j++) {
if (rotated2[i][j] != target[j][k]) {
break;
}
}
if (j < n) {
break;
} else if (i == m - 1) {
return true;
}
}
return false;
}
}
}