-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.java
31 lines (25 loc) · 834 Bytes
/
day25.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
/**
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
**/
class Solution {
public boolean canJump(int[] nums) {
if (nums.length == 0)
return false;
if (nums.length == 1)
return true;
int i, max = nums[0];
int len;
len = nums.length;
for (i = 0; i < len; i++) {
if (max <= i && nums[i] == 0)
return false;
if (i + nums[i] > max)
max = i + nums[i];
if (max >= len - 1)
return true;
}
return false;
}
}