55. Jump Game(LeetCode)

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.

 1 class Solution {
 2 public:
 3     bool canJump(vector<int>& nums) {
 4       int size=nums.size();
 5         if(size<=0){
 6             return false;
 7         }
 8         
 9         int maxJump=-1;
10         for(int i=0; i<size; i++){
11             if(nums[i]>maxJump){
12                 maxJump=nums[i];
13             }
14             
15             if(maxJump>=size-i-1){
16                 return true;
17             }
18             
19             if(maxJump==0){
20                 return false;
21             }
22             
23             maxJump--;
24         }
25         
26         return false;
27     }
28 };
原文地址:https://www.cnblogs.com/wujufengyun/p/6943692.html