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.记录到如今为止能跳到最远的距离,记为max

2.每个点能走的步数,记为step,且每往前面走一步。step--

3.推断到这个点后往后跳的步数是不是大于max,假设是就更新max,不是就继续往前走

这种话我们能够看到:假设前面这个点是零,且这个时候step步数不够你迈过去,那么就会自己主动跳出返回false。

可是假设能够一直这么走走到终点,那么返回true

package testAndfun;

public class jumpGame {
public static void main(String args[]){
int[] A = {3,0,0,0};
jumpGame jp = new jumpGame();
if(jp.canJump(A)) System.out.println("We win");
else System.out.println("we lose");
} public boolean canJump(int[] A) {
int max = 0;
int step = 1;
if(A.length<=1) return true;
if(A[0]==0&&A.length>1) return false;
for(int i=0;i<A.length;i++){ step--;
if(i+A[i]>max){
max = i+A[i];
step = A[i];
}
if(step==0 && i<A.length-1) return false; }
return true;
}
}

最新文章

  1. 第0/24周 SQL Server 性能调优培训引言
  2. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增系统参数管理
  3. web项目总结——通过jsp+servlet实现对oracle的增删改查功能
  4. HowTo系列之virtualenv
  5. [转]MyBatis传入多个参数的问题 - mingyue1818
  6. HDU 2819 隐式二分图匹配
  7. Scala 深入浅出实战经典 第66讲:Scala并发编程实战初体验
  8. Flash Player 19.0.0.124 Beta + IHTMLDocument3 IHTMLDocument2 -&gt;get_innerHTML
  9. Piggy-Bank
  10. fixSidebar简介与修正log
  11. IdHttpServer实现webservice(130篇DataSnap文章)
  12. 在CentOS下安装配置MySQL(转)
  13. (转)Python 遍历List三种方式
  14. iOS 环信透传cmd消息多次重复接收,解决办法
  15. 51 nod 1227 平均最小公倍数
  16. mysql 开发进阶篇系列 49 表的数据导出(into outfile,mysqldump)
  17. linux 配置本地光盘YUM源
  18. UpdatePanel1里面使用FileUpload控件
  19. redis:set集合类型的操作(无序集合)
  20. linux配置防火墙和重启防火墙

热门文章

  1. .NET中常见的加解密方式
  2. WordPress 编辑器没有可视化
  3. 【java 基础 9】原来我从没有了解过String类
  4. nginx报错504
  5. 【bzoj3123】[Sdoi2013]森林 倍增LCA+主席树+启发式合并
  6. BZOJ 4719 [Noip2016]天天爱跑步 ——树链剖分
  7. BZOJ 3990 [SDOI2015]排序 ——搜索
  8. [luoguP2051] [AHOI2009]中国象棋(DP)
  9. 【loj6191】「美团 CodeM 复赛」配对游戏
  10. 什么是JNI?