JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用maxstep记录最大步程,然后遍历(greedy)

 1 public class Solution {
 2     public boolean canJump(int[] A) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         int maxstep = 0;
 6         for(int i = 0; i < A.length; i++)
 7         {
 8             if(i > maxstep)
 9                 return false;
10             maxstep = Math.max(maxstep, (i + A[i]));
11             if(maxstep >= A.length - 1)
12                 return true;
13         }
14         return true;
15     }
16     
17 }

 

posted on 2013-11-07 07:18  JasonChang  阅读(185)  评论(0编辑  收藏  举报