LeetCode——Jump Game
2015-10-14 21:59
471 查看
Description:
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 =
A =
贪心,每次取最大step看是否能够到达最后的索引。
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.
贪心,每次取最大step看是否能够到达最后的索引。
public class Solution { public boolean canJump(int[] nums) { if(nums.length == 0 || nums.length == 1) return true; int maxStep = nums[0]; for(int i=0; i<nums.length; i++) { if(maxStep == 0) { return false; } maxStep = Math.max(maxStep-1, nums[i]); if(maxStep+i >= nums.length-1) { return true; } } return true; } }
相关文章推荐
- 黑马程序员——概述
- I have updated Android SDK to rev. 22 yesterday and there is no apkbuilder in tools
- Eclipse的基本使用
- iOS 调用支付宝进行支付功能
- 【非作业部分】队内暂时安排
- use PostgreSQL trigger manage stock & offer infomation
- Swift-String转Int
- Android成长笔记(1)——Windows+eclipse+Android开发环境搭建
- android文字阴影效果(转)
- linux----命令替换
- 一个 IT 青年编程四年的感悟
- PostgreSQL use UDF combine NoSQL DB's Computing Power
- Java剪切板操作大全
- hdu 1556 Color the ball(成段更新)
- ==和equls
- 结巴分词
- Android数据解析获取JSONObject的key值-Iterator应用
- 在Google使用Borg进行大规模集群的管理 5-6
- action请求参数决定结果
- 找出一个二维数组中的“鞍点”,即该位置上的元素在该行中 最大,在该列中最小(也可能没有“鞍点”),打印有关信息。(提示:注意特 殊情况:没鞍点或多个鞍点)