您的位置:首页 > 其它

LeetCode 55

2016-05-20 21:49 218 查看
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 = 0;

int i;
for( i=0; i<numsSize && sum<=numsSize; i++ )
{
if (i > sum)
{
return 0;
}
sum = MAX( nums[i]+i, sum );
printf("sum is %d\n", sum);
}
return 1;
}

int main()
{
int nums[] = { 2,3,1,1,4 };
int numsSize = 5;

int ret = canJump( nums, numsSize );
printf("%d\n", ret);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: