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. struts2面试题汇总

    一.工作原理 一个请求在Struts2框架中的处理大概分为以下几个步骤 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2 这个请求经过一系列的过滤器(Filter)(这些过滤器 ...

  2. 【前端学习】sublime开启vim模式

    学习目标:在sublime下开启vim模式,了解基本vim的编辑快捷键. 下载安装Sublime Text 3 :http://www.sublimetext.com/3 Vim/Vi: Vim/Vi ...

  3. codeforces 715B:Complete The Graph

    Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...

  4. Tomcat 404

    原因:servlet没有配置正确 ,查看web.xml确认正确,以及自己的请求路径正确 在IE中提示"404"错误有以下三种情况 1.未部署Web应用 2.URL输入错误 排错方法 ...

  5. HDU 2242 考研路茫茫----空调教室

    传送门 考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. 可以开心的用Markdown了

    1 计划 月计划 周计划 日计划 2 实现

  7. mongodb(基础用法)

    驱动和客户端库 https://mongodb-documentation.readthedocs.org/en/latest/ecosystem/drivers.html#id2 https://m ...

  8. php-fpm.conf两个至关重要的参数

    这里规定了PHP-CGI的连接.发送和读取的时间,300秒足够用了,因此我的服务器很少出现504 Gateway Time-out这个错误.最关键的是php-fpm.conf的设置,这个会直接导致50 ...

  9. SpringMVC 返回JSON数据

    首先添加json包

  10. 使用XtraGrid自定义列计算1 z

    Devexpress控件集提供的DataGrid控件,在功能和界面样式上都完爆WinForm的DataGridView控件,以前需要在 DataGridView控件上进行某列的统计,需要在GridVi ...