class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() <= 1){
return0;
}
int firstNum = nums[0];
if (firstNum == 0){
return0;
}
int scope = firstNum;
int step = 0;
int from = 1;
while (scope < nums.size() - 1){
int currentScope = scope;
for (int i = from; i <= scope; i++){
if (currentScope < i + nums[i])
currentScope = i + nums[i];
}
step++;
from = scope + 1;
scope = currentScope;
}
return step + 1;
}
};
在LeetCode上刷的第三题,尝试了几种方法,最后还是选择贪心,复杂度为O(n),个人感觉有不少的收获。该题的题目是这样的,给定一个非负整数数组,每个元素代表着在当前位置最大能跳多少步,而我们的目标是从第一个位置开始,通过最少的步数到达最后一个位置。例如,给定数组是 A = [2,3,1,1,4],则只需两步就能到达终点(第一个位置跳1步到达第二个位置,第二个位置上跳三步到达最后一个位置)。首