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#中,有时候我们需要读取枚举值的描述属性,也就是说这个枚举值代表了什么意思.比如本文中枚举值 Chinese ,我们希望知道它代表意思的说明(即“中文”). 有下面的枚举: 1 2 3 4 5 6 ...

  2. Swift入门篇-基本类型(3)

    一:元组 格式 变量或常量关键字 元组变量 = ( 变量,变量, …) 说明: : 元组变量还是一个变量,只不过表现方式和其他变量不一样 :()括号里面可以放入N个变量组成 例子: import Fo ...

  3. 解决VS2010中在项目上右键鼠标,无“添加STS引用”菜单的问题

    解决方法:将Windows Identity Foundation SDK文件夹C:\Program Files (x86)\Windows Identity Foundation SDK\v3.5\ ...

  4. Java 时间和字符换的处理

    /** * * @param timeStr 时间字符串 * @param diff 与起始值差距,单位为毫秒 * @throws ParseException */ public String de ...

  5. IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)

    继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...

  6. iOS中生成并导入基于Swift编程语言的Framework

    从iOS 8.0开始就引入了framework打包方式以及Swift编程语言.我们可以主要利用Swift编程语言将自己的代码打包成framework.不过当前Xcode 7.x在自动导入framewo ...

  7. 怎样用UltraISO制作U盘系统安装盘

    http://jingyan.baidu.com/article/d169e186800f02436711d87b.html 如今用u盘装系统成为主流,如何不被社会淘汰.跟我往下边看吧~~ 工具/原料 ...

  8. git的一些相关知识

    1.配置多个git远程仓库的ssh-Key切换(转自) 目前的git仓库如github都是通过使用SSH与客户端连接,如果只是固定使用单个git仓库的单个用户 (first),生成生成密钥对后,将公钥 ...

  9. Python的Descriptor和Property混用

    一句话,把Property和Descriptor作用在同一个名字上,就只有Property好使.

  10. 提高FOR插入数据库动作的优化代码

    await Task.Factory.StartNew(() => Parallel.ForEach(result.data.o, s => { sql = "insert in ...