题目:跳跳游戏

难度: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. 下载flv格式视频

    我们再看flash plaer播放视频时,有些时候需要下载,无奈找不到下载的按钮.这时,我们可以用以下的方式来进行下载. 其它格式估计也是有迹可循,大家仔细看看网页源代码,看到类似于这种地址,看到有相 ...

  2. 决策树ID3算法python实现 -- 《机器学习实战》

    from math import log import numpy as np import matplotlib.pyplot as plt import operator #计算给定数据集的香农熵 ...

  3. Oracle存储——逻辑结构

    Oracle 数存储——物理结构 Oracle存储结构:物理结构+逻辑结构 Oracle 数据库存储逻辑结构 Oracle Schema Objects(Schema Object Storage A ...

  4. 解决EF6中表名变成复数的情况

    在用EF6 时,在进行数据调用的时候,总提示数据表名对象错误.. 解决此问题需要在继承DbContext的实体类中 加入: using System; using System.ComponentMo ...

  5. boost:property_tree::ini_parser:::read_ini 读取ini时崩溃

    原因: 1 路径错误 2 配置文件中某一行缺少=,例如用// 做注释的,前面应该加";" 解决办法: 添加异常处理,实例代码如下: #include <boost/prope ...

  6. CListCtrl消息及解释

    对于CListCtrl消息的解释:[来自网络]LVN_BEGINDRAG 鼠标左键正在被触发以便进行拖放操作(当鼠标左键开始拖拽列表视图控件中的项目时产生) LVN_BEGINRDRAG 鼠标右键正在 ...

  7. 在容器最前面添加DOM元素 parent.insertBefore(new, parent.children[0])

    //判断容器当前有没有子级元素,如果没有直接appendChild就行了; if (p.children[0]) { p.insertBefore(span, p.children[0]); } el ...

  8. Virtualbox报错------>make sure the kernel module has been loaded successfully

    错误描述 很久没有用virtualbox了,今天打算在virtualbox上安装一个Ubuntu系统的时候,新建好Ubuntu后启动的时候,直接报错: Cannot access the kernel ...

  9. Win10 jdk的安装以及环境变量的配置,及需要注意的坑

    此篇文章献给自己,希望下次长点记性 最近本人终于有时间开始学习appium,并且开始在电脑上配置环境,第一步就是在我那刚装的Win10 系统上安装jdk,过程并不顺利,由于之前都是用的win7,几乎都 ...

  10. docker镜像制作---jdk7+tomcat7基础镜像

    1. 安装docker rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm y ...