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只能跳出A[i]步。

用一个boolean型的数组记录某个位置能够到达。在位置i时,遍历所有的位置j(j<i),如果能够到达j而且能够从j跳到i,那么最终就可以到达i。

代码如下:

 public class Solution {
public boolean canJump(int[] A) {
if(A == null || A.length == 0)
return true; boolean[] reach = new boolean[A.length];
reach[0] = true; for(int i = 1;i < A.length;i++){
for(int j = 0;j < i;j++){
if(reach[j] && j+A[j]>=i){
reach[i] = true;
break;
}
}
} return reach[A.length-1];
}
}

【leetcode刷题笔记】Jump Game的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. 【leetcode刷题笔记】Jump Game II

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

  6. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

  7. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  8. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  9. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

随机推荐

  1. 北大BBS2008年毕业生晒工资

    http://www.amznz.com/19/快消类: 宝洁:本7200.研8200.博9700,均14个月,另有800交通补助,marketing每9个月 涨20%-30%. 玛氏:月薪10000 ...

  2. 下拉刷新Listview(8.30)

    Android-PullToRefresh 1项目托管地址: https://github.com/bavariama1/Android-PullToRefresh 2 快速开始教程:https:// ...

  3. SlidingMenu+Fragment实现当前最流行的侧滑

    1 http://www.krislq.com/2013/03/android_case_slidingmenu_fragment/ 2 https://github.com/jfeinstein10 ...

  4. PHP接收和发送XML数据(json也通用)

    一.接收xml数据, 使用php://input,代码如下: <?php $xmldata=file_get_contents("php://input"); $data=s ...

  5. MS coco数据集下载

    2017年12月02日 23:12:11 阅读数:10411 登录ms-co-co数据集官网,一直不能进入,FQ之后开看到下载链接.有了下载链接下载还是很快的,在我这儿晚上下载,速度能达到7M/s,所 ...

  6. Sqlserver建立Oracle的鏈接服務器

    --建立数据库链接服务器 EXEC sp_addlinkedserver @server =N'TestOracle', --要创建的链接服务器别名 @srvproduct=N'Oracle', -- ...

  7. 动态注册HttpModule管道,实现global.asax功能

    1.所用类库有 Microsoft.Web.Infrastructure.dll 和WebActivator.dll 2.类代码如下 using System; using System.Collec ...

  8. iOS UIView添加阴影

    _bottomView.layer.masksToBounds = NO; _bottomView.backgroundColor = [UIColor whiteColor]; _bottomVie ...

  9. 关于Java静态代码块、初始化块、构造函数的调用顺寻问题?

    public class ClassA { public ClassA(){ System.out.println("A 构造..."); } { System.out.print ...

  10. 通过Lock对象以及Condition对象实现多线程同步

    通过Lock对象以及Condition对象实现多线程同步: 在之前的学习中,无论是通过synchronized建立同步代码块,还是通过synchronized建立同步函数,都是把对象看成一把锁来实现同 ...