Jumping game
1, problem-solving ideas: the bottom-up, dynamic programming
class Solution { public boolean canJump(int[] nums) { int len = nums.length; if(len<=1){ return true; } boolean[] temps = new boolean[len]; return canJump2(nums,len,0,temps); } public boolean canJump2(int[] nums,int len,int start,boolean[] temps){ if(temps[start]){ return false; } if(start+nums[start]>=len-1){ return true; } for(int i=nums[start];i>0;i--){ if(canJump2(nums,len,start+i,temps)){ return true; } } temps[start]=true; return false; } }
2, dynamic programming, top-down
3, a very clever way
Problem-solving ideas: to traverse from right to left, always remember the leftmost point can be reached, this way, if the leftmost point is equal to 0, prove to be reached.
class Solution { public boolean canJump(int[] nums) { int len = nums.length; if(len<=1){ return true; } int min = len-1; for(int i=len-2;i>=0;i--){ if(nums[i]+i>=min){ min=i; } } if(min==0){ return true; }else{ return false; } } }