需要两个计数,lastJump记载当前能跳最远处,maxJump记录实际能跳最远距离。
class Solution {
public:
bool canJump(int A[], int n) {
int lastJump=0;
int maxJump=0;
int i=0;
while(maxJump<n-1){
if(maxJump<A[i]+i) maxJump=A[i]+i;
if(i==lastJump){
if(maxJump<=lastJump) return false;
lastJump=maxJump;
}
i++;
}
return true;
}
};