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
.
public class Solution {
public boolean canJump(int[] A) {
// Note: The Solution object is instantiated only once and is reused by each test case.
//其实只要没有0,有0的话,只要0前的数字只要有一个能跳过它就没问题。
boolean result = true;
if(A != null && A.length > 1) {
int appear = -1;
for (int i = A.length - 1; i >= 0; i--) {
if(appear > -1) {
if (appear - i < A[i]|| (appear == (A.length -1) && appear - i == A[i])) {appear = -1;}
}
if(A[i] == 0 && appear == -1) {
appear = i;
}
}
result = (appear < 0) ? true : false;
}
return result;
}
}