欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

跳跃游戏

程序员文章站 2022-04-24 14:12:49
...

链接
跳跃游戏

力扣运行代码:

bool canJump(int* nums, int numsSize){
    int longestJump = 0;
    for (int i = 0; i < numsSize; i++)
    {
        if (i>longestJump)
        {
            return 0;
        }

        if(longestJump<i+nums[i])
        {
            longestJump = i+nums[i];
        }
        if (longestJump >= numsSize-1)
        {
            return 1;
        }
        
    }
    return 1;
}

本地编译器运行代码:

//https://leetcode-cn.com/problems/jump-game/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>

int max(int a, int b)
{
    if (a>b)
    {
        return a;
    }
    return b;
}

int Jump(int a[],int n)
{
    int longestJump = 0;
    for (int i = 0; i < n; i++)
    {
        if (i>longestJump)
        {
            return 0;
        }

        longestJump = max(longestJump, i+a[i]);
        if (longestJump >= n-1)
        {
            return 1;
        }
        
    }
    return 1;
}

int main()
{
    int n;
    scanf("%d",&n);

    int a[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%d",a+i);
    }
    
    if(Jump(a,n))
    {
        printf("true");
    }
    else
    {
        printf("false");
    }
    
    return 0;
}

思路:

跳跃游戏

相关标签: 题库