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
.
从头开始遍历数组,并记录当前最远可以到达的位置。遍历时,根据实际情况更新最远距离。public boolean canJump(int[] A) { if (A == null) { return false; } int len = A.length; if (len == 1) { return true; } int max = A[0]; for (int i = 0; i < len && i <= max; i++) { if (A[i] + i > max) { max = A[i] + i; } } if (max >= len - 1) { return true; } else { return false; } }
原文地址:http://blog.csdn.net/u010378705/article/details/30065053