题目描述:
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.
For example:
A =[2,3,1,1,4], return true.
A =[3,2,1,0,4], return false.
思路解析:
- 维护一个max,使它保持更新为,最长的步数后的值
- 只要最长的步数不小于对应的索引值,并且,索引值在规定的范围内,就继续向前走
- 如果索引值和对应索引的数字,之和,比max大,就更新它。
代码:
public class Solution {
public boolean canJump(int[] A) {
int max = 0;
for(int i=0;i<A.length && max>=i;i++){
if(i+A[i]>max)
max = i+A[i];
}
return max>=A.length-1 ? true:false;
}
}