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.
#include<iostream>
#include<vector>
#include<Windows.h>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
int lar = ;
int i = ;
for (i = ; i < nums.size() && i<=lar; i++) {
lar = max(nums[i] + i, lar);
}
return i == nums.size();
} bool canJump2(vector<int>& nums) {
int overflow = ; // 当前可以覆盖到的下标
for (int i = ; i < nums.size() - ; i++) {
if (overflow < i) { return false; } // 提前中断, 走不下去了
int k = i + nums[i];
if (k > overflow) { overflow = k; }
}
return overflow >= nums.size() - ;
}
};