标签:
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.递归法
超时不符合要求
1 class Solution { 2 public: 3 bool jump(vector<int>& a, int cur, int len){
if(cur == len - 1){ 4 return true; 5 } 6 if(cur > len - 1){ 7 return false; 8 } 9 if(a[cur] == 0){ 10 return false; 11 } 12 bool res = false; 13 for(int i = 1; i <= a[cur]; i++){ 14 res = res || jump(a, cur + i, len); 15 } 16 return res; 17 } 18 bool canJump(vector<int>& nums) { 19 return jump(nums, 0, nums.size()); 20 } 21 };
标签:
原文地址:http://www.cnblogs.com/forlihui/p/4921471.html