LeetCode 55. Jump Game (跳跃游戏)
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.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
题目标签:Array
这道题目给了我们一个array,其中每一个数字代表着可以跳跃的最大步数,让我们判断,能不能跳跃到最后一个数字,这里没有要求要刚刚好跳到最后,超过也可以。一开始自己写了一个递归方法,但是因为速度慢,无法通过全是1的array。所以这道题目用Dynamic Programming方法,遍历array,过程中,我们只需要维护一个值 - 最远能跳跃到的地方 farthestNumIndex。对于每一个数字,我们都算一下它能最远跳跃到哪里,然后和 我们维护的 farthestNumIndex 比较,取大的。所以当我们知道了我们最远能够跳跃到哪里的话,如果遇到了原题中的第二种可能,怎么也跳跃不过0的这种情况,我们只需要判断目前的数字的index i 是不是超过了 farthestNumIndex,超过了,就直接判断为false了。 当farthestNumIndex 达到了,或者超越了最后一位数字的 index,就判断true。
Java Solution:
Runtime beats 39.82%
完成日期:07/19/2017
关键词:Array
关键点:Dynamic Programming - 维护一个跳跃到最远的值
public class Solution
{
public boolean canJump(int[] nums)
{
int farthestNumIndex = 0; // the farthest place we can jump to
boolean res = false; for(int i=0; i<nums.length; i++)
{
if(i > farthestNumIndex) // meaning there is no way to jump further
break; if(farthestNumIndex >= nums.length - 1) // meaning we can jump to or over the last number
{
res = true;
break;
}
// keep tracking the farthest spot we can jump
farthestNumIndex = Math.max(farthestNumIndex, i + nums[i]);
} return res;
} }
参考资料:
http://www.cnblogs.com/grandyang/p/4371526.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 55. Jump Game (跳跃游戏)的更多相关文章
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode每天一题】Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode(55): 跳跃游戏
Medium! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1, ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
随机推荐
- Java:print、printf、println的区别
printf主要是继承了C语言的printf的一些特性,可以进行格式化输出 print就是一般的标准输出,但是不换行 println和print基本没什么差别,就是最后会换行 System.out.p ...
- Android Studio不更新到最新版使用Kotlin
第一步:安装Kotlin插件 打开Settings面板,找到Plugins选项,点击Browse repositories(浏览仓库),输入“Kotlin”查找,然后安装即可.安装完成之后需要重启An ...
- JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】
什么是JSTL JSTL全称为 JSP Standard Tag Library 即JSP标准标签库. JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历.数据的输出. ...
- 尝试在Linux上部署Asp.net Core应用程序
快两个月没接触.net,倒是天天在用Linux,所以想尝试一下在Linux运行喜欢的.net 应用. 安装CentOS 安装.Net core for Linux 创建Asp.net Core应用程序 ...
- oracle中number类型最简单明了解释
NUMBER (p,s) p和s范围: p 1-38 s -84-127 number(p,s),s大于0,表示有效位最大为p,小数位最多为s,小数点右边s位置开始四舍五入,若s>p,小数点右侧 ...
- 第6章 Overlapped I/O, 在你身后变戏法 ---被激发的 Event 对象 -4
以文件 handle 作为激发机制,有一个明显的限制,那就是没办法说出到底是哪一个 overlapped 操作完成了.如果每个文件 handle 只有一个操作等待决定,上述问题其实并不成为问题.但是如 ...
- 初学node.js有感三
WebStorm下的node.js 一.回顾与继续 在前面,我们知道了node.js的基本框架和思路,在这些原生环境下我们对node.js的设计思想有了比较深刻的认识,并且具有了编写大型程 ...
- C# readonly
当某个字段是引用类型,并且该字段被标记为readonly时,不可改变的是引用,而非字段引用的对象,例如:
- flex的三个属性:
(1)flex-grow:指的是相对于其他的子元素的扩展比率:默认值为0:数字 (2)flex-basis:指的是子元素的具体长度:可以为长度(rem,px,em)也可以为百分比: (3)flex-s ...
- jquery层次选择器:空格 > next + nextAll ~ siblings
全栈工程师开发手册 (作者:栾鹏) jquery系列教程1-选择器全解 jquery层次选择器 jquery层次选择器,包括空格.>.next.+.nextAll.~.siblings等函数或表 ...