55. 跳跃游戏
class Solution {
public boolean canJump(int[] nums) {
if(nums.length == 1){
return true;
}
int coverange = 0;
for(int i =0; i <= coverange ; i++){
coverange = Math.max(coverange,i+nums[i]);
if(coverange >= nums.length-1){
return true;
}
}
return false;
}
}
45. 跳跃游戏 II
class Solution {
public int jump(int[] nums) {
int result = 0;
int temp = 0;
int end = 0;
for(int i = 0 ;i <= end && end < nums.length-1 ; i++){
temp = Math.max(temp,i+nums[i]);
if(i == end){
end = temp;
result++;
}
}
return result;
}
}