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. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
随机推荐
- C# 泛型集合
原文出处我的wiki,转载请说明出处 考虑到泛型在集合类型中的广泛应用,这里一起讨论. 1. 泛型 1.1 泛型的定义与约束 创建泛型方法.委托.接口或类时,需要在名称后增加尖括号及其中的泛型参数,泛 ...
- SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】
Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...
- 关于Java中数组的常用操作方法
1. 声明一个数组 String[] arr1 = new String[5]; String[] arr2 = {"a","b","c", ...
- 一条语句导致CPU持续100%
一大早收到一堆CPU预警邮件,通常每天只在统计作业执行期间会收到2~3封CPU预警邮件.这次的预警来自另一台服务器,并且明细数据显示其CPU一直维持在49%.登录到服务器,查看任务管理器(查看资源监视 ...
- 适用初学者:vue2.0构建单页应用最佳实战
参考:https://segmentfault.com/a/1190000007630677 自己gitHub项目地址:https://github.com/shixiaoyanyan/vue-wor ...
- Redis学习笔记之一 : 配置redis
Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...
- 【JAVA零基础入门系列】Day2 Java集成开发环境IDEA
开发环境搭建好之后,还需要一个集成开发环境也就是IDE来进行编程.这里推荐的IDE是IDEA,那个老掉牙的Eclipse还是先放一边吧,(手动滑稽). IDEA的下载地址:http://www.jet ...
- 为什么ABAP开发者需要使用面向对象技术?
ABAP对面向对象的支持已有十多年的历史,然而在生产实践中,我们对这门技术的应用十分有限. 一方面,面向过程的惯性长期存在着:另一方面,对于大部分二次开发工作而言,似乎并没有足够的理由促使开发者使用面 ...
- Crossin 8-3;8-4
8-3文件打开模式:r:只读模式.默认w:只写模式.会先清空文件a:追加写入模式,在文件末尾写入,不可读r+:打开一个文件用于读写.文件指针将会放在文件的开头,原文件内容不会清空b:二进制模式,与前面 ...
- Python自学笔记-logging模块详解
简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...