Given an array of non-negative integers, you are initially positioned atthefirst index ofthe array.
Each elementinthe array represents your maximum jump lengthat that position.
Determine if you are able to reach thelast index.
For example:
A = [2,3,1,1,4], returntrue.
A = [3,2,1,0,4], returnfalse.
class Solution {
public:
bool canJump(vector<int>& nums) {
int index = 0, size = nums.size();
for(int reach=0; index<size && index<=reach; ++index)
reach = std::max(index+nums[index], reach);
return index == size;
}
};