题目:

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. LA 3942 && UVa 1401 Remember the Word (Trie + DP)

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

  2. c# richTextBox判断是否为图片文件

    //图片 if (richText.Rtf.IndexOf(@"{\pict\") > -1)//条件成立为图片(richText为一个richTextBox的实例名称)

  3. (匹配 最小路径覆盖)Air Raid --hdu --1151

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. Example11(June 9,2015)

    %--------------sort------------------------------- >> A=[ ; ; ] A = >> B=sort(A,)%A(:,)& ...

  5. 集合(一)ArrayList

    前言 这个分类中,将会写写Java中的集合.集合是Java中非常重要而且基础的内容,因为任何数据必不可少的就是该数据是如何存储的,集合的作用就是以一定的方式组织.存储数据.这里写的集合,一部分是比较常 ...

  6. [Delphi]带进度条的ListView

    带进度条的ListView unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, C ...

  7. eclipse/myeclipse清除workspace

    打开Eclipse后,选择功能菜单里的 Windows -> Preferences->, 弹出对话框后,选择 General -> Startup and Shutdownwor ...

  8. Alwayson--辅助副本状态

    1. 同步中(SYNCHRONIZING),主副本和辅助副本之间存在数据差异,并正在进行同步: 2. 已同步(SYNCHRONIZED),主副本和辅助副本之间不存在数据差异,无需要同步的日志: 3. ...

  9. .net core2.0 codefirst 创建数据库的问题!

    appsettings.json和Startup.cs就不记录了,网上很多!! 1.必须在有DbContext类的项目里添加这3个NuGet引用 Microsoft.EntityFrameworkCo ...

  10. ACTGame项目

    项目地址:https://github.com/alonecat06/ACTGame游戏地址:http://pan.baidu.com/s/1hqD3IYw 项目是一个自制单机动作游戏demo,方向是 ...