每次选择的时候都选择最有利的,即跳得最远。
class Solution {
public:
int jump(vector<int>& nums) {
int s = nums.size();
if (s == 1) return 0;
int rtn = 0;
//一次循环就是一跳
for (int i = 0; i < s-1; rtn ++) {
int numi = nums[i];
//判断能否直接跳到末尾
if (i + numi >= s-1) return rtn+1;
//选择下一跳
int max = 0, tgt = -1;
for (int j = i+1; j <= i+numi; j ++) {
//下一跳的序号加上下一跳所能到达的最远距离
if (j+nums[j] >= max) {
max = j+nums[j];
tgt = j;
}
}
i = tgt;
}
return rtn;
}
};