题目:

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.

思路:

定义一个数来记录某一时刻所能达到的最远距离,遍历数组,每次更新。

/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function(nums) {
var n=nums.length,canarr=0;
for(var i=0;i<=canarr&&canarr<n-1;i++){
canarr=Math.max(i+nums[i],canarr);
} return canarr>=n-1;
};

【数组】Jump Game的更多相关文章

  1. TopCoder SRM 633div1

    250pts   PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳 ...

  2. vue实现分页组件

    创建pagination.vue /* * 所需参数 * total Number 总页数 * current Number 当前页面下标 * pageSize Number 页面显示条数 * siz ...

  3. Buy Fruits-(构造)

    https://ac.nowcoder.com/acm/contest/847/C 在blueland上有 n n个水果店,它们的编号依次为 0,1,2...n−1 0,1,2...n−1.奇妙的是, ...

  4. [CSP-S模拟测试]:跳房子(模拟)

    题目描述 跳房子,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子是在$N$个格子上进行的,$CYJ$对游戏进行了改进,该成了跳棋盘,改进后的游戏是在一个$N$行$M$列的棋盘上进行,并 ...

  5. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  6. Code Chef JUMP(递推+树状数组+李超线段树)

    \(JUMP\) 很容易写出转移柿子 \[f_i=\min_{p_j<p_i}\{(h_i-h_j)^2+f_j\}+w_i\] 把\(\min\)里面的东西展开一下 \[f_j=\min_{p ...

  7. [LeetCode] Jump Game 数组控制

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

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

  9. [LeetCode] Jump Game 跳跃游戏

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

随机推荐

  1. Java 注解概要

    转载自:https://www.cnblogs.com/peida/archive/2013/04/24/3036689.html(Java注解就跟C#的特性是一样的) 要深入学习注解,我们就必须能定 ...

  2. MATLAB常用函数

      Matlab的内部常数 pi                   圆周率 exp(1)             自然对数的底数e i 或j                虚数单位 Inf或 inf ...

  3. (转)SQL Server 2008无法修改表的解决办法

    转自:http://www.soaspx.com/dotnet/sql/mssql/sql2008/sqlserver2008_20121010_9683.html 在SQL Server 2008 ...

  4. 利用Project Tango进行室内三维建模 精度评定

    coming soon 在Android开发基础上开发Tango应用 Android+Tango

  5. (并查集 贪心思想)Supermarket -- POJ --1456

    链接: http://poj.org/problem?id=1456 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  6. Unidac连接出错:命名管道提供程序:管道的另一端上无任何进程.

    环境: DELPHI XE ,UniDAC 6.2.8, (Options->Provider=prNativeClient) 程序编译后,放在Windows Server 2003 (sp2 ...

  7. jQuery之noConflict() 方法

    jQuery 核心 - noConflict() 方法,运行这个函数将变量 $ 的控制权让渡给第一个实现它的那个库.这有助于确保jQuery不会与其他库的$对象发生冲突. noConflict() 方 ...

  8. 拒绝“高冷”词汇!初学C#中实用的泛型!

    初学C#,整天对着业务逻辑,写反反复复的“过程型”代码逻辑十分枯燥,不如用点新东西提升一下代码效率,让代码看起来更有逼格?! 好,下面我们看看C#中简单易学的泛型. 首先,我写了一个这样的方法 --- ...

  9. JaveScript之CSS变量

    大概是CSS3吧,出了一个叫CSS变量的东西,也叫自定义属性,还是比较有用的东东,可以用JavaScript灵活控制,变量作用 我们来实现一个div跟随鼠标滚动的小东西用来说明如何自定义变量 :roo ...

  10. K8S+GitLab-自动化分布式部署ASP.NET Core(三) 更新镜像版本并部署到K8S上

    一.介绍 前一篇,介绍了ASP.NET Core部署到K8S上,下面介绍我们在发布新一版本中怎么通过Gitlab CI自动给镜像打版本并部署到K8S上. 二.我们通过GitLab CI/CD 变量 不 ...