jump game

来源:互联网 发布:网络渗透攻击 编辑:程序博客网 时间:2024/06/05 06:41

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.

思路:

     刚开始动态规划,超时

     参考他人的代码,其思路主要为用一个变量保存能到达的最远的距离。

bool canJump(int A[], int n) {
        if(A==NULL || n<=0)
{
return false;
}


int max_dis=A[0];
for(int i=0; i<=min(max_dis,n-1); ++i)
{
max_dis = max(max_dis,i+A[i]);
}


return max_dis >= (n-1);
    }

0 0
原创粉丝点击