55. Jump Game

55. Jump Game

题目

 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_55 {
public:
    bool canJump(vector<int>& nums) {

        int maxend = 0;
        for (int i = 0; i < nums.size()&&maxend>=i;i++)
        {
            maxend = max(maxend,i+nums[i]);
        }

        if (maxend<nums.size()-1)
        {
            return false;
        }
        return true;
    }

    bool canJump(int A[], int n) {
        vector<int> vec(A, A + n);
        return canJump(vec);
    }
};

题目来源

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/8562729.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞