关闭

leetcode : jumpgame

标签: c++leetcode
84人阅读 评论(0) 收藏 举报
分类:
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], returntrue.
A =[3,2,1,0,4], returnfalse.






class Solution {
public:
    bool canJump(int A[], int n) {
        int reach = 1;
        for(int i = 0; i< reach && reach<n;i++)
        {
          reach = max(reach, i+1+A[i]);   
        }
        return reach>=n;
    }
};
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30498次
    • 积分:537
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:24篇
    • 译文:0篇
    • 评论:0条
    文章分类