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

Commit 5f0e323

Browse files
authored
Create Jump Game III.java
1 parent 3d94752 commit 5f0e323

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Medium/Jump Game III.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean canReach(int[] arr, int start) {
3+
Queue<Integer> queue = new LinkedList<>();
4+
Set<Integer> visited = new HashSet<>();
5+
queue.add(start);
6+
while (!queue.isEmpty()) {
7+
int size = queue.size();
8+
for (int i = 0; i < size; i++) {
9+
int removed = queue.remove();
10+
if (arr[removed] == 0) {
11+
return true;
12+
}
13+
if (visited.contains(removed)) {
14+
continue;
15+
}
16+
visited.add(removed);
17+
if (removed + arr[removed] < arr.length) {
18+
queue.add(removed + arr[removed]);
19+
}
20+
if (removed - arr[removed] >= 0) {
21+
queue.add(removed - arr[removed]);
22+
}
23+
}
24+
}
25+
return false;
26+
}
27+
}

0 commit comments

Comments
 (0)