class Solution {
public int jump(int[] nums) {
if(nums.length == 1){
return 0;
}
int curDistance = 0;
int ans = 0;
int nextDistance = 0;
for(int i = 0; i< nums.length ; i++){
nextDistance = Math.max( i + nums[i],nextDistance);
if(i == curDistance){
ans++;
curDistance = nextDistance;
if(nextDistance >= nums.length - 1) break;
}
}
return ans;
}
}
代码随想录——45. 跳跃游戏 II
最新推荐文章于 2025-05-18 19:14:41 发布