LeetCode 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 .
 
以前のJump Game IIと似ていますが、今回は到着できるかどうかを判断します.
同じ考え方で、具体的にはJump Game IIを参照してください.
2つのステップの間に到達できる最も遠い距離が変わらない場合(ゴールを超えていない場合)、ゴールが到達できないことを示します.
 
コード:
class Solution {
public:
    bool canJump(int A[], int n) {
		int last_end=-1,end=0;	//              ,           
		int i,temp;
		while(end<n-1)
		{
			temp=end;
			for(i=last_end+1;i<=end;i++)
				if(i+A[i]>temp)
					temp=i+A[i];
			last_end=end;
			end=temp;
			if(end==last_end)
				break;
		}
		if(end>=n-1)
			return true;
		else
			return false;
    }
};