代码语言:javascript复制
class Solution {
public:
int jump(vector<int>& nums) {
// 起跳点的范围[st, ed)
int st = 0;
//
int ed = 1;
int ans = 0;
while (ed < nums.size()) {
int maxPos = 0;
// 更新最远的跳跃距离
for (int i = st; i < ed; i ) {
maxPos = max(maxPos, i nums[i]);
}
// 更新区间
st = ed;
ed = maxPos 1;
ans ;
}
return ans;
}
};