跳跃问题II
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], returntrue.
A =[3,2,1,0,4], returnfalse.
问题来源
链接: 牛客地址
class Solution {
public:
int reach=0;//到达最远距离
bool canJump(int A[], int n) {
for(int i=0;i<n;i++)
{
if(reach<i)return false;
if(reach>=n-1)return true;
reach=reach>(A[i]+i)?reach:(A[i]+i);
}
}
};