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:
[2,3,1,1,4] , Return true
.
a = [3,2,1,0,4]
, Return false
.
Idea: Using greedy strategy, initialize set Step=nums[0], to the next step--, and take Step=max (Step,nums[i]), so that step has remained the maximum number of moving steps, if the step<=0, indicating can not reach I position, Then return FALSE. The entire loop comes down without returning a description to be able to reach the end of the position smoothly, returning true.
classSolution { Public: BOOLCanjump (vector<int>&nums) { if(Nums.size () <=1) return true; intStep =nums[0]; for(intI=1; I<nums.size (); i++){ if(step>0) {Step--; Step=Max (step,nums[i]); } Else return false; } return true; }};
55.Jump Game