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


题解:跟Jump Game十分相像,把原来的boolean数组改成int型数组,用来记录到达i时候最小的步骤数目,遍历j(j<i),考察从j跳到i需要的步骤数目是否小于当前的reach[i],如果小于,就更新reach[i]。最终返回reach[A.length-1]即可。

代码如下:

 public class Solution {
public int jump(int[] A) {
if(A == null || A.length == 0)
return 0; int[] reach = new int[A.length];
Arrays.fill(reach, Integer.MAX_VALUE);
reach[0] = 0; for(int i = 1;i < A.length;i++){
for(int j = 0;j < i;j++){
if(reach[j] != Integer.MAX_VALUE && j+A[j]>=i){
reach[i] = Math.min(reach[i], reach[j]+1);
break;
}
}
} return reach[A.length-1];
}
}

【leetcode刷题笔记】Jump Game II的更多相关文章

  1. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. 【leetcode刷题笔记】N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  7. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  8. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. SubVersion(SVN)的安装配置使用

    一. SubVersion服务器端安装 安装软件:Setup-Subversion-1.6.4.msi,下载地址:http://subversion.tigris.org/servlets/Proje ...

  2. jfreechart折线图 demo

    public class ChartUtil { public static ChartUtil chartUtil; private RoomViewsDataService roomViewsDa ...

  3. 微信公众号开发---上传临时素材到公众号遇到的问题:"errcode":41005,"errmsg":"media data missing

    1.上传临时素材到公众号遇到的问题:"errcode":41005,"errmsg":"media data missing 解决办法:因为php版本 ...

  4. dwr文件上传

    配置FileService映射: dwr.xml <create creator="new"> <param name="class" val ...

  5. cobbler pxe-menu

    对应的文件在 /var/lib/tftpboot/pxelinux.cfg下 如果profile的pxe-menu设置为1的话,就可以默认显示在menu上了.可以手动选择要下发哪一个profile. ...

  6. 【cocos2dx 3.3】口袋空战5 总结与公布

    打包好的APK:点击下载

  7. Cocos2d-x 3.2 之 别踩白块(第三篇)

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  8. Android JNI开发之NDK环境搭建

    参考:http://www.cnblogs.com/yejiurui/p/3476565.html 谷歌改良了ndk的开发流程,对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本, ...

  9. 二、Android应用的界面编程(六)ProgressBar及其子类[SeekBar、RatingBar]er

    通常用于向用户显示某个耗时操作完成的百分比.Android支持几种风格的进度条,通过style属性可以为ProgressBar指定风格.该属性支持如下几个属性值. # @android:style/W ...

  10. 基于SqlDependency的Asp.net数据缓存

    首先,确保目标数据库的is_broker_enabled已经enabled. SELECT name, is_broker_enabled FROM sys.databases 如果不是enabled ...