标签:style blog color os io for ar div log
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.
思路:每个元素的值表示了从该位置向后扩展的最远距离。从A[0]出发,不断更新能达到的范围,直至A[end]或者无法再向后进行扩展。
1 class Solution { 2 public: 3 bool canJump( int A[], int n ) { 4 if( n <= 1 ) { return true; } 5 int reach = A[0]; 6 for( int i = 1; i < n; ++i ) { 7 if( reach < i ) { return false; } 8 if( reach < A[i] + i ) { 9 reach = A[i] + i; 10 if( reach >= n-1 ) { return true; } 11 } 12 } 13 return true; 14 } 15 };
标签:style blog color os io for ar div log
原文地址:http://www.cnblogs.com/moderate-fish/p/3938202.html