通过构建当前位置可以到达的位置来更新最大值索引,如果走完之后可以走到终点,那么就可以,否则就是不可以。
/**
* 跳跃游戏
*/
bool judgeIsGoEnd(vector<int> nums)
{
vector<int> jumpIndex;
//构建最大索引跳跃集合
for (int i = 0; i < nums.size(); i++)
{
jumpIndex.push_back(i + nums[i]);
}
int maxIndex = nums[0];
int jumpCur = 0;
while (jumpCur < nums.size() && jumpCur <= maxIndex)
{
if (maxIndex < jumpIndex[jumpCur])
{
maxIndex = jumpIndex[jumpCur];
}
jumpCur++;
}
if (jumpCur == nums.size())
{
return true;
}
return false;
}