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

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

具体代码例如以下:

public class Solution {
public int jump(int[] nums) {
/**
* 本题用贪心法求解,
* 贪心策略是在每一步可走步长内。走最大前进的步数
*/
if(nums.length <= 1){
return 0;
}
int index,max = 0;
int step = 0,i= 0;
while(i < nums.length){
//假设能直接一步走到最后,直接步数+1结束
if(i + nums[i] >= nums.length - 1){
step++;
break;
}
max = 0;//每次都要初始化
index = i+1;//记录索引。最少前进1步
for(int j = i+1; j-i <= nums[i];j++){//搜索最大步长内行走最远的那步
if(max < nums[j] + j-i){
max = nums[j] + j-i;//记录最大值
index = j;//记录最大值索引
}
}
i = index;//直接走到能走最远的那步
step++;//步长+1
}
return step;
}
}

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. java float与double的范围和精度

    float与double的范围和精度 1. 范围  float和double的范围是由指数的位数来决定的.  float的指数位有8位,而double的指数位有11位,分布如下:  float:  1 ...

  2. Unity 游戏开发技巧集锦之创建部分光滑部分粗糙的材质

    Unity 游戏开发技巧集锦之创建部分光滑部分粗糙的材质 创建部分光滑部分粗糙的材质 生活中,有类物体的表面既有光滑的部分,又有粗糙的部分,例如丽江的石板路,如图3-17所示,石板的表面本来是粗糙的, ...

  3. WSDL语法

    <什么是WSDL语言> WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及 ...

  4. PHPStorm设置调试

    先下载PHP扩展Xdebug https://xdebug.org, 可以复制自己的phpinfo粘贴到https://xdebug.org/wizard.php中, 会生成需要下载的版本, php. ...

  5. 【尺取法】POJ3061 & POJ3320

    POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...

  6. [AHOI2009]同类分布

    题目大意: 问在区间[l,r]内的正整数中,有多少数能被其个位数字之和整除. 思路: 数位DP. 极端情况下,每一位都是9,所以各位数字之和不超过9*18.(为了方便这里用了9*19) f[i][j] ...

  7. C++ -- STL泛型编程(二)之set

    set集合容器实现了红黑树的平衡二叉检索树的数据结构,在插入元素时候它会自动调整二叉树的排列,把元素放在适当的位置,以确保每个子树根节点的键值都大于左子树的所有节点的键值,而小于右子树的所有节点的键值 ...

  8. HDU 5653 Bomber Man wants to bomb an Array. dp

    Bomber Man wants to bomb an Array. 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5653 Description ...

  9. Druid 配置_StatViewServlet配置

    https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE Druid内置提供 ...

  10. [Asp.net]AspNetPager分页组件

    引言 在基于Asp.net的内网系统中,分页功能是最常用的,用的最多的组件就是AspNetPager. AspNetPager 官网:http://www.webdiyer.com/aspnetpag ...