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 ...
随机推荐
- Java实现图片的裁剪(包括透明背景)
需求: 有一张位置大小的图片,现在需要根据这张原图得到指定尺寸的图片,且得到的图片要符合原先图片的比例,就是在原图的基础上等比例缩放得到图片后,在进行剪裁,这样保证得到的图片是原图的一部分,而不是将原 ...
- Fiddler抓包工具安装与使用
1.Fiddler简介2.Fiddler安装步骤3.Fiddler目录结构4.Fiddler证书配置5.Fiddler录制配置6.Fiddler工作原理7.Fiddler界面详解 1.Fiddler简 ...
- Codeforces Round #545 (Div. 2) D 贪心 + kmp
https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...
- s6-6 TCP 连接释放
TCP 连接释放 任何一方在没有数据要传送的时候,都可以发送一个FIN置位了的 TCP 数据段 当FIN被确认的时候,该方向的连接被关闭 当双向连接都关闭了的时候,连接释放 两军队问题 两军队问题 ( ...
- 关于select的使用感受~大坑~select不能添加点击事件触发~
这是一个坑,把我摔惨了! select+option是浏览器自带的下拉选项框,样式及其丑,还好现在有很多框架都相应做了些美化,select 元素是一种表单控件,可用于在表单中接受用户输入. 还有一个重 ...
- Eclipse项目里面看源码和文档
Eclipse项目里面看源码 1.新建项目列表 2.进入struts2-core-2.3.20.jar,双击之后,看不到源码 3.右键struts2-core-2.3.20.jar,选择propert ...
- LeetCode刷题:第七题 整数翻转 第九题 回文数
第七题题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入 ...
- UDF函数 解码url
背景 URL 的编码 是ASCII十六进制格式.数仓接受到前端上报的URL,要对URL字段解码. 如要将 https"Fmybook.do%3Frequest_type%3D%26type% ...
- MFC中的一些视图
本章主要介绍MFC中主要的视图类,这些继承自Cview类. 继承关系如上图所示. 滚动视图 CscrollView给Cview添加了基本的滚动功能,它包含WM_VSCROLL和WM_HSCROLL消息 ...
- WPF 开发备忘录
运营日: select t.* from (select ab.*, bs.station_cn_name, bd.device_name from audit_tvm_cas ...