Java for LeetCode 055 Jump Game
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
解题思路:
参考Java for LeetCode 045 Jump Game II把返回值改为boolean即可,JAVA实现如下:
static public boolean canJump(int[] nums) {
int index=0,maxStepIndex=0,start=0;
while(nums.length>1){
for(int i=start;i<=index+nums[index];i++){
if(i+nums[i]>=nums.length-1)
return true;
if(i+nums[i]>=nums[maxStepIndex]+maxStepIndex)
maxStepIndex=i;
}
start=index+nums[index]+1;
if(index==maxStepIndex)
return false;
index=maxStepIndex;
}
return true;
}
Java for LeetCode 055 Jump Game的更多相关文章
- Java for LeetCode 045 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 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 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 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- 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 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- office 软件常用备忘(删除线/冻结等)
Word: 1 大纲显示/navigation display: 视图/View --> Navigation Pane (left) 2 Track changes: 显示删除线 / 修改地方 ...
- ZOJ1586 QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- Bookmarklet
学习 http://www.ruanyifeng.com/blog/2011/06/a_guide_for_writing_bookmarklet.html
- 15个Linux Wget下载实例终极指南
15个Linux Wget下载实例终极指南 Linux wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,尤其对于网络管理员,经常要下载一些软件或从远程服务器恢复备份到 ...
- Oracle 常用入侵命令
1.查看当前数据库实例名称:select * from v$instance;2.查看当前用户的角色:select * from user_role_privs;3.查看当前用户下所有的表:selec ...
- phpcms v9无法连接数据库服务器,请检查配置
安装phpcms v9是数据库信息配置正确,但仍提示:无法连接数据库服务器,请检查配置 1.修改install/step5.tpl.php 127行为:'&dbpw='+escape($('# ...
- IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm(转载)
IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm 自从本系列发布之后,收到了很多的朋友的回复!非常感谢,同时很多朋友问到了一些问题,有些问 ...
- 利用memcached构建高性能的Web应用程序(转载)
面临的问题 对于高并发高访问的Web应用程序来说,数据库存取瓶颈一直是个令人头疼的问题.特别当你的程序架构还是建立在单数据库模式,而一个数据池连接数峰 值已经达到500的时候,那你的程序运行离崩溃的边 ...
- log4j:ERROR setFile(null,true) call failed.java.io.FileNotFoundException: ..\logs\2010-1-19.log (系统找不到指定的路径。)
log4j:ERROR setFile(null,true) call failed.java.io.FileNotFoundException: ..\logs\2010-1-19.log (系统找 ...
- 新浪微博客户端(24)-计算原创微博配图frame
DJStatus.h #import <Foundation/Foundation.h> @class DJUser; /** 微博 */ @interface DJStatus : NS ...