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.


【题目分析】

A[i]的值代表如果到达 i 时可以向前跳的最大步长,判断从i = 0出发是否能到达数组的最后一个值。


【思路】

用distance记录当前i之前跳的最远距离。如果distance< i,即代表即使再怎么跳跃也不能到达i。当到达i时,A[i]+i,代表从i能跳最远的距离。max(distance,A[i]+i)代表能到达的最远距离。


【java代码】

 public class Solution {
public boolean canJump(int[] nums) {
if(nums == null || nums.length == 0) return true; int distance = 0;
for(int i = 0; i < nums.length && i <= distance; i++){
distance = Math.max(nums[i]+i, distance);
}
return distance < nums.length -1 ? false : true;
}
}

LeetCode OJ 55. Jump Game的更多相关文章

  1. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  2. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  3. 【LeetCode】55. Jump Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  4. LeetCode OJ 45. Jump Game II

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

  5. [leetcode greedy]55. Jump Game

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

  6. 【LeetCode】55. Jump Game

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

  7. LeetCode OJ:Jump Game II(跳跃游戏2)

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

  8. LeetCode OJ:Jump Game(跳跃游戏)

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

  9. [LeetCode] 55. Jump Game 跳跃游戏

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

随机推荐

  1. Arch安装KDE5

    plasma desktop Install the plasma-meta meta-package or the plasma group. Alternatively, for a more m ...

  2. cos距离与欧式距离

    1. 欧氏距离(EuclideanDistance) 欧氏距离是最易于理解的一种距离计算方法,源自欧氏空间中两点间的距离公式. (1)二维平面上两点a(x1,y1)与b(x2,y2)间的欧氏距离: ( ...

  3. 安卓用canvas画曲线图

    1.新建一个常变量类Constant.java package com.rain.db; import android.graphics.Point; public class Constant { ...

  4. [Q]pdfFactory打印机内存不能为read的问题

    运行环境:xp系统,AutoCAD2007 1. 使用pdfFactory打印文本文件没有问题. 2. 使用CAD打印的时候出现问题,使用CAD自带的PLOT命令打印也出现相同的问题. 3. 使用相同 ...

  5. Playmaker 基础使用与案例操作

    首先是把下载好的插件导入Unity工程中. ▼导入完成后第一个动作就是检查下拉菜单里面是否已经增加了Playmaker的功能,如果在安装后没看到Playmaker的菜单,一般情况下直接点击菜单上的空白 ...

  6. tomcat7或8如何修改浏览器标题上的ICON小图标

    替换tomcat根目录/webapps/ROOT下的favicon.ico文件,之后重启了服务器,可是还是显示那只猫的图标. 如果不能显示,请清除浏览器的缓存

  7. Jsp中out.println()与System.out.println()的区别

    第一次上Web实验课时咱写了一个jsp程序: <% System.out.println("Hello The World"); %> 然后放在浏览器下运行,结果是这样 ...

  8. QDataStream对QVector的序列化

    最近发现QDataStream这个好东东,序列化发送数据很方便,与大家分享一下. 客户端: line.h #ifndef LINE_H #define LINE_H #include <QStr ...

  9. 移动端默认返回按键,使用h5+修改默认事件

    hbuilder的h5+提供开发webapp的诸多便利,很多手机自带back虚拟按键,如果不修改其默认事件,点一下app就退出了,所以我这里提供一种修改这个按键默认事件事件的代码. 首先你要用hbui ...

  10. 第一个Android crackme(2016-05)

    第一个Android crackme 0x00 背景 最近在学习Android的逆向,把基本的环境搭好后,看了看<第一行代码--Android>,然后就按照非虫大牛的<Android ...