Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0e6e94

Browse files
add 1091
1 parenta9fe086 commitf0e6e94

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ _If you like this project, please leave me a star._ ★
289289
|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java)||Medium|String, Sliding Window|
290290
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java)|[:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
291291
|1090|[Largest Values From Labels](https://leetcode.com/problems/largest-values-from-labels/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1090.java)|[:tv:](https://youtu.be/E0OkE3G95vU)|Medium|HashTable, Greedy|
292+
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1091.java)||Medium|BFS|
292293
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java)||Easy||
293294
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1087.java)||Medium|Backtracking|
294295
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java)|[:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy||
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.LinkedList;
4+
importjava.util.Queue;
5+
6+
publicclass_1091 {
7+
publicstaticclassSolution1 {
8+
int[]directions =newint[]{0,1,1,0, -1,1, -1, -1,0};
9+
10+
publicintshortestPathBinaryMatrix(int[][]grid) {
11+
intm =grid.length;
12+
intn =grid[0].length;
13+
if (grid[0][0] ==1 ||grid[m -1][n -1] ==1) {
14+
return -1;
15+
}
16+
intminPath =0;
17+
Queue<int[]>queue =newLinkedList<>();
18+
queue.offer(newint[]{0,0});
19+
boolean[][]visited =newboolean[m][n];
20+
visited[0][0] =true;
21+
while (!queue.isEmpty()) {
22+
intsize =queue.size();
23+
for (inti =0;i <size;i++) {
24+
int[]curr =queue.poll();
25+
if (curr[0] ==m -1 &&curr[1] ==n -1) {
26+
returnminPath +1;
27+
}
28+
for (intj =0;j <directions.length -1;j++) {
29+
intnewx =directions[j] +curr[0];
30+
intnewy =directions[j +1] +curr[1];
31+
if (newx >=0 &&newx <n &&newy >=0 &&newy <n && !visited[newx][newy] &&grid[newx][newy] ==0) {
32+
queue.offer(newint[]{newx,newy});
33+
visited[newx][newy] =true;
34+
}
35+
}
36+
}
37+
minPath++;
38+
}
39+
return -1;
40+
}
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1091;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_1091Test {
11+
privatestatic_1091.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1091.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
assertEquals(2,solution1.shortestPathBinaryMatrix(newint[][]{
21+
{0,1},
22+
{1,0}
23+
}));
24+
}
25+
26+
@Test
27+
publicvoidtest2() {
28+
assertEquals(4,solution1.shortestPathBinaryMatrix(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,0],[1,1,0],[1,1,0]")));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
assertEquals(-1,solution1.shortestPathBinaryMatrix(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,0,0],[1,1,0],[1,1,0]")));
34+
}
35+
36+
@Test
37+
publicvoidtest4() {
38+
assertEquals(-1,solution1.shortestPathBinaryMatrix(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,0],[1,1,0],[1,1,1]")));
39+
}
40+
41+
@Test
42+
publicvoidtest5() {
43+
assertEquals(1,solution1.shortestPathBinaryMatrix(newint[][]{
44+
{0}
45+
}));
46+
}
47+
48+
@Test
49+
publicvoidtest6() {
50+
assertEquals(7,solution1.shortestPathBinaryMatrix(newint[][]{
51+
{0,1,0,0,1,1,0}, {1,0,0,0,0,0,0}, {1,0,0,1,1,1,1}, {0,1,0,0,0,0,0}, {1,0,0,0,0,0,1}, {1,0,0,1,0,0,0}, {1,0,1,0,0,1,0}
52+
}))
53+
;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp