public class Solution { public boolean canJump(int[] nums) { if(nums.length == 0) return false; int i = 0; //判斷有沒有0,沒有0的肯定能達到 while(i < nums.length){ if(nums[i] == 0){ break; } i++; } //沒有0,肯定能達到 if(i == nums.length){ return true; } i = 0; while(i < nums.length){ if(i + nums[i] >= nums.length - 1) return true; if(nums[i] == 0) return false; int max = 0; int index = 0; //下一步能前進最大的步驟 for(int j = i+1; j - i <= nums[i]; j++){ if(max < j - i + nums[j]){ max = j - i + nums[j]; index = j; } }//走到下一步的索引 i = index; } return true; } }