zoukankan      html  css  js  c++  java
  • [LeetCode]Jump Game

    题目: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>
    #include<stdbool.h>
    
    bool canJump(int* nums, int numsSize) {
            bool flag = false;
        int i = 0;
        int range = nums[i] + i;
        if(range >= numsSize)return true;
        i++;
        while(range >= i && i < numsSize){
            range = range > nums[i] + i ? range : nums[i] + i;
            if(range >= numsSize){
                flag = true;
                break;
            }
            i++;
        }
        return flag;
    }
    
    void main(){
        //int nums[] = {2,3,1,1,4};
        //int nums[] = {3,2,1,0,4};
        //int nums[] = {3,2,1,3,4};
        printf("%d",canJump(nums,5));
    }

    自己考虑了一下,如果要求正好到数组的末尾才算能够到达,没个数组的值不再是范围内的下标都可以,而是必须到对应的下标位置,该怎么解。

    思路如下:遍历数组验证能否到达末尾,并标记访问过的值;遍历时,遇到访问过的跳过。

    失败的情况:

    访问的值+当前下标超过了数组的长度时,失败;

    访问的值+当前下标等于已访问过的下标时,失败;

    /***************************************************************************************************
    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>
    #include<stdbool.h>
    
    /**
     *严格判定,只要不能正好跳到最后就为false
     */
    bool checkRoad(int *indexs,int *visit,int size,int step){
        if(step == size - 1)return true;
        if(step >= size)return false;
        if(visit[step] == 1)return false;
        visit[step] = 1;
        return checkRoad(indexs,visit,size,indexs[step]);
    }
    
    bool canJump(int* nums, int numsSize) {
        int *newNums = (int *)malloc(numsSize*sizeof(int));
        for(int i = 0;i < numsSize;i++){
            newNums[i] = nums[i] + i;
        }
    
            int *visit = (int *)malloc(numsSize*sizeof(int));
            memset(visit,0,numsSize*sizeof(int));
            bool ret = checkRoad(newNums,visit,numsSize,newNums[0]);
        //for(int i = 0);
    
        free(visit);
        free(newNums);
        return ret;
    }
    
    void main(){
        //int nums[] = {2,3,1,1,4};
        //int nums[] = {3,2,1,0,4};
        int nums[] = {3,2,1,3,4};
        printf("%d",canJump(nums,5));
    }

    同类型的题目:http://www.cnblogs.com/yeqluofwupheng/p/6755622.html

  • 相关阅读:
    一款由张鹏老师录制的一周HOLD住HTML+CSS视频教程分享给大家
    一款LAMP兄弟连最近录制的《HTML5视频教程》此款视频不错哟!上吧
    一款HTML5的基础视频教程分享给大家,希望大家好好学习啊。
    分享一款由杨中科老师主讲的javascript视频教程,属于.NET课程是视频教程
    今天给大家带来的视频教程是LINUX视频教程,希望大伙能在里面学到你想要的!
    周一好亲们!今天还给大家分享的是Oracle视频教程,来自于传智播客!
    零晨了,为大家分享一套很好的javascript视频教程!喜欢的拿走啊。
    最新为大家整理的一套android视频教程,有兴趣的便宜可以去看看!
    .net的基础学习,.NET视频教程
    yii2 (not set), GridView
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6661395.html
Copyright © 2011-2022 走看看