【leetcode刷题笔记】Jump Game II
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];
}
}
【leetcode刷题笔记】Jump Game II的更多相关文章
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- Android实用工具
1 json类:hiJson 格式化json字符串 2 sqlite类:sqlitespy,SQLiteExpertSetup 3
- git 4种对象的理解
git中有四种基本对象类型,可以说Git的所有操作都是通过这四种对象完成的.下图是<Git版本控制管理>中文第二版的原话,顺便吐槽一下,这本书真的翻译的一般.. 下面说下我的理解吧,首先b ...
- Snail—UI学习之UITextField
简单看一下UITextField的属性 - (void)createTextField{ UITextField * textField = [[UITextField alloc] initWith ...
- Yandex.Algorithm 2011 A. Double Cola
1.题目描写叙述:点击打开链接 2.解题思路:本题是一道找规律的数学题,通过题意描写叙述不难知道,相当于有5棵二叉树构成了一个森林,须要你按层次遍历找到第n个人是谁. 观察后不难发现,如果最開始的一层 ...
- Apollo配置中心解惑(一):关于一个portal管理多个环境,要求环境相互之间不影响,独立
关于作者的回答很官方,不太懂: https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD% ...
- Trie|如何用字典树实现搜索引擎的关键词提示功能
Trie字典树 Trie字典树又称前缀树,顾名思义,是查询前缀匹配的一种树形数据结构 可以分为插入(创建) 和 查询两部分.参考地址极客时间 下图为插入字符串的过程: 创建完成后,每个字符串最后一个字 ...
- 关于.cap文件分析
CAP文件是比较通用的一种文件格式,基本上大多数抓包软件都支持以此格式将捕获的网络数据包存储下来. 需要说明的是,CAP文件存储下来的,一般都是网络数据帧.不同网络传输协议下的帧格式是有差异的, ...
- ”非常危险“的Linux命令
Linux命令是一种很有趣且有用的东西,但在你不知道会带来什么后果的时候,它又会显得非常危险.所以,在输入某些命令前,请多多检查再敲回车. rm –rf rm –rf是删除文件夹和里面附带内容的一种最 ...
- Android学生管理系统
现在要做这么一个小的demo,可以添加.展示,并且在添加完了之后刷新列表内容. 要点: 在代码中给线性布局添加View 让控件滚动,放到ScrollView中 保存数据就是把数据保存到本地,然后恢复的 ...
- Maven学习----dependencies与dependencyManagement的区别(转)
转自:http://blog.csdn.net/liutengteng130/article/details/46991829 1.DepencyManagement应用场景 当我们的项目模块很多的时 ...