LeetCode: 55. Jump Game(Medium)
1. 原题链接
https://leetcode.com/problems/jump-game/description/
2. 题目要求
给定一个整型数组,数组中没有负数。从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数。问能否跳到该数组的最后一个元素位置
注意:可以跳的步数超出数组长度依旧视为可以达到最后位置
3. 解题思路
从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是“i+nums[i]”=2,也就是nums[2]。
然后让 “i<2;i++”继续遍历,nums[1]+1=3,可以到达nums[3] =1,nums[3] +1>nums.length-1,返回true
4. 代码实现
public class JumpGame55 {
public static void main(String[] args) {
int[]nums ={2,2,0,2,4};
System.out.println(canJump(nums));
}
public static boolean canJump(int[] nums) {
int dis = 0;
for (int i = 0; i <= dis; i++) {
dis = Math.max(dis, i + nums[i]);
System.out.println(dis);
if (dis >= nums.length-1) {
return true;
}
}
return false;
}
}
LeetCode: 55. Jump Game(Medium)的更多相关文章
- [leetcode] 55. Jump Game (Medium)
原题 题目意思即 每一格代表你当前最多能再往后跳几次,从第一格开始,如果能跳到最后一格返回true,反之为false. 思路:用一个下标记录当前最多能跳到哪一格,遍历一遍 --> 如果当前格子不 ...
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
随机推荐
- 使用Hibernate注解Annotations进行对象映射的异常处理
通过Hibernate注解Annotations进行对象映射,想在Oracle数据库中自动创建表,代码如下: 实体类: import javax.persistence.Basic;import ja ...
- Android(java)学习笔记1:多线程的引入
1. 多线程的引入:
- 备份&添加无线网络配置
netsh wlan export profile key=clear folder=c:\ #备份 (ls c:\*.xml).FullName|%{netsh wlan add profile f ...
- Tree - Rooted Trees
Rooted Trees A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a b ...
- Pyplot教程(深度学习入门3)
源地址:http://matplotlib.org/users/pyplot_tutorial.html .caret, .dropup > .btn > .caret { border- ...
- get-pip.py 安装
http://www.pip-installer.org/en/latest/installing.html$ curl http://pypi.python.org/packages/source/ ...
- STM32之系统时钟
转载:http://www.openedv.com/posts/list/302.htm 时钟系统是处理器的核心,所以在学习STM32所有外设之前,认真学习时钟系统是必要的,有助于深入理解STM32. ...
- Extjs header column 自定义排序规则
Extjs 的表格自带排序功能,这个功能在大部分情况下能够满足我们的需求,但是在某种情况下,例如IP排序,默认情况下,按照字符串进行排序, 此时我们需要自定义排序规则,这个时候就需要我们重写方法了, ...
- Window系统Oracle 安装
一:安装Oracle 数据库软件 1.先去官网下载所需文件:http://www.oracle.com/technetwork/database/enterprise-edition/download ...
- Rem实现自适应布局
rem布局的目的是为了让我们可以用同一份代码,适应不同的移动终端(rem:就是css单位) 1.项目入口html文件<meta name="viewport" content ...