跳跃游戏
public boolean canJump(int[] nums) {
if(nums.length ==1) return true;
int coverRange = 0;
for(int i=0; i<=coverRange; i++){ // 遍历coverRange,因为coverRange是当前可以跳跃的最大距离,所以遍历coverRange即可
coverRange = Math.max(coverRange, i+nums[i]); // 站在第i个位置,最多可以跳nums[i]步
if(coverRange >= nums.length-1) return true; // 如果coverRange >= nums.length-1(最后一个索引位置),则可以到达最后一个位置
}
return false;
}
2025年6月25日大约 5 分钟