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 解答的更多相关文章

  1. Jump Game II 解答

    Question Given an array of non-negative integers, you are initially positioned at the first index of ...

  2. [array] leetcode-55. Jump Game - Medium

    leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...

  3. 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 ...

  4. 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 ...

  5. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  6. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  7. 刷题55. Jump Game

    一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...

  8. [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 ...

  9. [LeetCode] Jump Game 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. 数据结构之顺序表,c#实现

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  2. POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10521   Accepted: 7477 Descri ...

  3. MongoDB安装,打开及增,删,改,查

    MongoDB是目前最流行的NoSQl之一,NoSQL及No Only Sql,之所以这样叫我的理解是它摒弃了传统关系型数据库的字段类型的概念,而是将所有的数据以key-value方式存储,以key索 ...

  4. qt反走样(简选)

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #qt反走样(简选) #概念 """ ...

  5. C#使用自定义字体(从文件获取)

    在进行软件开发,尤其是开发WinForm程序时,有时为了实现界面的美化,不可避免的需要使用一些特殊的字体,但是在开发完成之后,将程序移到其他的机器上时,由于这些机器可能没有安装相应的字体,所以整个界面 ...

  6. Subversion安装

    一.Subversion介绍 Subversion是一个集中式的信息共享系统.版本库是Subversion的核心部分,是数据的中央仓库.版本库以典型的文件和目录结构形式文件系统树来保存信息.任意数量的 ...

  7. 数据分析系统DIY3/3:本地64位WIN7+matlab 2012b訪问VMware CentOS7+MariaDB

    数据分析系统DIY中要完毕的三个任务. 一.用VMware装64位CentOS.数据库服务端用CentOS自带的就好. 二.数据採集与预处理用Dev-C++编程解决. 三.用本地Win7 64上的MA ...

  8. CSS---------------之文本颜色

    CSS2支持如下名字的颜色 注意点: 你的浏览器有可能支持更多名字,但是在实际用的过程中尽量少使用名字的,因为各个浏览器对颜色的会存在差异:查看颜色可以参考 对于更多地颜色,你可以使用代表红,绿,蓝三 ...

  9. T-SQL 函数概述

    T-SQL函数的类别和描述: 函数类别 作用 聚合函数 返回一个标量值,表示在某个值域上的聚合,应用于特定的聚合选择或者汇总 配置变量 返回SQL Server执行环境的信息.这些信息可用于给对象编程 ...

  10. C#控制条码打印机 纸张大小,间距,绘制内容(所有条码打印机通用)

    其他条码知识 请访问:http://www.ybtiaoma.com ,本文仅供参考,请勿转载,谢谢 using System; using System.Drawing; using System. ...