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. Arduino可穿戴教程之第一个程序——上传运行程序(四)

    Arduino可穿戴教程之第一个程序——上传运行程序(四) 2.4.5  上传程序 现在所有Arduino IDE的设置都完成了,我们就可以将示例程序上传到板子中了.这非常简单,只需要单击如图2.45 ...

  2. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

  3. python @classmethod 的使用场合

    python @classmethod 的使用场合 官方的说法: classmethod(function)中文说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实 ...

  4. FastReport.Net使用:[27]样式使用

    样式设置与使用 1.打开样式设置界面,通过 报表->样式 来打开. 2.样式设置包含:边框,填充,字体和文本颜色.假如不需要某项设置,可将其选择框去掉. 3.设置好样式后,将标题的style设置 ...

  5. PHP之 xampp 安装环境

    1.安装XAMPP  需要注意以下几点: (1):必须已管理员身份运行: (2):先点击安装Apache和mysql(如果apche端口被占用,先停止服务里面的apche服务) (3):别忘记切换PH ...

  6. hihocoder #1076 与链 dp

    直接背包不可做 我们只需要知道每个数位上有多少个$1$,那么我们就能构造出解 因此,我们对每一位讨论, 可以拆出$n + \frac{n}{2} + \frac{n}{4} + ... = 2n$个物 ...

  7. poj 1456 贪心+STL

    题意:有n个商品,每个商品如果能在截止日期之前售出就会获得相应利益,求能获得的最大利益 一开始对每个时间进行贪心,后来发现后面的商品可以放到之前来卖,然后就wa了 这里就直接对价格排序,把物品尽量放到 ...

  8. Java泛型应用总结

    一.泛型的引入原因 在操作集合的时候,之前方法的定义都是Object类型,向集合中添加对象,都自动向上转型,加入的元素可以是任何类型 但是,在取出元素的时候,通常想要使用对象的特有功能,就必须向下转型 ...

  9. JDK源码(1.7) -- java.util.AbstractList<E>

    java.util.AbstractList<E> 源码分析(JDK1.7) ------------------------------------------------------- ...

  10. PHP -- 页面传值的6种获取方法

    1.PHP4以后获取传值的方法 一般在页面中传值常见的是POST.GET和COOKIE几种,所以下面我也主要介绍这几种.PHP4以后都采用的是$_POST.$_GET等数组来获取网页传值.在PHP3. ...