本题源自leetcode 45
-------------------------------------------------------------------------
思路:用俩个 变量标识当前的位置 和 下一步能调到最远的位置。
代码:
int jump(vector<int>& nums) {
int n = nums.size();
if(n <= 1)
return 0;
int cur = nums[0]; //当前跳到的最远距离
int next = nums[0]; //下一步能跳到的最远距离
int step = 1;
for(int i = 1; i< n; i++){
if(cur < i){
cur = next;
step++;
}
if(i + nums[i] > next){
next = i + nums[i];
}
if(cur >= n-1)
return step;
}
return 0;
}