1 解题思想
这道题的意思,就是给了一个数组,数组里面表示的是在这个位置上,最大可跳跃的位置。
那么现在,就假设你在0的位置上,问你可以跳跃到最末尾的位置吗?
那么这道题的思路就是使用一个贪心法,使用一个步进指针,用一个上界指针。
每次遍历的时候,不停的更新上界指针的位置(也就是当前位置+当前可以跳到的位置),知道看你能遇到结尾吗?如果成功了,就范围true,没有就返回false
2 原题
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
3 AC解
public class Solution {
/**
* 贪心寻找上界!!!!还有这题是不寻找路线的!!!
* */
public boolean canJump(int[] nums) {
int reach=0;
int i=0;
while(i<nums.length && i<=reach){
reach=Math.max(reach,i+nums[i]);
i++;
}
return reach>=nums.length-1;
}
}
本文介绍了一种使用贪心算法解决跳跃问题的方法。给定一个数组,数组中的每个元素表示当前位置的最大跳跃距离。文章详细解释了算法的实现过程,并通过两个示例来展示如何判断从数组起始位置是否能够跳跃到数组的末尾。

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



