forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchjung99.java
More file actions
38 lines (29 loc) · 813 Bytes
/
Copy pathchjung99.java
File metadata and controls
38 lines (29 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
boolean isArrived;
int SIZE;
public boolean canJump(int[] nums) {
isArrived = false;
SIZE = nums.length;
bfs(nums, 0);
return isArrived;
}
public void bfs(int[] nums, int x) {
Queue<Integer> q = new ArrayDeque<>();
q.add(x);
boolean[] visit = new boolean[SIZE];
visit[x] = true;
int curX;
int nextX;
while (!q.isEmpty()){
curX = q.poll();
isArrived = (curX + 1) == SIZE;
if (isArrived) return;
for (int dx = 1; dx <= nums[curX]; dx++){
nextX = curX + dx;
if (nextX >= SIZE || visit[nextX]) continue;
q.add(nextX);
visit[nextX] = true;
}
}
}
}