1. /**
  2. * // Source : https://oj.leetcode.com/problems/jump-game-ii/
  3. *
  4. * Created by lverpeng on 2017/7/17.
  5. *
  6. * Given an array of non-negative integers, you are initially positioned at the first index of the array.
  7. *
  8. * Each element in the array represents your maximum jump length at that position.
  9. *
  10. * Your goal is to reach the last index in the minimum number of jumps.
  11. *
  12. * For example:
  13. * Given array A = [2,3,1,1,4]
  14. *
  15. * The minimum number of jumps to reach the last index is 2.
  16. * (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
  17. *
  18. */
  19. public class JumpGame2 {
  20. /**
  21. * 找到需要跳的次数最少的方法
  22. *
  23. * 贪心算法:保证当前是是最优的,以期结果是最优的
  24. *
  25. * 先保证能调到最后的最好的解,然后再计算能跳到当前最好解位置的最好解
  26. *
  27. * @param arr
  28. * @return
  29. */
  30. public int jump (int[] arr) {
  31. int end = arr.length - 1;
  32. int count = 0;
  33. while (end > 0) {
  34. for (int i = 0; i < end; i++) {
  35. if (i + arr[i] >= end) {
  36. count ++;
  37. end = i;
  38. break;
  39. }
  40. }
  41. }
  42. return count;
  43. }
  44. public static void main(String[] args) {
  45. JumpGame2 jumpGame2 = new JumpGame2();
  46. int[] arr = new int[]{2,3,1,1,4};
  47. System.out.println("2----->" + jumpGame2.jump(arr));
  48. }
  49. }

leetcode — jump-game-ii的更多相关文章

  1. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. LeetCode——Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [LeetCode] Jump Game II 贪心

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] Jump Game II(贪婪算法)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. leetcode–jump game II

    1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...

  8. leetcode Jump Game II python

    @link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...

  9. [Leetcode] jump game ii 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  10. 【To Read】LeetCode | Jump Game II(转载)

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

随机推荐

  1. pandas库的学习笔记

    Environment pandas 0.21.0 python 3.6 jupyter notebook 开始 习惯上,我们导入如下: import pandas as pd import nump ...

  2. 在datasnap 中使用unidac 访问数据(客户端)

    前面我们讲了如何使用unidac 在datasnap 的服务端访问数据库,今天大概讲一下客户端如何访问 前面做的服务器?其实这个客户端适合任何datasnap 服务端. 首先我们建一个应用,并加入一个 ...

  3. 操作系统组成和工作原理以及cpu的工作原理

  4. servlet从mysql中取数据时出现的汉字编码问题

    取出的汉字都是问号 之后根据网友提示在输出之前对response进行编码设置 正常显示

  5. kubernetes1.7.6 ha高可用部署

    写在前面:  1. 该文章部署方式为二进制部署. 2. 版本信息 k8s 1.7.6,etcd 3.2.9 3. 高可用部分 etcd做高可用集群.kube-apiserver 为无状态服务使用hap ...

  6. 点击a标签的文字后页面的跳转

    1.方法一 (1)js var html=""; html+="<a href=\"#\" onclick=\check('"+id+ ...

  7. javaWeb中MVC的编程思想示例

    没有学习MVC之前我只写了一个Servlet类(Note_List.java),分层之后,我将这个类分成了5个类(NoteDao.java,,NoteDaoImpl.java,,NoteService ...

  8. Pyhon学习笔记-基础3

    文件操作 1.基本操作 f = open("filename","r",encoding="utf-8") #打开文件,以r模式,字符编码模 ...

  9. html5 css选择器。 井号,句号的区别

    .理解CSS的样式组成CSS里的样式表是有规则组成的,每条规则有三个部分组成:1.选择器(如下面例子中的:“body”),告诉浏览器文档的哪个部分受规则影响:2.属性(如实例中的font-family ...

  10. DICOM医学图像处理:WEB PACS初谈四,PHP DICOM Class

    背景: 预告了好久的几篇专栏博文一直没有整理好,主要原因是早前希望搭建的WML服务器计划遇到了问题.起初以为参照DCMTK的官方文档wwwapp.txt结合前两天搭建的WAMP服务器可以顺利的实现WM ...