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

Commitb315c79

Browse files
add 1971
1 parentf79bbc1 commitb315c79

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ _If you like this project, please leave me a star._ ★
3535
|1980|[Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1980.java)||Medium||
3636
|1979|[Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1979.java)||Easy||
3737
|1974|[Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1974.java)||Easy||
38+
|1971|[Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1971.java)||Easy|BFS, DFS, Graph|
3839
|1968|[Array With Elements Not Equal to Average of Neighbors](https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1968.java)||Medium||
3940
|1967|[Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1967.java)||Easy||
4041
|1961|[Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1961.java)||Easy||
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.HashSet;
5+
importjava.util.LinkedList;
6+
importjava.util.Map;
7+
importjava.util.Queue;
8+
importjava.util.Set;
9+
10+
publicclass_1971 {
11+
publicstaticclassSolution1 {
12+
publicbooleanvalidPath(intn,int[][]edges,intstart,intend) {
13+
if (start ==end) {
14+
returntrue;
15+
}
16+
Map<Integer,Set<Integer>>neighborsMap =newHashMap<>();
17+
for (int[]edge :edges) {
18+
intu =edge[0];
19+
intv =edge[1];
20+
Set<Integer>neighbors1 =neighborsMap.getOrDefault(u,newHashSet<>());
21+
neighbors1.add(v);
22+
neighborsMap.put(u,neighbors1);
23+
24+
Set<Integer>neighbors2 =neighborsMap.getOrDefault(v,newHashSet<>());
25+
neighbors2.add(u);
26+
neighborsMap.put(v,neighbors2);
27+
}
28+
Queue<Integer>queue =newLinkedList<>();
29+
Set<Integer>visitedVertices =newHashSet<>();
30+
queue.offer(start);
31+
while (!queue.isEmpty()) {
32+
intsize =queue.size();
33+
for (inti =0;i <size;i++) {
34+
Integercurr =queue.poll();
35+
for (intneighbor :neighborsMap.getOrDefault(curr,newHashSet<>())) {
36+
if (neighbor ==end) {
37+
returntrue;
38+
}
39+
if (visitedVertices.add(neighbor)) {
40+
queue.offer(neighbor);
41+
}
42+
}
43+
}
44+
}
45+
returnfalse;
46+
}
47+
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1971;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_1971Test {
11+
privatestatic_1971.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1971.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
int[][]edges =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[1,2],[2,0]");
21+
assertEquals(true,solution1.validPath(3,edges,0,2));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
int[][]edges =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[0,2],[3,5],[5,4],[4,3]");
27+
assertEquals(false,solution1.validPath(6,edges,0,5));
28+
}
29+
30+
@Test
31+
publicvoidtest3() {
32+
int[][]edges =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[4,3],[1,4],[4,8],[1,7],[6,4],[4,2],[7,4],[4,0],[0,9],[5,4]");
33+
assertEquals(true,solution1.validPath(10,edges,5,9));
34+
}
35+
36+
@Test
37+
publicvoidtest4() {
38+
int[][]edges =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,7],[0,8],[6,1],[2,0],[0,4],[5,8],[4,7],[1,3],[3,5],[6,5]");
39+
assertEquals(true,solution1.validPath(10,edges,7,5));
40+
}
41+
42+
@Test
43+
publicvoidtest5() {
44+
assertEquals(true,solution1.validPath(1,newint[][]{},0,0));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp