116 · 跳跃游戏 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.
样例
Example 1:
Input:
A = [2,3,1,1,4]
Output:
true
Explanation:
0 -> 1 -> 4 (the number here is subscript) is a reasonable scheme.
public class Solution {
/**
* @param A: A list of integers
* @return: A boolean
*/
public boolean canJump(int[] A) {
// write your code here
int maxlength = 0 ;
for(int i = 0 ; i < A.length ; i++){
if(i > maxlength){
return false ;
}
maxlength = Math.max(maxlength , A[i] + i);
if(maxlength >= A.length-1){
return true ;
}
}
return false ;
}
}
这是一个关于算法的问题,名为跳跃游戏。给定一个非负整数数组,初始位置为数组的第一个元素。每个元素表示当前位置的最大跳跃长度。任务是判断是否能到达数组的最后一个元素。提供的Java代码实现中,通过维护当前最大可达位置`maxlength`来判断是否能成功跳跃到末尾。
6674

被折叠的 条评论
为什么被折叠?



