55 跳跃游戏
维护最大可达位置nextStep
如果当前位置i <= nextStep,说明当前位置i可达,可跳到当前位置i上,更新最大可达位置
如果最大可达位置比n - 1大,说明能够到达n - 1
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
int nextStep = 0;
for(int i = 0; i < n; i++){
if(i <= nextStep){
nextStep = max(nextStep, i + nums[i]);
if(nextStep >= n - 1)
return true;
}
}
return false;
}
};