/**
* // Source : https://oj.leetcode.com/problems/jump-game-ii/
*
* Created by lverpeng on 2017/7/17.
*
* 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.)
*
*/
public class JumpGame2 { /**
* 找到需要跳的次数最少的方法
*
* 贪心算法:保证当前是是最优的,以期结果是最优的
*
* 先保证能调到最后的最好的解,然后再计算能跳到当前最好解位置的最好解
*
* @param arr
* @return
*/
public int jump (int[] arr) {
int end = arr.length - 1;
int count = 0;
while (end > 0) {
for (int i = 0; i < end; i++) {
if (i + arr[i] >= end) {
count ++;
end = i;
break;
}
}
}
return count;
} public static void main(String[] args) {
JumpGame2 jumpGame2 = new JumpGame2();
int[] arr = new int[]{2,3,1,1,4};
System.out.println("2----->" + jumpGame2.jump(arr));
}
}

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. MyEclipse has detected that less than 5% of

      选择Windows->Preferences; 2 选择MyEclipse->Servers->Integrated Sandbox->MyEclispe Tomcat 6 ...

  2. spring实现一个简单的事务管理

    前两天给公司的数据库操作加了事务管理,今天博客就更一下这个吧. 先说明:本文只是简单得实现一下事务,事务的具体内容,比如事务的等级,事务的具体实现原理等等... 菜鸟水平有限,暂时还更不了这个,以后的 ...

  3. sublime构建各个编译环境

    一 java运行环境配置: 打开sublime选择Tool 到 Building System 选择new building System 输入 {"shell_cmd": &qu ...

  4. linux从0开始----01

    1.VMware 虚拟机安装与卸载 推荐安装较高版本,11.x以后的.本课程安装12.x版本,需要序列号. 2.在vmware中安装centos客户机.初学者选择典型安装也可以. 1.vware文件菜 ...

  5. mysql 模糊查询条件带‘%’问题

  6. Notes : <Hands-on ML with Sklearn & TF> Chapter 6

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  7. spring扩展点总结

    NamespaceHandler 通过自定义的NamespaceHandler,配合BeanDefinitionParser,可以完成自定义Bean的组装操作,对于BeanDefinition的数据结 ...

  8. prim算法和克鲁斯卡尔算法

    Prim 设图G=(V,E)是一个具有n个顶点的连通网,其生成树的顶点集合为U.首先把v0放入U,再在所有的u∈U,v∈V-U的边(u,v)∈E中找一条最小权值的边,加入生成树,并把该边的v加入U集合 ...

  9. ie9上传文件

    兼容ie9文件上传,解决ie9下提示下载或保存 如果不考虑ie9兼容性,实现[上传图片]大致的思路如下: 由于公司是将所有上传的图片都放到[代理服务器]里.所以[上传图片]其实是上传到[代理服务器]. ...

  10. 【洛谷P3960】列队题解

    [洛谷P3960]列队题解 题目链接 题意: Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n×m ...