116 · 跳跃游戏 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.
样例
Example 1:
Input:
A = [2,3,1,1,4]
Output:
true
Explanation:
0 -> 1 -> 4 (the number here is subscript) is a reasonable scheme.
public class Solution {
/**
* @param A: A list of integers
* @return: A boolean
*/
public boolean canJump(int[] A) {
// write your code here
int maxlength = 0 ;
for(int i = 0 ; i < A.length ; i++){
if(i > maxlength){
return false ;
}
maxlength = Math.max(maxlength , A[i] + i);
if(maxlength >= A.length-1){
return true ;
}
}
return false ;
}
}