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

  • 相关阅读:
    MyBatis3.2从入门到精通第一章
    (转)浅析Java中的访问权限控制
    mysql添加索引命令
    (转)浅谈Java中的对象和对象引用
    (转)String、StringBuffer与StringBuilder之间区别
    (转)浅谈Java中的equals和==
    Java并发编程:Lock
    Java并发编程:synchronized
    安装MySQL
    Excel常用函数
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6661395.html
Copyright © 2011-2022 走看看