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 ...
随机推荐
- hdu4453-Looploop(伸展树)
题目有很多图,不好粘贴..... 题意:给出N个数和K1,K2的值,最开始指针指向第一个数,有6种操作 add x : 给前K2个数都增加x reverse : 翻转前K1个数 insert x : ...
- 不得不看的JVM内存管理
作为一个任何完整的机器都会有内存管理这块组成结构.作为jvm也有自己的内存管理. 1.那么在java中哪些组件需要使用内存. a) Java堆 b) 线程:线程是在jvm运行 ...
- 设计模式13---设计模式之观察者模式(Observer)(行为型)
1.场景模式抽象 订阅报纸的过程,如果报纸来了的时间不确定,那么订报纸的人如何知道呢?可以抽象为:当一个对象的状态发生改变的时候,如何让依赖他的所有对象得到通知,并进行相应的处理呢?生活中最常见的例子 ...
- 一段代码说明javascript闭包执行机制
假设你能理解以下代码的执行结果,应该就算理解闭包的执行机制了. var name = "tom"; var myobj = { name: "jackson", ...
- mbed OS - ARM关于物联网(IoT)的战略布局
关于IoT 在刚刚过去的ARMTECHCON2014(Santa Clara Convention Center)第1天会议,首要的keynote就是ARM针对建立物联网(InternetOf Thi ...
- 【HDU】 1018 Big Number
大意就是求 : log10(n!) = log10(1 * 2 * 3 * .......*n) = log10(1) + log10(2) + ........+log10(n); 打表的话会ML ...
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...
- js转换ascii编码如中文友转换为编码友;可防止乱码
- oracle 10g 数据库字符集更改
1.更改数据库字符集为GBK SHUTDOWN IMMEDIATE; STARTUP MOUNT EXCLUSIVE; ALTER SYSTEM ENABLE RESTRICTED SESSION;A ...
- [HeadFirst-JSPServlet学习笔记][第一章:前言与概述]
第一章 前言与概述 web服务器做什么? 答:接收客户请求,然后向客户返回结果 web客户做什么? 答:此处客户指浏览器,web客户允许用户请求服务器上的某个资源,并向用户展现请求的结果. html ...