题目:跳跳游戏

难度:Medium

题目内容

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.

翻译

给定一个非负整数数组,您最初被定位在数组的第一个索引中。

数组中的每个元素代表您在该位置的最大跳跃长度。

确定你是否能够到达最后一个索引。

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: 第一步,跳2步到索引2,第二步跳1步到索引3,第三步跳1步到索引4,到达
        第一步,跳1步到索引1,第二步跳3步到索引4,到达

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: 无论如何,你总是会到达索引3。它的最大跳转长度为0,这使得到达最后一个索引是不可能的。

我的思路:不知道咋做。。。

答案代码

 public boolean canJump(int[] A) {
int max = 0;
for(int i=0;i<A.length;i++){
if(i>max) {return false;}
max = Math.max(A[i]+i,max);
}
return true;
}

答案思路:因为所跳的步数能从0到A[ i ] 都行所以只要前面能达到的最远下标的最大值能大于后面的下标,就表示能跳到,否则跳不到。能达到的最远下标为A[i] + i 。

eg.:[3,2,1,0,4]

前面三个的能达到的最远下标最大值为3,所以此时最远只到下标3,而下标3的最远下标是自己,到了下标4进行判断发现前面的最远下标比自己的下标小,所以跳不到。

LeetCode第[55]题(Java):Jump Game的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  6. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. LeetCode第[15]题(Java):3Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  9. LeetCode第[16]题(Java):3Sum Closest 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...

随机推荐

  1. Spoken English Practice( Believe it or not, I don't need to make believe its a big deal. (believe,deal, You don't say))

    音标复习                                                绿色:连读:红色:略读:蓝色:浊化:橙色:弱读 口语蜕变(2017/6/25) Sorry, t ...

  2. Vmware VsPhere下的VM安装Hyper-v服务

    问题:Vmware VsPhere下的VM无法安装Hyper-v服务 解决方案:打开vmware vm的安装目录,然后找到.vmx的文件,然后进行修改 打开w-c-w2012.vmx文件 添加如下 h ...

  3. python系列十六:Python3 面向对象

    #!/usr/bin/python #-*-coding:gbk-*- #Python3 面向对象 '''面向对象技术简介    类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了 ...

  4. 如何在 window 上面输入特殊字符?

    打开 字符映射表 程序 选中任意一个字符,它会在下方显示该字符的 16进制 转换16进制至10进制,并在输入法打开的状态下,按住 Alt 键输入 10 进制数值即可.

  5. windows7上使用docker容器

    1.安装 下载DockerToolbox,并安装. 下载地址:https://dn-dao-github-irror.daocloud.io/docker/toolbox/releases/downl ...

  6. struct 模块 把一个类型,如数字,转成固定长度的bytes

    该模块可以把一个类型,如数字,转成固定长度的bytes import struct headers=struct.pack('i',132333) print(headers,len(headers) ...

  7. 关于var关键字的详解

    var 在很多语言中都比较常见,到底var是什么,如何应用,下面就笔者常用的javascript.c#对var进行说明: var 是 variable(变量,可变物)的简写.在多种计算机编程语言中,v ...

  8. Redis一(Redis-py与String操作)

    Redis 介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...

  9. pc端用微信扫一扫实现微信第三方登陆

    官方文档链接 第一步:获取AppID  AppSecret (微信开发平台申请PC端微信登陆)   第二步:生成扫描二维码,获取code https://open.weixin.qq.com/conn ...

  10. Python Tornado框架三(源码结构)

    Tornado 是由 Facebook 开源的一个服务器“套装”,适合于做 python 的 web 或者使用其本身提供的可扩展的功能,完成了不完整的 wsgi 协议,可用于做快速的 web 开发,封 ...