• [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

  • 相关阅读:
    java+selenium+new——同一个标签窗口里 ,访问多个网页的后退driver.navigate().back()、前进driver.navigate().forward()、刷新driver.navigate().refresh()等功能 。以及获取当前页面的title属性driver.getTitle()和获取当前页面的url地址driver.getCurrentUrl()
    SoapUI接口测试——关联——参数化
    SoapUI接口测试——添加测试套件——new TestSuite——(类似于postman里面的集合)——添加测试步骤——teststeps(测试步骤)
    java+selenium+new——获取网页源代码driver.getPageSource()
    g++命令行详解
    hdoj_1503Advanced Fruits
    指针遍历vector向量
    最长公共子序列
    hdoj_1087Super Jumping! Jumping! Jumping!
    pcc32应用1
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6661395.html
Copyright © 2020-2023  润新知