一天一道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. ACM Secrete Master Plan

    Problem Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. T ...

  2. Docker控制组

    控制组是 Linux 容器机制的另外一个关键组件,负责实现资源的审计和限制. 它提供了很多有用的特性:以及确保各个容器可以公平地分享主机的内存.CPU.磁盘 IO 等资源:当然,更重要的是,控制组确保 ...

  3. 配置 docker0 网桥

    Docker 服务默认会创建一个 docker0 网桥(其上有一个 docker0 内部接口),它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和本地主机都放到同一个物理网络. Docker 默认 ...

  4. opencv之人脸识别

    最近在做一个类似于智能广告投放的项目,简单思路是利用opencv获取摄像头图像,然后调用接口或利用其他一些离线模型进行人脸属性识别,进而投放广告.本篇先简单介绍利用opecv进行人脸识别. # -*- ...

  5. Microsoft Dynamics 365 Developer Toolkit下载地址

    下载,支持Visual Studio 2012, 2013, 2015

  6. win10+ubuntu双系统安装方案

    网上有很多教程,大多是win7,win8的,我折腾了一天,今天终于都安装好了,折腾的够呛,很多人都说挺简单的,嗯其实的确很简单,很多人回复说安装不成功,很有可能就是电脑安全权限的问题,我用的是华硕的电 ...

  7. 快速了解 Robot Operating System(ROS) 机器人操作系统

     http://www.ros.org/ 关于ROS About ROS http://www.ros.org/about-ros/ 机器人操作系统(ROS)是用于编写机器人软件的灵活框架.目的在简化 ...

  8. Unity插件 - MeshEditor(八)模型镜像特效

    将静态模型(带MeshFilter)按指定轴向.指定距离克隆一个镜像物体出来,思路很简单,将模型的顶点坐标按指定轴取反,并累加上设定的距离值,然后就完毕了!不过,因为镜像体的顶点镜像于之前模型的顶点, ...

  9. 微信小程序之最简单的Demo设计使用

    这个小Demo,代码量不多:导航样式.View.Text.点击.JS交互的使用,主要是理解每个后缀文件的功能,然后才能更好的使用开发.......(下面代码和源代码没差别,实在想要的请留言,谢谢... ...

  10. Java基本语法-----java注释

    1注释的作用 通过注释提高程序的可读性,是java程序的条理更加清晰,易于区分代码行与注释行.另外通常在程序开头加入作者,时间,版本,要实现的功能等内容注释,方便后来的维护以及程序员的交流. 2注释的 ...