【题目描述】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.
【解题思路】一维动态规划,从左到右扫描,遍历记录下当前能到达的最远的位置,如果能到达n,则一定能达到<n的任何位置
【考查内容】动态规划,数组
class Solution {
public:
bool canJump(int A[], int n) {
int maxCover = 0;
for(int start = 0; start<=maxCover && start<n;start++){
if(A[start]+start > maxCover)
maxCover = A[start]+start;
if(maxCover >=n-1)
return true;
}
return false;
}
};