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() == '\n')
		{
			break;
		}
	}
	
	Solution so;
	count = so.jump(t);
	cout << count << endl;
	system("pause");
	return 0;
}

你可能感兴趣的:(算法笔试,算法与数据结构,leetcode)