@Lin--
2020-02-24T14:01:30.000000Z
字数 509
阅读 339
Leetcode
依据题意:跳步最大值为该当前数组值,若能跳出。返回true,跳不出,返回false。
解题思路:贪心算法,当前能到达最大下标记录为temp,遍历nums[i]+i,若出现temp>numsSize,则说明能跳出去。否则跳不出去。
/*
*File:canJump.c:
*Author:0HP
*Date:20200224
*Purpose:to solve the problem in Leetcode
*https://leetcode.com/problems/jump-game/
*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
bool canJump(int* nums, int numsSize)
{
if(numsSize==0)return false;
if(numsSize==1)return true;
int temp=0;
for(int i=0;i<=temp;++i)
{
if(temp<nums[i]+i)
{temp=nums[i]+i;}
if(temp>=(numsSize-1)){return true;}
}
return false;
}