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

  • 相关阅读:
    ES6---async, await, promise 综合例子
    ES6---Promise应用: async, await
    ES6---Promise 4: 更多案例
    掌握这两个技术点,你可以玩转AppCan前端开发
    AppCan4.0:开发者要做有价值的APP
    以“掌上东航”为例,论混合开发在企业级项目中的实践
    基于AppCan MAS系统,如何轻松实现移动应用数据服务?
    正益移动王国春:布局在是与不是之间
    【TOP10 APP】这些应用成了AppCan千人大会的焦点
    我爱我家:我为什么选择AppCan?
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6661395.html
Copyright © 2011-2022 走看看