
/**
每个位置的数值代表能走的最大步数,能否走到最后一格?
遍历数组维护一个变量max_reachable = index + nums[i];代表当前位置能到达的最远位置,能到nums.length - 1,即能达到最后一个下标
若遍历过程中出现max_reachable == index,则代表无法继续往下走了,直接返回false
注意事项:
遍历需要在nums.length - 2处终止,nums.length - 1处就是最后一个位置
*/
class Solution {
/**
每个位置的数值代表能走的最大步数,能否走到最后一格?
遍历数组维护一个变量max_reachable = index + nums[i];代表当前位置能到达的最远位置,能到nums.length - 1,即能达到最后一个下标
若遍历过程中出现max_reachable == index,则代表无法继续往下走了,直接返回false
注意事项:
遍历需要在nums.length - 2处终止,nums.length - 1处就是最后一个位置
*/
public boolean canJump(int[] nums) {
int maxReachable = 0; //当前最远可达位置
for(int index = 0; index < nums.length - 1; index++) {
maxReachable = Math.max(maxReachable, index + nums[index]);
if(maxReachable == index) {
return false;
}
}
return true;
}
}
300

被折叠的 条评论
为什么被折叠?



