码迷,mamicode.com
首页 > 其他好文 > 详细

Jump Game

时间:2016-09-19 06:37:40      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

Analyse: suppose reachMe[i] represents whether you could reach i, then reachMe[i] should be calculated as:

for j from [0, i - 1], if could reach j, and nums[j] >= i - j, then reachMe[i] is true. 

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

 

Jump Game

标签:

原文地址:http://www.cnblogs.com/amazingzoe/p/5883684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!