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.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)


题解:跟Jump Game十分相像,把原来的boolean数组改成int型数组,用来记录到达i时候最小的步骤数目,遍历j(j<i),考察从j跳到i需要的步骤数目是否小于当前的reach[i],如果小于,就更新reach[i]。最终返回reach[A.length-1]即可。

代码如下:

 public class Solution {
public int jump(int[] A) {
if(A == null || A.length == 0)
return 0; int[] reach = new int[A.length];
Arrays.fill(reach, Integer.MAX_VALUE);
reach[0] = 0; for(int i = 1;i < A.length;i++){
for(int j = 0;j < i;j++){
if(reach[j] != Integer.MAX_VALUE && j+A[j]>=i){
reach[i] = Math.min(reach[i], reach[j]+1);
break;
}
}
} return reach[A.length-1];
}
}

最新文章

  1. [恶趣味]搞了下局域网内的arp网络欺骗
  2. 为 Github 创造 Integration
  3. apache vhost
  4. UE4在C++中使用OnComponentBeginOverlap之类的时间
  5. web服务器内置对象,或者说是ServletAPI的实例
  6. SpringMVC 避免IE执行AJAX时,返回JSON出现下载文件
  7. Web模板引擎本质前奏
  8. Android Java混淆(ProGuard)
  9. php curl_exec optimize
  10. 线性整流函数(ReLU)
  11. P3167 [CQOI2014]通配符匹配
  12. mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072)
  13. js判断是否为undefined
  14. Linux常见目录使用区别
  15. WebSocket原理分析
  16. flutter 保存图片到本地
  17. Android开发-API指南-&lt;activity&gt;
  18. iOS 快速遍历 效率分析 for loop for in enumerateBlock 适用条件
  19. webpack4.x ,1基本项目构建 详解
  20. .Net反射机制

热门文章

  1. GridControl 二次封装,自定义颜色样式风格
  2. php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法
  3. 二分Kmeans的java实现
  4. tomcat安装配置规范
  5. iOS scrollView中嵌套多个tableView处理方案
  6. Modern.IE,创建现代网站的给力开发工具!
  7. simple_pool对象池——优化&amp;lt;二&amp;gt;
  8. 关于使用eclipse开发最小运行组件包
  9. js浅度克隆/深度克隆
  10. shader一些语义或术语的解释