leetcode 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.

题解:就是一道非常简单的跳格子的题目 每格有可以往下跳的步数 这道题难就难在它的判定上 并且需要遍历每一个元素

代码如下:

bool canJump(vector& nums) {
        int moves=-1;
        if(nums.size()==1)
        return true;
        for(int i=0;inums[i])
            {
                moves-=1;
            }
            if(moves==0&&i!=nums.size()-1)
            return false;
        }
        return false;
    }


你可能感兴趣的:(leetcode 55. Jump Game)