【数组】Jump Game
题目:
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的更多相关文章
- TopCoder SRM 633div1
250pts PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳 ...
- vue实现分页组件
创建pagination.vue /* * 所需参数 * total Number 总页数 * current Number 当前页面下标 * pageSize Number 页面显示条数 * siz ...
- Buy Fruits-(构造)
https://ac.nowcoder.com/acm/contest/847/C 在blueland上有 n n个水果店,它们的编号依次为 0,1,2...n−1 0,1,2...n−1.奇妙的是, ...
- [CSP-S模拟测试]:跳房子(模拟)
题目描述 跳房子,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子是在$N$个格子上进行的,$CYJ$对游戏进行了改进,该成了跳棋盘,改进后的游戏是在一个$N$行$M$列的棋盘上进行,并 ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- 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 ...
- [LeetCode] Jump Game 数组控制
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [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 ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- PCL(point cloud library) 学习——简介
Point Cloud Library (PCL) 是开源点云处理库, 包括 filtering, feature estimation, surface reconstruction, regist ...
- ROM初始化HEX文件
intel hex格式 记录格式 Intel HEX由任意数量的十六进制记录组成.每个记录包含5个域,它们按以下格式排列: :llaaaatt[dd...]cc 每一组字母对应一个不同的域,每一个字母 ...
- 端口模式(IN,OUT,INOUT,BUFFER)
in: OUT: INOUT: BUFFER:缓冲模式,与OUT类似可作为输出使用,但也可把输出的信号作为输入使用.
- C++ 11可变参数接口设计在模板编程中应用的一点点总结
概述 本人对模板编程的应用并非很深,若要用一句话总结我个人对模板编程的理解,我想说的是:模板编程是对类定义的弱化. 如何理解“类定义的弱化”? 一个完整的类有如下几部分组成: 类的名称: 类的成员变量 ...
- Android操作HTTP实现与服务器通信
(转自http://www.cnblogs.com/hanyonglu/archive/2012/02/19/2357842.html) 本示例以Servlet为例,演示Android与Servlet ...
- Eclipse代码提示功能设置
1. 解决实例化时自动补全不必要的单词问题 2. 以MyEclipse 6.5重新配图 鉴 于网上的批评之声甚大,我只想说明我的想法:这样的增强代码提示,最终是用来辅助我们 ...
- Python学习-28.Python中的列表切片
在实际的业务过程中,经常有这么一个操作:取一个列表的一部分进行操作,而不影响原来的列表.也就是将原来的列表复制一个部分副本并对其进行操作. 举个完整例子: list = ['a','b','c','d ...
- dstat常用参数组合
io/if/vm三合一 dstat -cdlmnpsy dstat --top-mem --top-cpu --top-io
- asp.net Ibatis.net 批量插入数据ORACLE
在开发中我们有时会遇到需要批量插入数据,最普通的就是每次 插入一条.但是当数据量大道一定的地步会很影响性能.下面例子示范了ibatis.net批量插入 ibatis.net 的XML文件里面使用ite ...
- Ubuntu16.04 - 安装RabbitVCS,linux下的TortoiseSVN!!!
RabbitVCS 官网:http://rabbitvcs.org/ 1,添加PPA源.在shell里面执行下面命令: sudo add-apt-repository ppa:rabbitvcs/pp ...