Java for LeetCode 045 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.)
解题思路:
题目不难,只需每次找出向右的最大范围的指针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的更多相关文章
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- Java for LeetCode 055 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 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 ...
- 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 ...
- 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 ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
随机推荐
- Query对象与DOM对象之间的转换方法
转自http://www.jquerycn.cn/a_4561 刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介 ...
- 44.Android之Shape设置虚线、圆角和渐变学习
Shape在Android中设定各种形状,今天记录下,由于比较简单直接贴代码. Shape子属性简单说明一下: gradient -- 对应颜色渐变. startcolor.endcolor就不多说 ...
- Bringing up interface eth0: Error:Connection activation failed:Device not managed by NetworkManager
Just follow the below steps and everything will be ok... 1. Remove Network Manager from startup Se ...
- [IOS Block和delegate的对比]
原文:http://blog.sina.com.cn/s/blog_9e8867eb0102uykn.html 这篇文章建议和前一篇一起看, 另外先弄清楚IOS的block是神马东东. 委托和bloc ...
- 用requests库实现登录遇到的问题
想登录zhihu,然后总是得到403 foribidden的错误,各种谷歌百度,得到结论说是输入错误或者是url错误,用fldder发现的确是url错了,post的地址是错误的 ==. 开始以为是#s ...
- POJ1753Flip Game(DFS + 枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37050 Accepted: 16122 Descr ...
- Activity的成员变量
// set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. ...
- PHP防止重复提交表单(helloweba网站经典实例)
<?php session_start(); header("Content-Type:text/html;charset:utf8"); function set_toke ...
- ASP+ACCESS手工注入详解
SQL注入这么长时间,看见有的朋友还是不会手工注入,那么我来演示一下.高手略过. 我们大家知道,一般注入产生在没经过虑的变量上,像ID?=XX这样的. 下面以这个网址为例: http://zsb.xx ...
- centos命令
alt + z 打开终端(自定义命令) su 切换到root