Leetcode题解系列45. Jump Game (c++版)
输入:
2,
3,1, 1,
4
输出:
2
代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() < 2) {
return 0;
}
int level = 0, current_max = 0, next_max = 0, i = 0;
while (i < nums.size()) {
level++;
for (; i <= current_max; i++) {
next_max = max(next_max, nums[i] + i);
if (next_max >= nums.size() - 1) return level;
}
current_max = next_max;
}
return level;
}
};
int main() {
char mk[100001];
int i = 0;
int count = 0;
vector<int> t;
while (cin >> mk[i])
{
char c = mk[i];
int a =c-'0';
if (a ==-132 )
continue;
t.push_back(a);
i++;
if (cin.get() == '\n')
{
break;
}
}
Solution so;
count = so.jump(t);
cout << count << endl;
system("pause");
return 0;
}