Leetcode問題解シリーズ45.Jump Game(c++版)


Leetcode問題解シリーズ45.Jump Game(c++版)
入力:
2,
3,1,    1,
4

出力:
2

コード:
#include 
#include 
#include 

using namespace std;

class Solution {
public:
	int jump(vector& 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 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() == '
') { break; } } Solution so; count = so.jump(t); cout << count << endl; system("pause"); return 0; }