leetcode — jump-game-ii
/**
* // 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的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode——Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode–jump game II
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- pandas库的学习笔记
Environment pandas 0.21.0 python 3.6 jupyter notebook 开始 习惯上,我们导入如下: import pandas as pd import nump ...
- 在datasnap 中使用unidac 访问数据(客户端)
前面我们讲了如何使用unidac 在datasnap 的服务端访问数据库,今天大概讲一下客户端如何访问 前面做的服务器?其实这个客户端适合任何datasnap 服务端. 首先我们建一个应用,并加入一个 ...
- 操作系统组成和工作原理以及cpu的工作原理
- servlet从mysql中取数据时出现的汉字编码问题
取出的汉字都是问号 之后根据网友提示在输出之前对response进行编码设置 正常显示
- kubernetes1.7.6 ha高可用部署
写在前面: 1. 该文章部署方式为二进制部署. 2. 版本信息 k8s 1.7.6,etcd 3.2.9 3. 高可用部分 etcd做高可用集群.kube-apiserver 为无状态服务使用hap ...
- 点击a标签的文字后页面的跳转
1.方法一 (1)js var html=""; html+="<a href=\"#\" onclick=\check('"+id+ ...
- javaWeb中MVC的编程思想示例
没有学习MVC之前我只写了一个Servlet类(Note_List.java),分层之后,我将这个类分成了5个类(NoteDao.java,,NoteDaoImpl.java,,NoteService ...
- Pyhon学习笔记-基础3
文件操作 1.基本操作 f = open("filename","r",encoding="utf-8") #打开文件,以r模式,字符编码模 ...
- html5 css选择器。 井号,句号的区别
.理解CSS的样式组成CSS里的样式表是有规则组成的,每条规则有三个部分组成:1.选择器(如下面例子中的:“body”),告诉浏览器文档的哪个部分受规则影响:2.属性(如实例中的font-family ...
- DICOM医学图像处理:WEB PACS初谈四,PHP DICOM Class
背景: 预告了好久的几篇专栏博文一直没有整理好,主要原因是早前希望搭建的WML服务器计划遇到了问题.起初以为参照DCMTK的官方文档wwwapp.txt结合前两天搭建的WAMP服务器可以顺利的实现WM ...