LeetCode 55

时间:2022-05-17 18:29:49

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.

 /*************************************************************************
> File Name: LeetCode055.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Wed 11 May 2016 20:30:25 PM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> #define MAX(a,b) ((a)>(b) ? (a) : (b)) int canJump(int* nums, int numsSize) { int sum = ; int i;
for( i=; i<numsSize && sum<=numsSize; i++ )
{
if (i > sum)
{
return ;
}
sum = MAX( nums[i]+i, sum );
printf("sum is %d\n", sum);
}
return ;
} int main()
{
int nums[] = { ,,,, };
int numsSize = ; int ret = canJump( nums, numsSize );
printf("%d\n", ret); return ;
}