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.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

Solutions:

1. DP1
每到一个点 i,我们扫描之前所有的点,如果之前某点j本身可达,并且与current 点可达,表示点i是可达的。

返回值:DP数组的最后一个值。

 1 // DP1.
 2     public boolean canJump1(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return false;
 5         }
 6 
 7         int len = A.length;
 8         boolean[] can = new boolean[len];
 9         can[0] = true;
10 
11         for (int i = 1; i < len; i++) {
12             can[i] = false;
13             for (int j = 0; j < i; j++) {
14                 // j can arrive and can jump to i.
15                 if (can[j] && A[j] >= i - j) {
16                     can[i] = true;
17                     break;
18                 }
19             }
20         }
21 
22         return can[len - 1];
23     }
View Code

相关文章: