题目
C代码
int jump(int* nums, int numsSize) {int i = 0;int j = 0;int last_i = 0;int last_can = 0;int max_i = 0;int max_can = 0;int min_jump = 0;if (numsSize < 2) {//注意点1:数组小于两个的时候,只需要跳转0次;goto end;}// 注意点2: 当数组数量大于1个的时候,最小跳才是1;因为都是从nums[0]开始跳;min_jump++;last_can = nums[last_i] + last_i;max_can = nums[max_i] + max_i;for (i = 1; i < numsSize; i++) {if ((last_i + nums[last_i]) < i) {min_jump++;last_i = max_i;last_can = max_can;} else {//nothing to do}// 注意点3: 需要完成跳动后,再把最后这个值更新进系统,因为如果当前这个为最后的值;if ((nums[i] + i) < max_can) {//nothing to do} else {max_can = nums[i] + i;max_i = i;}}
end:return min_jump;
}