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

解题思路:

题目不难,只需每次找出向右的最大范围的指针maxStepIndex即可,为了减少遍历,可以用一个指针start维护每次遍历的起始位置,JAVA实现如下:

static public int jump(int[] nums) {
int result=0,index=0,maxStepIndex=0,start=0;
if(nums.length>1&&0+nums[0]>=nums.length-1)
return 1;
while(index<nums.length-1){
result++;
for(int i=start;i<=index+nums[index];i++){
if(i+nums[i]>=nums.length-1)
return result+1;
if(i+nums[i]>=nums[maxStepIndex]+maxStepIndex)
maxStepIndex=i;
}
start=index+nums[index]+1;
index=maxStepIndex;
}
return 0;
}

Java for LeetCode 045 Jump Game II的更多相关文章

  1. LeetCode 045 Jump Game II

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

  2. Java for LeetCode 055 Jump Game

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

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

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

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

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

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. [leetcode]45. Jump Game 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 (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

随机推荐

  1. PostConstruct

    Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解 1.6. @PostConstr ...

  2. 【codevs1257】 打砖块

    http://codevs.cn/problem/1257/ (题目链接) 题意 在等腰三角形上打砖块,总共有m发炮弹,每块砖有一个权值,求打出的最大权值 Solution 今天考试题,考场上的2个小 ...

  3. BZOJ2186 欧拉函数

    欧拉函数:一般记作φ(n),表示1-n中与n互质的数的数量. 欧拉函数是积性函数,即φ(m*n)=φ(m)*φ(n) //这条定理基友面试时还遇到了= = 欧拉函数的值φ(n)=n*(1-p[1])* ...

  4. POJ2263 Heavy Cargo

    Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4004   Accepted: 2124 Descr ...

  5. (转)eclipse项目导入到android studio中

    原文:http://www.cnblogs.com/lao-liang/p/5016541.html?utm_source=tuicool&utm_medium=referral Androi ...

  6. java 文件读取大全

    1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**      * 以字节为单位读取文件,常用 ...

  7. linux性能监测与优化

    top命令命令功能top命令可以实时动态地查看系统的整体运行情况,是一个综合了多方信息的监测系统性能和运行信息的实用工具.命令语法top(选项)选项说明-b:以批处理模式操作;-d:屏幕刷新间隔时间. ...

  8. CSS3系列三(与背景边框相关样式 、变形处理、动画效果)

    与背景相关的新增属性 大家都知道在HTML页面中,元素都是由以下几部分组成 使用background-clip来修改背景的显示范围,如果设定为border-box,则背景范围包含边框区域,如果设定为p ...

  9. 中国天气网-天气预报接口api

    中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...

  10. php网站验证码的生成

    <?php header("Content-type:text/html;charset=utf-8"); header("Content-type:image/p ...