Jump Game leetcode java
题目:
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.
题解:
参考了http://fisherlei.blogspot.com/2012/12/leetcode-jump-game.html的代码
很巧妙的解法,代码很短,看着一眼就能看懂,但自己想不见得想的出来,好好品味一下
代码如下:
1 public boolean canJump(int[] A) {
2 int maxCover = 0;
3 for(int start =0; start<= maxCover && start<A.length; start++)
4 {
5 if(A[start]+start > maxCover)
6 maxCover = A[start]+start;
7 if(maxCover >= A.length-1)
8 return true;
9 }
return false;
}
Jump Game leetcode java的更多相关文章
- Jump Game - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Jump Game - LeetCode 注意点 解法 解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能.并且如果 ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Jump Game II leetcode java
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Leetcode】经典的Jump Game in JAVA
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode][Java] Jump Game II
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- FPGA+ARM or FPGA+DSP?
网上有人说.现在的FPGA,ARM功能已经强大到无需DSP协助处理了,未来DSP会不会消声灭迹?是DSP取代FPGA和ARM,还是ARM,FPGA取代DSP呢?担心好不容易学精了DSP,结果DSP变成 ...
- react篇章-React State(状态)-数据自顶向下流动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- COCO 数据集使用说明书
下面的代码改写自 COCO 官方 API,改写后的代码 cocoz.py 被我放置在 Xinering/cocoapi.我的主要改进有: 增加对 Windows 系统的支持: 替换 defaultdi ...
- java-Excel导出中的坑
在Excel导出过程中,若遇到合并单元格样式只有第一行合并,而下面要合并的行没有边框显示. 一般问题出在将单元格样式设置与合并单元格放在同一个循环中导致. 以下为一个完整版的demo以供参考 定义边框 ...
- hdu1242Rescue
STL容器之优先队列 优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关键字是priority_queue(太伤了).在一些定义了权重的地方这个数据结构是很有用的. 先回顾队列的定义 ...
- parcelable 和 serializable 区别
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha serializable 是 java 中的 序列化 接口. parcelable 是 ...
- 20162327WJH2016-2017-2《程序设计与数据结构》课程总结
20162327WJH2016-2017-2<程序设计与数据结构>课程总结 一.每周作业链接汇总 第一周作业:算法分析 第三周作业:查找与排序 第五周作业:有关栈的总结 第七周作业:树的有 ...
- 【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)
Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills, every ski ...
- python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名
import MySQLdb #connect try: conn = MySQLdb.connect( host = "localhost", user = "root ...
- Codeforces Round #196 (Div. 2) A. Puzzles 水题
A. Puzzles Time Limit: 2 Sec Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...