-
Notifications
You must be signed in to change notification settings - Fork 0
/
canJump.java
37 lines (28 loc) · 937 Bytes
/
canJump.java
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
37
class Solution {
public boolean canJump(int[] nums) {
int currentNum = nums[0];
if (nums.length == 1)
return true;
if (currentNum == 0)
return false;
for (int i = 0; i < nums.length; i++) {
currentNum = nums[i];
//if it can reach out of bounds it can reach end of the array as well
if(i + currentNum >= nums.length - 1){
return true;
}
//jumping from the index until not reaching a 'zero'
for(int j = 0 ; j < nums[i] ; j++){
if(nums[i + currentNum] == 0){
currentNum--;
}
if(currentNum==0){
return false;
}
//if you can do not land on a 'zero' continue from that index
//continue;
}
}
return true;
}
}