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

 1 class Solution {
 2 public:
 3     bool canJump(vector<int>& nums) {
 4         bool ans=false;
 5         int size=nums.size();
 6         int right=0;
 7         for(int i=0;i<size&&i<=right;i++)
 8         {
 9             int temp=i+nums[i];
10             if(temp>right)
11                 right=temp;
12             if(right>=size-1)
13             {
14                 ans=true;
15                 break;
16             }
17         }
18         return ans;
19     }
20 };
View Code

 

posted @ 2016-01-06 15:41  西小贝  阅读(111)  评论(0编辑  收藏  举报