您的位置:首页 > 其它

Jump Game

2015-08-28 06:17 253 查看
原网址:http://blog.csdn.net/linhuanmars/article/details/21354751

发现递归跟动规的知识都还给老师了。。最近要大补一下。

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
.

代码:

package codes;

public class CanJump {

	public static void main(String[] args) {
		// A = [2,3,1,1,4], return true.
       int [] nums = {2,3,1,1,4};
       System.out.println(new CanJump().canJump(nums));
       
	}
	public boolean canJump(int[] nums) {
        if (nums.length == 0 || nums == null)
        	return false;
        
        int reach = 0;
        //还必须要保证 是能够取到的
        for(int i=0;i< nums.length && i<=reach;i++){
        	reach=Math.max(reach, i+nums[i]);
        }
        if(reach < nums.length-1)
        	return false;
        return true;
        
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: