【数组】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;
};
时间: 2024-10-15 08:59:03

【数组】Jump Game的相关文章

TC SRM 633div1

250pts   PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳完后从从头开始,问最后能否达到(x,0). 限制:|x| <= (int)1e9,n <= 50, 1 <= len[i] <= (int)1e9. 分析: 题解分析的很详细,每次跳完后可达的范围离原点的距离总是一个区间[a,b],考虑下一次跳的长度r之后,可达的范围, (1). r

vue实现分页组件

创建pagination.vue /* * 所需参数 * total Number 总页数 * current Number 当前页面下标 * pageSize Number 页面显示条数 * sizes Array 页面条数容器数组 * jump(index) function 重新获取数据的方法 * getPageSize(index) function 更改页面显示条数的方法 */ <style lang="less"> @color:#1199F0; .page-w

Buy Fruits-(构造)

https://ac.nowcoder.com/acm/contest/847/C 在blueland上有 n n个水果店,它们的编号依次为 0,1,2...n−1 0,1,2...n−1.奇妙的是,每个水果店都只卖一种水果,且这些水果店卖的水果种类都各不相同. 在每个水果店有一个传送门,且这些传送门也有各自的编号,其中 i i号水果店的传送门编号为 Ai Ai,每个传送门的编号也各不相同,且是 [0,n−1] [0,n−1]中的一个整数.简单的说, A0A1A2...An−1 A0A1A2..

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

题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. 求解出从下标为0开始到下标到数组最后一个所需要的最少跳动次数! 1.当数组为空或者数组长度等于1时,不需要跳动,因此返回0 否则初始化step=1 2.初始化left=0 right = nums[0] 当left<=right时进入循环体中. 3.从第i=0开始,如果当前跳动距离大于数组长度则返回st

LeetCode45 Jump Game II

题目: 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. Your goal is to reach the last index in the minimum number of j

hdu-4417 Super Mario(树状数组 + 划分树)

题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is

【Jump Game II 】cpp

题目: 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. Your goal is to reach the last index in the minimum number of j

jump display

查找了数据库,再在while里拼接成json是很麻烦的,所以,jump display 获得数组 <?php header("Content-Type:text/html; charset=utf-8"); /*json接口测试*/ $g=$_GET['g']; $v=$_GET['v']; $msu=$_GET['msu']; print_r ($g); print_r ($v); print_r ($msu); /*json*接口测试*/ //进行my数据库连接 $con =

leetcode. 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 =