55.Jump Game

Source: Internet
Author: User

Given an array of non-negative integers, you is initially positioned at the first index of the array.

Each element of the array represents your maximum jump length is at that position.

Determine if you is able to reach the last index.

for example:
[2,3,1,1,4] , Return true .

a = [3,2,1,0,4] , Return false .

Idea: Using greedy strategy, initialize set Step=nums[0], to the next step--, and take Step=max (Step,nums[i]), so that step has remained the maximum number of moving steps, if the step<=0, indicating can not reach I position, Then return FALSE. The entire loop comes down without returning a description to be able to reach the end of the position smoothly, returning true.

classSolution { Public:    BOOLCanjump (vector<int>&nums) {        if(Nums.size () <=1)            return true; intStep =nums[0];  for(intI=1; I<nums.size (); i++){            if(step>0) {Step--; Step=Max (step,nums[i]); }            Else                return false; }        return true; }};




55.Jump Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.