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;
    }

 

posted @ 2015-05-15 11:05  TonyLuis  阅读(117)  评论(0编辑  收藏  举报