Last active
March 28, 2016 05:36
-
-
Save cangoal/dbd87f8298982d3e2edf to your computer and use it in GitHub Desktop.
LeetCode - Jump Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
public boolean canJump(int[] nums) { | |
if(nums.length == 0) return false; | |
int i=-1, reach = 0; | |
while(i < reach){ | |
if(reach >= nums.length-1) return true; | |
i++; | |
reach = Math.max(reach, i+nums[i]); | |
} | |
return false; | |
} | |
// | |
public boolean canJump(int[] A) { | |
if(A==null || A.length==0) return false; | |
int reach = 0; | |
for(int i=0; i<=reach&&i<A.length; i++){ | |
int localMax = i+A[i]; | |
reach = Math.max(localMax, reach); | |
} | |
if(reach>=A.length-1) return true; | |
return false; | |
} | |
// | |
public boolean canJump(int[] nums) { | |
if(nums == null || nums.length == 0) return false; | |
int range = nums[0]; | |
int i = 0; | |
while(i <= range){ | |
if(range >= nums.length-1) return true; | |
if(i + nums[i] > range){ | |
range = i+ nums[i]; | |
} | |
i++; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment