【数组】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 ...
随机推荐
- 7系列GTX中的疑惑
1.PCOMMA与MCOMMA指什么? PCOMMA是指RD-部分的数据,MCOMMA是指RD+部分的数据. 2.risk信号作用? risk信号来指示有效的K码. 如果不选择用8b10b来编码,是需 ...
- 关于on commit preserve rows与on commit delete rows的测试
1:先分别建立两张表 sql> CREATE GLOBAL TEMPORARY TABLE tmp_session on commit preserve rows as select * fro ...
- 20155305乔磊2016-2017-2《Java程序设计》第七周学习总结
教材学习内容总结 第十二章 Lambda 12.1 认识Lambda语法 - Lambda 教材的引入循序渐近.深入浅出 如果使用JDK8的话,可以使用Lambda特性去除重复的信息,例: Compa ...
- Shiro 登录页面的几个固定字段
http://shiro.apache.org/webapp-tutorial.html Step 3b: Add a login page Since Step 3a enabled login a ...
- mysql中要根据某个逗号分割的字符串关联查询另一张表的数据
首先观察下面的查询 select * from company where f_id in ('210','205','208') select * from company where f_id i ...
- Java火焰图在Netflix的实践
转自 http://www.infoq.com/cn/news/2015/08/java-flamegraph 亲爱的读者:我们最近添加了一些个人消息定制功能,您只需选择感兴趣的技术主题,即可获取重要 ...
- cordova使用webrtc与网页端及移动端视频、语音聊天
最近在做一个移动端与移动端.网页端文字.视频.语音聊天的功能.文字聊天使用websocket,在网上很多资料,也没什么难度.但是在视频.语音聊天上遇到了小小的难点.之前一直在找一些SDK想快速开发,例 ...
- cmder简单使用
window命令行的替代工具cmder.至于为什么要找个替代cmd的工具,你懂得! 一 官网下载 http://gooseberrycreative.com/cmder/ 二 安装 1 直接解压 2 ...
- underscore objects
1._.keys():获取对象的属性名,不包含原型链 _.keys = nativeKeys || function(obj) { if (obj !== Object(obj)) throw new ...
- 分享一个经验,代码打开mysql链接,执行存储过程时,提示:Table 'mysql.proc' doesn't exist
先说说的场景 老项目,因为服务器升级了mysql数据库版本,从5.7.13升到8.0.15 然而代码里面有直连数据的访问,通过执行存储过程来查询数据的业务,此时抛出异常 Table 'mysql. ...