Leetcode#55 Jump Game

原题地址

 

跟Jump Game II(见这篇文章)不同的地方在于,只需要判断能否到达终点即可

遍历每个位置,更新能够到达的范围,最后看看范围是否覆盖了终点

代码:

 1 bool canJump(int A[], int n) {
 2         int range = 0;
 3         int pos = 0;
 4         
 5         while (pos < n && pos <= range) {
 6             range = max(range, pos + A[pos]);
 7             pos++;
 8         }
 9         
10         return range + 1 >= n;
11 }

 

posted @ 2015-01-27 14:54  李舜阳  阅读(152)  评论(0编辑  收藏  举报