Jump Game 解答
Question
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.
Solution
We apply Greedy Algorithm here to solve the problem. Key to the solution is to check largest distance that every element can reach.
1) when the current position can not reach next position (return false)
2) when the maximum index can reach the end (return true).

public class Solution {
public boolean canJump(int[] nums) {
if (nums == null || nums.length <= 1)
return true;
int max = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0 && max <= i)
return false;
if (i + nums[i] > max)
max = i + nums[i];
if (max >= nums.length - 1)
return true;
}
return false;
}
}
Jump Game 解答的更多相关文章
- Jump Game II 解答
Question Given an array of non-negative integers, you are initially positioned at the first index of ...
- [array] leetcode-55. Jump Game - Medium
leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...
- LeetCode403. Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence
1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- 刷题55. Jump Game
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Asp.net MVC中的ViewData与ViewBag(转)
在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...
- MySQL数据库配置主从服务器实现双机热备
转自:http://www.cnblogs.com/cchun/p/3712637.html 一.安装MySQL 说明:在两台MySQL服务器192.168.21.169和192.168.21.168 ...
- hdu 5033 Building (单调栈 或 暴力枚举 )
Description Once upon a time Matt went to a small town. The town was so small and narrow that he can ...
- debian下编译libev库
系统为Linux debian 2.6.32-5-686.这是裸系统,连xwindows都没有.帐户为root,不是的注意一下权限.这里想说明安装过程及出现的问题,故打印的信息较多,以供出现错误的读者 ...
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...
- date命令小结
date命令是查看日期时间的常用命令,date MMDDhhmmYY.ss(修改顺序)用来更改时间 linux时间分为系统时间和硬件时间, [root@www doc]# clock--------- ...
- Zabbix使用外部邮箱服务器发送邮件报警
本来是想自己写一篇文章的,但是看到发现网上有写的不错的,于是乎又抄别人的文章,作为记录. 使用外部邮箱来发生邮件明显好处就是防止其他邮箱服务器当垃圾邮件处理,另一方面能降低收邮件延迟. 下面开始进行使 ...
- MBTI性格测试
INFP 哲学家型——生活在自己的理想世界 报告接收人: 才储成员4361454 日期: 2014/9/2 一.你的MBTI图形 倾向示意图表示四个维度分别的倾向程度.从中间往两侧看,绿色指示条对应下 ...
- weblogic开机启动脚本
1.在/home/bea/startBeaAll目录内创建一个startBeaAll.sh文件,加入如下内容(把相应目录与命令修改即可,红字部分为修改地方): #!/bin/sh echo " ...
- Xcode6和Xcode5获取app名字
1.在Xcode5下,获取程序名字(app name)的方法为: NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionar ...