文章目录

  • 题目地址
  • 题目描述
  • 思路
  • 题解

题目地址

https://leetcode-cn.com/problems/jump-game-ii/

题目描述

Given an array of non-negative integers nums, 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.

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

Example 1:

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: 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.

Example 2:

Input: nums = [2,3,0,1,4]
Output: 2

Constraints:

  • 1 <= nums.length <= 3 * 104
  • 0 <= nums[i] <= 105

思路

贪心,找到从当前步跳到的步数加起来最远的位置,直到终点。

题解

class Solution {public int jump(int[] nums) {if(nums.length==1) return 0;int max_pos = 0;int next = 0;int step=0;for(int i=0;i<nums.length;){step++;if(i+nums[i]>= nums.length-1) return step;max_pos = 0;for(int j=0;j<=nums[i];++j){if(j+nums[i+j]>max_pos){max_pos = j+nums[i+j];next = j;}}i += next;}return step;}
}

LeetCode T45 Jump Game II相关推荐

  1. 【贪心】LeetCode 55. Jump Game

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

  2. 【leetcode】Jump Game I, II 跳跃游戏一和二

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

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

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

  4. Leetcode 45. 跳跃游戏 II

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

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

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

  6. leetcode 45.跳跃游戏 II

    一.题意 给出一个非负整数数组 nums ,数组中的每个元素代表在该位置可以跳跃的最大长度.使用最少的跳跃次数到达数组的最后一个位置.假设总是可以到达数组的最后一个位置. 二.解法 贪心算法. 解法一 ...

  7. Leetcode - Subsets I,II

    Leetcode - 078. Subsets 这道题重定义了什么叫可行解: 一般而言,可行解需要满足强约束性条件集,而本题的可行解就是单一弱约束性条件(distinct integers,只需要当前 ...

  8. Leetcode - Permutations I,II

    Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...

  9. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

最新文章

  1. 团体程序设计天梯赛 L2 题目合集
  2. ListGetandSetDemo 集合操作get set remove add方法
  3. [P2387魔法森林
  4. python多线程logger_python多线程logger_Dblogger:一个Python语言实现支持多进程和多线程日志打印高效Python日志库......
  5. 新年发财专属红包封面,高端大气上档次!
  6. 机器学习竞赛(代码)
  7. MessasgePack:一个小巧高效的序列化方式
  8. 经典 SQL 数据库笔试题及答案整理
  9. dosbox进行debug调试教程
  10. win7下ApmServ启动失败问题
  11. python的开发环境
  12. Java入门 二维数组、一维数组、多维数组的解析(第十三天)
  13. mac电脑怎么连接蓝牙鼠标?
  14. EasyCVR通过进程启动无报错,但是自动退出该如何解决?
  15. OsgEarth加载DEM(数字高程模型)
  16. 职场邮件的十大忌讳,你犯了吗?
  17. 基于STM32风速风向检测仿真
  18. 爬虫—爬取微博热搜榜
  19. Unity控制窗口全屏显示
  20. 信用卡融资的优势,这些你了解过吗?

热门文章

  1. 装修时千万别忽略媳妇的审美,这三款装修风格给她二次心动的感觉
  2. 屏的像素与传输速率_摄像头像素与分辨率(1080P)的关系
  3. T02 - 007、南京华苏科技有限公司
  4. android百度地图例子,Android使用百度地图的注意点
  5. 【EI快速检索会议】2021年图像处理与智能控制国际学术会议(IPIC 2021)
  6. access柏拉图_柏拉图的奇特案例:冷战互联网
  7. 我每天使用的5个Python自动化脚本
  8. 高校俱乐部参会成员餐票领取安排及第二届研讨会邀请信
  9. 监控--Prometheus
  10. windows kerberos认证报错:Cannot find KDC for requested realm