一天一道LeetCode系列

(一)题目

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.

Your goal is to reach the last index in the minimum number of jumps.

For example:

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

Note:

You can assume that you can always reach the last index.

(二)解题

1、首先想到的做法就是对于序号i,判断i+nums[i]能不能到达终点,如果能直接return,如果不能对小于nums[i]的数依次进行递归,得到最小的min值。这种做法直接超时了,TLE!

class Solution {
public:
    int min;
    int jump(vector<int>& nums) {
        min = nums.size();
        jumpdfs(nums,0,0);
        return min;
    }
    void jumpdfs(vector<int>& nums , int idx , int count)
    {
        int len = nums.size();
        if(idx >= len -1) {
            min = count;
            return;
        }
        if(idx<len && idx + nums[idx] >= len-1){
            min = min>(count+1)?(count+1):min;
            return;
        }
        else
        {
            for(int i = 1 ; idx<len && i<= nums[idx] ; i++)
            {
                jumpdfs(nums,idx+i,count+1);
            }
        }
    }

};

2、然后看到提示中有提到贪心算法,于是联想到昨天做的通配符那题,可以记录上一跳能达到的最远位置,和当前跳所能到达的最远位置,每次循环就更新这两个值,直到上一跳能达到终点就退出。

/*
思路:贪心算法是假定每一次都作出当前的最优解,所以对于本题,每次记录当前的最远位置,和上一跳的最远位置。
*/
class Solution {
public:
    int jump(vector<int>& nums) {
        int ret = 0 ;
        int last = 0;
        int cur = 0;
        for(int i = 0 ; i < nums.size() ; i++)
        {
            if(last>=nums.size()-1) break;
            if(i>last)//如果当前位置已经跳出了上一跳的最远位置
            {
                last = cur;//更新上一跳的最远位置等于当前跳的最远位置
                ++ret;//跳的次数加1
            }
            cur = max(cur , i+nums[i]);//更新当前跳的最远位置
        }
        return ret;
    }
};

【一天一道LeetCode】#45. Jump Game II的更多相关文章

  1. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  2. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [LeetCode] 45. Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 45. Jump Game II 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  8. Leetcode 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. [leetcode] 45. Jump Game II(hard)

    原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...

  10. [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)

    https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...

随机推荐

  1. pg备份恢复与设置编码

    psql create database jasygl encoding='UTF8' TEMPLATE = template0; 命令行 备份 pg_dump dabase_name > ba ...

  2. Bootstrap3 代码-用户输入

    通过 <kbd> 标签标记用户通过键盘输入的内容. To switch directories, type cd followed by the name of the directory ...

  3. Swift基础之使用Alamofire库进行网络请求和断点下载

    好久没有写过Swift相关的文章博客了,这里我就展示一下关于使用Alamofire库的方法 1.什么是Alamofire (1)Alamofire 的前身是 AFNetworking.AFNetwor ...

  4. Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  5. Dynamics CRM FORM脚本库加载本地脚本

    用传统的开发方式,js脚本都是保存在数据库中的,这样也方便迁移,但如果不想存数据库而是存在物理磁盘上,则可通过下述代码,将脚本存放在CRMWEB文件夹的某个路径下,每次都动态引用本地JS. funct ...

  6. SpriteKit中的共享动作(Sharing Actions)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在SpriteKit中某些动作需要一些额外的延时,如果每次都重 ...

  7. Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)

    demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...

  8. Microsoft公司的匈牙利法命名规则

    Microsoft公司的"匈牙利"法命名规则 比较著名的命名规则当推Microsoft公司的"匈牙利"法,该命名规则的主要思想是"在变量和函数名中加入 ...

  9. Android简易实战教程--第二十二话《自定义组合控件模拟qq登录下拉框和其中的一些”小技巧”》

    转载此文章请注明出处:点击打开链接   http://blog.csdn.net/qq_32059827/article/details/52313516 首先,很荣幸此专栏能被CSDN推荐到主页.荣 ...

  10. 福利:工作经常用到的Mac软件整理(全)

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博!iOS开发者交流QQ群: 446310206 前言 这是我个人在工作中会用到的Mac软件,其中包括办公.开发.视频等 ...