1. 题目

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.

2. 思路

第一个节点必须要进入,每次进入一个起点i之后,下一步的落点可以是[i+1, i+nums[i]],找到这个范围内的可以跳跃最远的点作为下一步。循环直到跳跃到最后。

3. 代码

耗时:13ms

class Solution {
public:// 当做到第i步时,此时最大可以跳跃到的点时i+a[i]; // 下一步找到[i, i+a[i]]之间的最大可跳跃点, 下一步选择这个最大点// 循环往复直到到最后一个点int jump(vector<int>& nums) {int sz = nums.size();if (sz <= 1) { return 0; }int k = 0;int i = 0;int i_max = 0;while (i < sz - 1) {k++;int i_max = i + nums[i]; // 当前跳跃段的起点if (i_max >= sz - 1) { break; }int j_max = -1;int j_max_idx = -1;for (int j = i + 1; j <= i_max && j < sz; j++) { // 选择[i, i+nums[i]]段内的最大跳跃点作为下一点int tmp_max = j + nums[j];if (tmp_max > j_max) {j_max = tmp_max;j_max_idx = j;}}i = j_max_idx;}return k;}
};

【leetcode】45. Jump Game II 非负数组的最少跳跃步数相关推荐

  1. LeetCode 45. Jump Game II--Python解法--动态规划

    题目地址:Jump Game II - LeetCode Given an array of non-negative integers, you are initially positioned a ...

  2. 45. Jump Game II

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

  3. 45. Jump Game II(跳跃游戏II)

    原题链接:https://leetcode.com/problems/jump-game-ii/ 这道题比较适合用贪心来做,局部的最优解最后刚好是整体的最优解. 如下图,开始的位置是 2,可跳的范围是 ...

  4. 45 jump game II(贪心)

    文章目录 1:题目描述 2:解决方法 1:题目描述 给一个元素非负的数组,你开始是在第一个元素上 每一个元素都代表你在当前位置最大可跳的长度 你的目标是到达最后一个位置,用最小的跳数 你总是可以到达最 ...

  5. LeetCode T45 Jump Game II

    文章目录 题目地址 题目描述 思路 题解 题目地址 https://leetcode-cn.com/problems/jump-game-ii/ 题目描述 Given an array of non- ...

  6. 【贪心】LeetCode 55. Jump Game

    LeetCode 45. Jump Game II 参考网址:http://www.cnblogs.com/grandyang/p/4373533.html Solution1: 贪心算法,这里贪婪并 ...

  7. 55/45 Jump Game 跳跃游戏

    55 Jump Game 思路:贪心算法,每次记录可以跳到的最远距离. class Solution { public:bool canJump(vector<int>& nums ...

  8. Leetcode 45. 跳跃游戏 II

    Leetcode 45. 跳跃游戏 II 题目 给你一个非负整数数组 nums ,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组 ...

  9. leetcode 45. 跳跃游戏 II 46. 全排列

    leetcode 45. 跳跃游戏 II 46. 全排列 45. 跳跃游戏 II 难度中等1273收藏分享切换为英文接收动态反馈 给你一个非负整数数组 nums ,你最初位于数组的第一个位置. 数组中 ...

最新文章

  1. Dispatcher与UI线程交互
  2. python csv模块 一次读多行_python中csv模块读取reader只能读取一次
  3. Python使用sklearn构建广义线性模型:泊松回归(Poisson regression)实战
  4. html 查询表单,如何让我的HTML表单查询适用于所有表单元素?
  5. 科大星云诗社动态20220113
  6. vue项目职责_进大厂兼职的机会来了!腾讯微校项目招人了!
  7. 答CSDN关于建模的系列问题
  8. java连接hbase时出现....is accessible from more than one module:
  9. C#LeetCode刷题之#500-键盘行(Keyboard Row)
  10. js中图片获取src的正则
  11. Spark调优 数据本地化调优
  12. java中动态代理的使用
  13. python获取图像灰度极值点_【图像处理】灰度图、亮度峰值极值查找
  14. android建脚本,Android Studio Gradle构建脚本(示例代码)
  15. 国外免费图片素材站收集
  16. 软考(网络工程师)-必备网络知识(笔记一)
  17. UserAgent个人整理
  18. 相机快门之:电子快门
  19. 最易懂得 鸿蒙 实战 - 真机调试 原子服务
  20. 物联网安全与隐私保护之物联网安全体系

热门文章

  1. 也谈压缩感知和贝叶斯大脑
  2. 基础理论研究是人工智能持续发展的保证
  3. Yann LeCun 怒喷 Sophia:这就是彻头彻尾的骗局
  4. 机器学习 TOP 10 必读论文 | 资源
  5. 太任性!00 后少年买不到回国机票,因“泄愤”找黑客攻击系统,被判刑 4 年
  6. iOS 工程师被要求写小程序,过分吗?| 每日趣闻
  7. doT.js-doT模板方便快捷的组织页面DOM
  8. MySQL 5.6通过Keepalived+互为主从实现高可用架构
  9. ElasticSearch IK分词器安装
  10. 注册中心 Eureka 源码解析 —— 应用实例注册发现(五)之过期