Description:

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.

贪心,每次取最大step看是否能够到达最后的索引。

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

LeetCode——Jump Game的更多相关文章

  1. [LeetCode] Jump Game 跳跃游戏

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

  2. [LeetCode] Jump Game II 跳跃游戏之二

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

  3. LeetCode——Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [leetcode]Jump Game @ Python

    原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: Given an array of non-negative integers, you ar ...

  5. LeetCode: Jump Game II 解题报告

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

  6. LeetCode: Jump Game Total 解题报告

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

  7. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

  8. [LeetCode] Jump Game II 贪心

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

  9. Leetcode jump Game II

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

  10. Leetcode jump Game

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

随机推荐

  1. C#窗体全屏功能

    最近有朋友让我给他弄个应用程序全屏的功能,例如银行的取号程序界面.所以我从网上查询了一些实现的方法. C#应用程序中如何实现全屏幕显示功能? 效果就像windows自带的屏幕保护程序和众多的游戏那样, ...

  2. NEWS - InstallShield 2015 正式发布

    如果您需要为Windows®应用程序创建安装,InstallShield®便是您的最佳解决方案.在为桌面.服务器.云.Web和虚拟环境构建可靠的Windows Installer (MSI)和Inst ...

  3. Scrum介绍

    Scrum介绍 摘要 如今,项目管理的步伐越来越快.项目管理需要更灵活.更积极地,向应客户的需求.使用敏捷项目管理方法,项目经理可以在不影响价值.质量和商业规则的前提下实现所有目标,Scrum是一种迭 ...

  4. 解决vbox下安装centos不能上网问题

    由于工作需要用到Centos做服务器,使用VBOX安装Centos7系统后发现不能上网,记录解决方法,以便下次使用.找到/etc/sysconfig/network-scripts/ifcfg-enp ...

  5. 使用 Python SimpleHTTPServer 快速共享文件

    近期,想着从一个服务器 向另一个服务器传输文件,但是对其知之甚少,就从别人那里知道一种方法,使用 Python SimpleHTTPServer 快速共享文件. 直接运行:python -m Simp ...

  6. 使用 CountDownLatch 控制多个线程执行顺序

    已同步更新至:http://dxjia.cn/2015/08/countdownlatch-use/ 有时候会有这样的需求,多个线程同时工作,然后其中几个可以随意并发执行,但有一个线程需要等其他线程工 ...

  7. Lua截取utf-8编码的中英文混合字符串

    参考博客:UTF8字符串在lua的截取和字数统计[转载] 需求 按字面个数来截取子字符串 函数(字符串, 开始位置, 截取长度) utf8sub(,) = 好1世界哈 utf8sub(,) = 你好1 ...

  8. Android学习笔记----Activity的生命周期图示

    转载,一目了然.

  9. T-SQL 小数点转换百分数

    -- ============================================= -- Author: <Author,,CC> -- Create date: <C ...

  10. Vim 练级攻略

    以下的文章翻译自<Learn Vim Progressively>,我认为这是给新手最好的VIM的升级教程了,没有列举全部的命令,仅仅是列举了那些最实用的命令. 很不错. -------- ...