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

jump game

时间:2014-07-15 08:42:00      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   art   

1。第一次觉得很简单,但是超时了

bubuko.com,布布扣
 1 public class Solution {
 2     public boolean canJump(int[] A) {
 3         
 4         int len=A.length;
 5         boolean b[]=new boolean[len];
 6         b[0]=true;
 7         for(int i=1;i<len&&i<=A[0];i++) b[i]=true;
 8         
 9         for(int j=1;j<len;j++)
10         {
11             if(b[j])
12             {
13                 int i;
14                 for( i=1;j+i<len&&i<=A[j];i++)
15                 {
16                     b[j+i]=true;
17                 }
18                 if(j+i==len) return b[len-1];
19                 
20                 
21             }
22             
23             
24         }
25         return b[len-1];
26             
27         }
28         
29    
30 }
View Code

2.看了别人的思路,很牛,改进上述思想就是其实没必要a[i]后a[i]个数设为1,记住后面最大的范围就行。,

 

bubuko.com,布布扣
 1 public class Solution {
 2     public boolean canJump(int[] A) {
 3         
 4         
 5         int len=A.length;
 6         if(len==1&&A[0]==0) return true;
 7       int max=A[0];
 8       int i=1;
 9         while(i<len-1)
10         {
11             if(max<=0) return false;
12             
13             max=Math.max(A[i],max-1);
14             i++;
15             if(max<=0) return false;
16             
17             
18         }
19        
20        if(max>=1) return true;
21         return false;
22         
23             
24         }
25         
26    
27 }
View Code

3.我后来想还是0的原因导致最后到不了尾巴,0的宽度就是一条沟,前面一直在积蓄力量。就是最大能跳的长度。跳过了积蓄,跳不过失败。

4.参考最简洁的代码http://www.cnblogs.com/remlostime/archive/2012/11/12/2765894.html(没仔细琢磨,把几个情况统一了)

 1 public class Solution {
 2     public boolean canJump(int[] A) {
 3         
 4         
 5         int len=A.length;
 6         int max=A[0];
 7         for(int i=1;i<len;i++)
 8         {
 9             if(max>0)
10             {
11                 
12             max=Math.max(A[i],max-1);
13             }
14             else return false;
15         }
16             
17             return true;
18       
19             
20         
21  
22        
23         }
24         
25    
26 }

 

 

jump game,布布扣,bubuko.com

jump game

标签:style   blog   http   color   os   art   

原文地址:http://www.cnblogs.com/hansongjiang/p/3841648.html

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