贪心之跳跃游戏
力扣的跳跃游戏以及拓展题目
package com.zhang.reflection.面试.算法模版.贪心算法;
/**
* 给定一个非负整数数组nums ,你最初位于数组的 第一个下标 。
*
* 数组中的每个元素代表你在该位置可以跳跃的最大长度。
*
* 判断你是否能够到达最后一个下标。
*/
public class 跳跃游戏 {
public boolean canJump(int[] nums) {
int n=nums.length;
int rightmost=0;
for(int i=0;i<n;i++){
if(i<=rightmost){
rightmost=Math.max(rightmost,i+nums[i]);
if(rightmost>=n-1){
return true;
}
}else{
return false;
}
}
return false;
}
}

曼迪匹艾公司福利 121人发布