0%

Algorithm-Greedy

阅读更多

1 Question-55[★★★★★]

Jump Game

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
public boolean canJump(int[] nums) {
if (nums == null || nums.length == 0) return false;

int farest = 0;
int curFar = 0;
int i = 0;

while (farest < nums.length - 1) {
while (i <= farest) {
curFar = Math.max(curFar, i + nums[i++]);
}

if (curFar == farest) return false;
farest = curFar;
}

return true;
}
}