We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ed38bd6 commit 37fb6e2Copy full SHA for 37fb6e2
jump-game/chjung99.java
@@ -0,0 +1,38 @@
1
+class Solution {
2
+ boolean isArrived;
3
+ int SIZE;
4
+
5
+ public boolean canJump(int[] nums) {
6
+ isArrived = false;
7
+ SIZE = nums.length;
8
9
+ bfs(nums, 0);
10
11
+ return isArrived;
12
+ }
13
14
+ public void bfs(int[] nums, int x) {
15
+ Queue<Integer> q = new ArrayDeque<>();
16
+ q.add(x);
17
+ boolean[] visit = new boolean[SIZE];
18
+ visit[x] = true;
19
20
+ int curX;
21
+ int nextX;
22
23
+ while (!q.isEmpty()){
24
+ curX = q.poll();
25
+ isArrived = (curX + 1) == SIZE;
26
+ if (isArrived) return;
27
28
+ for (int dx = 1; dx <= nums[curX]; dx++){
29
+ nextX = curX + dx;
30
+ if (nextX >= SIZE || visit[nextX]) continue;
31
+ q.add(nextX);
32
+ visit[nextX] = true;
33
34
35
36
+}
37
38
0 commit comments