Leetcode 45. Jump Game II

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Jump Game II

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int jump(vector<int>& nums) {
int current = 0;
int count = 0;
while(current < nums.size() - 1) {
count++;
if(current + nums[current] >= nums.size() - 1) {
break;
}
int distance = 0;
int index = current + 1;
int begin = current + 1;
int end = current + nums[current];
for(int i = begin; i <= end; i++) {
if(nums[i] + i > distance) {
distance = nums[i] + i;
index = i;
}
}
current = index;
}
return count;
}
};

Reference

  1. https://leetcode.com/problems/jump-game-ii/description/
如果有收获,可以请我喝杯咖啡!