dfs也能做,不过会超时,显得很呆
关键点在于不用考虑跳几步,只需要考虑每个节点可达范围内的值即可,不用管是怎么跳到这一步的,然后计算最大值能否cover住数组的大小
class Solution {
public boolean canJump(int[] nums) {
int max = 0;
if (nums.length == 1) {
return true;
}
for (int i = 0; i <= max; i++) {
max = Math.max(i + nums[i], max);
if (max >= nums.length - 1) {
return true;
}
}
return false;
}
}