Fork me on GitHub

55. Jump Game

55. Jump Game

题目

 Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

解析

class Solution_55 {
public:
	bool canJump(vector<int>& nums) {

		int maxend = 0;
		for (int i = 0; i < nums.size()&&maxend>=i;i++)
		{
			maxend = max(maxend,i+nums[i]);
		}

		if (maxend<nums.size()-1)
		{
			return false;
		}
		return true;
	}

	bool canJump(int A[], int n) {
		vector<int> vec(A, A + n);
		return canJump(vec);
	}
};

题目来源

posted @ 2018-03-13 21:43  ranjiewen  阅读(146)  评论(0编辑  收藏  举报