Jump Game

来源:互联网 发布:淘宝店尾页区域怎么改 编辑:程序博客网 时间:2024/06/05 06:43

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.

//http://tech.ddvip.com/2014-08/1407731415212434.html//http://www.cnblogs.com/ganganloveu/p/3761715.htmlclass Solution {public:    int jump(vector<int>& nums) {        int n = nums.size();        int ret = 0;        int curRch = 0;        int curMax = 0;        for(int i = 0;i<n;i++)        {            if(curRch < i)            {                ret++;                curRch = curMax;            }            curMax = max(curMax,nums[i] + i);        }        return ret;            }};


0 0
原创粉丝点击