# Java Solution easy to understand

• ``````public boolean canJump(int[] A) {
int max = 0;
for(int i=0;i<A.length;i++){
if(i>max) {return false;}
max = Math.max(A[i]+i,max);
}
return true;
}``````

• Python version, can stop iteration early

``````class Solution:
# @param A, a list of integers
# @return a boolean
def canJump(self, A):
maxs=0
for idx,item in enumerate(A):
if maxs<idx:
return False
maxs=max(maxs,idx+item)
if maxs>=len(A)-1:
return True``````

• The loop will stop when max cannot reach cur or max can reach the last index.

``````public class Solution {
public boolean canJump(int[] nums) {
int max = 0, length = nums.length;
for(int cur = 0; cur <= max && max < length - 1; max = Math.max(cur + nums[cur], max), cur++);
return max >= length - 1;
}
}``````

• This post is deleted!

• public boolean canJump(int[] A) {
int max = 0;
for(int i=0;i<A.length;i++){
if(i>max) {return false;}
max = Math.max(A[i]+i,max);
}
return true;
}

Get it,Thanks. the "max" mean the farthest distance that can be reached currently,right?

• No, your answer is TLE. However, I think it is LeetCode's error

• I don't think that solution works. I'm not sure if the problem definition changed, but I made a modification and it works fine.

``````public static bool CanJump(int[] nums)
{
int max = 0;

for (int i = 0; i < nums.Length;i++){
if (i > max) return false;
max = Math.Max(nums[i]+i, max);
}
return true;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.