Jump Game II



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.)

思路:本题是典型的贪心算法。题目难度上也不算难,贪心策略是每步前进的地方是下一步能达到的地方最远。

具体代码例如以下:

  1. public class Solution {
  2. public int jump(int[] nums) {
  3. /**
  4. * 本题用贪心法求解,
  5. * 贪心策略是在每一步可走步长内。走最大前进的步数
  6. */
  7. if(nums.length <= 1){
  8. return 0;
  9. }
  10. int index,max = 0;
  11. int step = 0,i= 0;
  12. while(i < nums.length){
  13. //假设能直接一步走到最后,直接步数+1结束
  14. if(i + nums[i] >= nums.length - 1){
  15. step++;
  16. break;
  17. }
  18. max = 0;//每次都要初始化
  19. index = i+1;//记录索引。最少前进1步
  20. for(int j = i+1; j-i <= nums[i];j++){//搜索最大步长内行走最远的那步
  21. if(max < nums[j] + j-i){
  22. max = nums[j] + j-i;//记录最大值
  23. index = j;//记录最大值索引
  24. }
  25. }
  26. i = index;//直接走到能走最远的那步
  27. step++;//步长+1
  28. }
  29. return step;
  30. }
  31. }

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法的更多相关文章

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

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

  2. [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 ...

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

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

  4. leetcode 113. Path Sum II (路径和) 解题思路和方法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

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

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

  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 解题思路

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

  8. 【LeetCode每天一题】Jump Game II(跳跃游戏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

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

随机推荐

  1. [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索

    [HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...

  2. bzoj 4094: [Usaco2013 Dec]Optimal Milking

    4094: [Usaco2013 Dec]Optimal Milking Description Farmer John最近购买了N(1 <= N <= 40000)台挤奶机,编号为1 . ...

  3. bzoj 1010 斜率优化DP

    我的第二道斜率DP. 收获: 1.假设两个位置:p<q<i,然后让某一位置优,看其满足什么性质,所谓斜率优化就是满足: (g[q]-g[p])/(f[q]-f[p])  op h[i] 要 ...

  4. AtCoder Beginner Contest 022 A.Best Body 水题

    Best Body Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://abc022.contest.atcoder.jp/tasks/abc02 ...

  5. JQ 使用模板

    1.首先需要添加模板JS文件,

  6. CentOS 6.9设置阿里云源/163源

    阿里云: 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS ...

  7. Scrapy入门教程(转)

    关键字:scrapy 入门教程 爬虫 Spider作者:http://www.cnblogs.com/txw1958/出处:http://www.cnblogs.com/txw1958/archive ...

  8. SPOJ 7001. Visible Lattice Points (莫比乌斯反演)

    7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...

  9. POJ 1286 Necklace of Beads(Polya简单应用)

    Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...

  10. MySQL在windows系统的安装

    原文:https://blog.csdn.net/wokaowokaowokao12345/article/details/76736152 MySQL在windows系统的安装 原创 2017年08 ...