贪婪,贪婪的比较巧 算是,你在或不不在,他都在这里。。。。
遍历所有的index,维护一个当前可以走的最远的位子,如果最远符合条件,那么就是了,否则就算你遍历完,你也走不到那里。。。中间判断是否能走到的时候哟啊看当前i是不是比m小,否则,就算到了也不算,因为suppose就不应该到那里。。。
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
.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(int A[], int n) {
if (n==0)
return false;
if (n==1)
return true;
int i=0,m=0;
for (;i<n;i++){
if(i<=m){
m=max(m,A[i]+i);
if(m>=n-1)
return true;
}
}
return false;
}
};