55. Jump Game

来源:互联网 发布:淘宝企业店铺贷款额度 编辑:程序博客网 时间:2024/06/07 09:20

题目

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.

思路

本题是典型的dp问题,找到状态子方程即可:即当前位置的能跳跃的最大距离global = max(global,nums[i]+i)

代码

class Solution {public:    bool canJump(vector<int>& nums) {        int global=0,i;        for(i=0;i<=global&&global<nums.size()-1;i++)            global = max(global,nums[i]+i);        if(i>global)            return false;        return true;    }};
原创粉丝点击