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
.
Translate:
Code:
/**
*
*/
package From41;
/**
* @author MohnSnow
* @time 2015年6月26日 下午1:48:28
*
*/
public class LeetCode55 {
/**
* @param argsmengdx
* -fnst
*/
//380msAC---参考45题,每一部记录最远距离
public static boolean canJump(int[] nums) {
if (nums.length <= 1) {
return true;
}
int current_jump_max = 0;//记录的是到i为止统计过的每一个元素的最远距离
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == 0) {
if (current_jump_max <= i) {
return false;
}
} else {
current_jump_max = Math.max(current_jump_max, i + nums[i]);
}
System.out.println("current_jump_max: " + current_jump_max);
}
return true;
}
public static void main(String[] args) {
int[] nums = { 2, 0, 0 };
System.out.println("spiralOrder: " + canJump(nums));
}
}