jump-game
/**
*
* @author gentleKay
* 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], returntrue.
* A =[3,2,1,0,4], returnfalse.
*
* 给定一个非负整数数组,您最初定位在数组的第一个索引处。
* 数组中的每个元素表示该位置的最大跳跃长度。
* 确定是否能够达到最后一个索引。
* 例如:
* A=[2,3,1,1,4],返回真。
* A=[3,2,1,0,4],返回假。
*/
/** * * @author gentleKay * 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], returntrue. * A =[3,2,1,0,4], returnfalse. * * 给定一个非负整数数组,您最初定位在数组的第一个索引处。 * 数组中的每个元素表示该位置的最大跳跃长度。 * 确定是否能够达到最后一个索引。 * 例如: * A=[2,3,1,1,4],返回真。 * A=[3,2,1,0,4],返回假。 */ public class Main34 { public static void main(String[] args) { int[] A = {2,0,0}; System.out.println(Main34.canJump(A)); } public static boolean canJump(int[] A) { if (A.length <= 0) { return false; } if (A.length == 1) { return true; } int index = 0; for (int i=0;i<A.length; ) { index = A[i]; i = i + index; if (i >= A.length-1) { return true; } if (A[i] == 0) { return false; } } return true; } }