首页 > 编程语言 > 详细

Jump Game leetcode java

时间:2014-07-28 11:33:50      阅读:293      评论:0      收藏:0      [点我收藏+]

题目

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             }  
10             return false;  
11        } 

Jump Game leetcode java,布布扣,bubuko.com

Jump Game leetcode java

原文:http://www.cnblogs.com/springfor/p/3872320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!