45. Jump Game II

 

 

 1     public int jump(int[] nums) {
 2         if(nums == null || nums.length == 0) {
 3             return 0;
 4         }
 5         int reach = 0;
 6         int lastReach = 0;
 7         int step = 0;
 8         for(int i = 0; i < nums.length; i++) {
 9             if(i > lastReach) {
10                 lastReach = reach;
11                 step++;
12             }
13             reach = Math.max(reach, nums[i] + i);
14         }
15         return (reach >= nums.length - 1)? step: 0;
16     }

 

你可能感兴趣的:(45. Jump Game II)