# LeetCode题目：Jump Game，一维动态规划

`vector`

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.

```class Solution {
public:
bool canJump(int A[], int n) {
//if (n > 100) return false;
if (n <= 1)
return true;
//vector canJump(n, false);
bool *canJump = new bool[n];
canJump[n - 1] = true;
for( int i = n - 2; i >= 0; --i ) {
int maxJump = A[i];
for(int j = i + 1; j <= i + maxJump && j < n ; ++j) {
if (canJump[j]) {
canJump[i] = true;
break;
}
}
}
return canJump[0];
}
};
```

```class Solution {
public:
bool canJump(int A[], int n) {
if (n <= 1)
return true;
vector canJump(n, false);
canJump[n - 1] = true;
for( int i = n - 2; i >= 0; --i ) {
int maxJump = A[i];
if (i + maxJump >= n)
maxJump = n - i;
for(int j = i + maxJump; j > i; --j) {
if (canJump[j]) {
canJump[i] = true;
break;
}
}
}
return canJump[0];
}
};
```

## “LeetCode题目：Jump Game，一维动态规划”上的11条回复

tenos说：

vector这个用法有问题，参考effective STL

traveller说：

traveller说：
```class Solution {
public:
bool canJump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
for(int i=n-2;i>=0;i--){
if(A[i]==0){
int j=i-1;
for(;j>=0;j--){
if((j+A[j])>i)
break;
}
if(j<0)return false;
}
}
return true;
}
};
```
leo说：

Your code does not work for the input [2, 5, 2, 1, 0, 1].
Your code returns false.

Yes, he needs check if the maxRange < A[i] + i, before change the value of it.

Yes, you are right! This problem can be solved by greedy strategy.