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. 分享一下自己在用的CSS样式重置代码

    通过借鉴网上大牛们的经验和自己在工作中碰到的一些问题,总结出了这些比较常用的CSS样式重置的代码: @charset "utf-8"; /* 防止用户自定义背景颜色对网页的影响,添 ...

  2. angular的post提交

    用下来明显感觉jquery的post提交比ng的post提交好用很多 一开始,用angularjs的$http提交的数据,在php服务器端无法通过 因为jQuery会把作为JSON对象的data序列化 ...

  3. java_设计模式_装饰者模式_Decorator Pattern(2016-07-28)

    装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的结构 装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任.换言之,客户 ...

  4. Sublime Text 皮肤插件安装

    安装皮肤, 举例sodahttps://github.com/buymeasoda/soda-themectrl+shift+p => Package Control: Install Pack ...

  5. mysqldump: command not found问题解决

    首先得知道mysql命令或mysqldump命令的完整路径,可以使用find命令查找除非你知道mysql安装路径可以略过这一步. find / -name mysql -print 例如我的mysql ...

  6. Wdcp缺少mod_rewite模块

    1.下载apache源码包,解压并查找到mod_rewrite.c文件 tar -zxvf httpd-2.2.27.tar.gz cd httpd-2.2.27 [root@localhost ht ...

  7. BAE Flask UEditor 使用七牛云

    1. 配置BAE支持七牛云的SDK BAE的python requirements当然不支持竞争对手了. 解决方法: 把qiniu这个文件包直接放置在你项目的目录中(与其他app同级) 运行会发现缺少 ...

  8. 一些CMS网站系统漏洞,练手用(持续更新)

    仅供拿shell,提权测试用,请勿恶意破坏 XuSoft系统: 后台万能密码:'or'='or'  可直接登陆,后台地址 /manage/login.asp inurl:ReadArticlemb.a ...

  9. Python核心编程2第六章课后练习

    6-1 字符串 .string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? #!/usr/bin/env python def contain(str1 ...

  10. OC 之 谓词

    NSPredicate 分类: Objective-C iOS XCode Mac2012-10-26 17:26 10557人阅读 评论(1) 收藏 举报 简述:Cocoa框架中的NSPredica ...