Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

贪心:

 1 public boolean canJump(int[] nums) {
 2         if (nums == null || nums.length == 0) {
 3             return false;
 4         }
 5         int finalIndex = 0;
 6         for (int i = 0; i < nums.length; i++) {
 7             if (finalIndex < i) {
 8                 return false;
 9             }
10             finalIndex = Math.max(finalIndex, i + nums[i]);
11         }
12         return true;
13     }
View Code

相关文章: