import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return bool布尔型
*/
public boolean can_jump (int[] nums) {
// 最大跳跃位置
int maxPosition = 0;
for (int i = 0; i < nums.length; i++) {
// 如果当前索引都比最大跳跃位置大了,说明肯定不能到达下一个点,已经G了
if (maxPosition < i) {
return false;
}
// 每次取最大跳跃位置,要最远的
maxPosition = Math.max(maxPosition, i + nums[i]);
// 如果能达到数组结尾,返回true
if (maxPosition >= nums.length -1) {
return true;
}
}
return false;
}
}
本题知识点分析:
1.贪心算法(跳跃位置求最远)
2.数组遍历
3.数学模拟
本题解题思路分析:
1.利用Math.max每次求最远跳跃的距离,这题是跳跃游戏的简单版
2.如果当前索引都比最大跳跃位置大了,说明肯定不能到达下一个点,已经G了
3.如果能达到数组结尾,返回true

京公网安备 11010502036488号