leetcode 55. 跳跃游戏
本题可以采用贪心的思想,我们可以用一个整数维护能跳到最远的位置,如果最远的位置能到达最后一个位置说明可以跳到
最远位置如何更新呢
最远位置=max(当前位置i+当前位置可以跳的距离nums[i],最远位置)
class Solution {
public boolean canJump(int[] nums) {
//贪心的思想,维护一个跳到最远的位置
int fastest=0;//能跳到的最远的位置
for(int i=0;i<nums.length-1;i++){
if(i<=fastest){//注意,要确保当前位置 我们能跳到
fastest=Math.max(i+nums[i],fastest);
}
}
if(fastest>=nums.length-1){
return true;
}else{
return false;
}
}
}