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.

题目大意:给定一个非负数组,每个元素代表可以跳的步数,判断从第一个能否跳到最后一个。

解题思路:暴力?二重循环,检查是否能到达最后一个元素。

     优化:时间O(n),空间O(1),一次遍历,首先记录当前可以覆盖的最大下标,遍历的时候如果从当前位置可以跳的更远则更新最大下标,如果当前位置不在最大下标覆盖范围内则return false,如果最大下标比数组更长,则return true。

public class Solution {
public boolean canJump(int[] nums) {
if(nums == null){
return false;
}
int pos = 0;
for(int i=0;i<nums.length;i++){
if(pos>=nums.length-1){
return true;
}
if(i<=pos){
if(i+nums[i]>pos)
pos= i +nums[i];
}
else{
return false;
}
}
return false;
}
}

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

  1. Jump Game - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Jump Game - LeetCode 注意点 解法 解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能.并且如果 ...

  2. 55. Jump Game leetcode

    55. Jump Game Total Accepted: 95819 Total Submissions: 330538 Difficulty: Medium Given an array of n ...

  3. Jump Game leetcode java

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

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

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

  8. [LeetCode] Jump Game 跳跃游戏

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

随机推荐

  1. 关于黑名单IP的设置

    最近在做一个项目的时候,需要做一个自动的黑名单设置,也就是将一天内重复出错的超过一定次数的手机号,和IP给加入黑名单里面,下次请求的时候先判断是否在黑名单里. 这个是获取IP地址的方法 private ...

  2. apk文件解析,学习笔记

    Android 应用程序包文件 (APK) 是一种Android操作系统上的应用程序安装文件格式,其英文全称为 “application package file” . 如果懂得使用反编译工具,可以下 ...

  3. C#开发基于Http的LaTeX数学公式转换器

    本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式.具体思路如下: (1)通过TextBox获取用户输入的LaTeX数学表达式, ...

  4. maven发布的资源文件到tomcat项目下

    问题:项目中有hibernate的资源文件,src/main/java和src/main/resources都有这些文件,当启动项目时发现出错.但是src/main/java已经修改好了, 经查tom ...

  5. SGU 199 Beautiful People(DP+二分)

    时间限制:0.25s 空间限制:4M 题意: 有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数. Solution: 显然这是一个两个关 ...

  6. spl_autoload_register()和__autoload()

    关于spl_autoload_register()和__autoload() 看两者的用法: //__autoload用法 function __autoload($classname) {     ...

  7. PHP 数组转JSON数据(convert array to JSON object);

    <?php header('Content-type: appliction/json; charset=shift-JIS'); $data =array(); class Test { pu ...

  8. iframe中在父窗口打开子页面

    我们在做页面框架的时候,通常会采用一个iframe来显示子页面,但有这么种情况,就是session失效时,登录页面就会显示在iframe中,这不符合常理,一般应该显示在顶部才对. 下面的js代码可以解 ...

  9. C#/.NET整数的三种强制类型转换(int)、Convert.ToInt32()、int.Parse()的区别

    这三种方式都是强制把内容转换为整数,但他们之间是有区别的,如下: 一.(int)适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型). 二.int.Parse(string sP ...

  10. 定义文字用em、rem,效果和px一样

    1em=16px font-size: 2.4rem;/*2.4 × 10px = 24px*/