0.问题分析
- 如果某一个作为 起跳点 的格子可以跳跃的距离是 3,那么表示后面 3 个格子都可以作为起跳点。
- 可以对每一个能作为起跳点的格子都尝试跳一次,把能跳到最远的距离不断更新。
- 如果可以达到最后或者甚至超过最后,那就成功。如果到达一个不能作为起跳点的位置,那么后面的位置也必定不能成为起跳点,那就失败。
1.实际实现
class Solution {
public boolean canJump(int[] nums) {
int farthest=0;
for(int i=0;i<nums.length;i++){
if(i>farthest){
return false;
}
farthest=java.lang.Math.max(farthest,nums[i]+i);
if(farthest>nums.length){
return true;
}
}
return true;
}
}