Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 66bf4b2

Browse files
add 1886
1 parent 4c3aff7 commit 66bf4b2

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1886|[Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1886.java) ||Easy|Array|
1112
|1880|[Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1880.java) ||Easy|String|
1213
|1877|[Minimize Maximum Pair Sum in Array](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1877.java) ||Medium|Greedy, Sort|
1314
|1876|[Substrings of Size Three with Distinct Characters](https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1876.java) ||Easy|String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1886 {
4+
public static class Solution1 {
5+
public boolean findRotation(int[][] mat, int[][] target) {
6+
int m = mat.length;
7+
int n = mat[0].length;
8+
for (int i = 0; i < m; i++) {
9+
int j = 0;
10+
for (; j < n; j++) {
11+
if (mat[i][j] != target[i][j]) {
12+
break;
13+
}
14+
}
15+
if (j < n) {
16+
break;
17+
} else if (i == m - 1) {
18+
return true;
19+
}
20+
}
21+
22+
//rotate 90 degrees once
23+
for (int i = 0, k = n - 1; i < m; i++, k--) {
24+
int j = 0;
25+
for (; j < n; j++) {
26+
if (mat[i][j] != target[j][k]) {
27+
break;
28+
}
29+
}
30+
if (j < n) {
31+
break;
32+
} else if (i == m - 1) {
33+
return true;
34+
}
35+
}
36+
int[][] rotated = new int[m][n];
37+
for (int i = 0, k = n - 1; i < m; i++, k--) {
38+
for (int j = 0; j < n; j++) {
39+
rotated[j][k] = mat[i][j];
40+
}
41+
}
42+
43+
//rotate 90 degrees the second time
44+
for (int i = 0, k = n - 1; i < m; i++, k--) {
45+
int j = 0;
46+
for (; j < n; j++) {
47+
if (rotated[i][j] != target[j][k]) {
48+
break;
49+
}
50+
}
51+
if (j < n) {
52+
break;
53+
} else if (i == m - 1) {
54+
return true;
55+
}
56+
}
57+
int[][] rotated2 = new int[m][n];
58+
for (int i = 0, k = n - 1; i < m; i++, k--) {
59+
int j = 0;
60+
for (; j < n; j++) {
61+
rotated2[j][k] = rotated[i][j];
62+
}
63+
}
64+
65+
//rotate 90 degrees the third time
66+
for (int i = 0, k = n - 1; i < m; i++, k--) {
67+
int j = 0;
68+
for (; j < n; j++) {
69+
if (rotated2[i][j] != target[j][k]) {
70+
break;
71+
}
72+
}
73+
if (j < n) {
74+
break;
75+
} else if (i == m - 1) {
76+
return true;
77+
}
78+
}
79+
return false;
80+
}
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1886;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1886Test {
10+
private static _1886.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1886.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.findRotation(new int[][]{
20+
{0, 1},
21+
{1, 0}
22+
}, new int[][]{
23+
{1, 0},
24+
{0, 1}
25+
}));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
assertEquals(false, solution1.findRotation(new int[][]{
31+
{0, 1},
32+
{1, 1}
33+
}, new int[][]{
34+
{1, 0},
35+
{0, 1}
36+
}));
37+
}
38+
39+
@Test
40+
public void test3() {
41+
assertEquals(true, solution1.findRotation(new int[][]{
42+
{0, 0, 0},
43+
{0, 1, 0},
44+
{1, 1, 1}
45+
}, new int[][]{
46+
{1, 1, 1},
47+
{0, 1, 0},
48+
{0, 0, 0}
49+
}));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)