Leetcode 55 Jump Game

来源:互联网 发布:妮维雅 爽肤水 知乎 编辑:程序博客网 时间:2024/05/20 07:50

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://blog.csdn.net/accepthjp/article/details/52484618

如果能够跳到终点,则用最小步数的试探方法一定能够到达终点!

class Solution {public:    bool canJump(vector<int>& nums) {        int st=0;        while(st<nums.size()-1)        {            int maxx=-1,pos;            for(int i=1;i<=nums[st];i++)            {                if(i+nums[st+i]>=maxx)                {                    maxx=i+nums[st+i];                    pos=i;                }            }            if(maxx==-1) return false;            st+=pos;        }        return true;    }};


1 0
原创粉丝点击