Given an array of non-negative integers, you is initially positioned at the first index of the array.
Each element of the array represents your maximum jump length is at that position.
Determine if you is able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
"Problem Analysis"
The value of A[i] Indicates whether the last value of the array can be reached by starting from i = 0 if the maximum step to jump forward is reached.
Ideas
Use distance to record the farthest distance before the current I jump. If distance< I, that means even if again how jumps can not reach I. When I reach I, the a[i]+i, which represents the farthest distance from I can jump. Max (Distance,a[i]+i) represents the farthest distance that can be reached.
"Java Code"
1 Public classSolution {2 Public BooleanCanjump (int[] nums) {3 if(Nums = =NULL|| Nums.length = = 0)return true;4 5 intDistance = 0;6 for(inti = 0; I < nums.length && i <= distance; i++){7Distance = Math.max (nums[i]+I, distance);8 }9 returnDistance < nums.length-1?false:true;Ten } One}
Leetcode OJ 55. Jump Game