45. Jump Game II(js)

45. 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 jumps.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
    Jump 1 step from index 0 to 1, then 3 steps to the last index.题意:以数组项的值作为步长,求出最短跳跃次数代码如下:
/**
 * @param {number[]} nums
 * @return {number}
 */
//上次跳:before,当前跳:curr,res:跳跃次数
var jump = function(nums) {
    var len=nums.length;
    var before=0;
    var curr=0;
    var res=0;

    for(var i=0;i<len;i++){
        //前一跳无论如何不能到达终点或越过终点
        if(i>curr){
            return -1;
        }
        //继续向前跳
        if(i>before){
            before=curr;
            res++;
        }
        //记录当前跳能最远达到多远
        curr=Math.max(curr,i+nums[i])
    }
    return res;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10424952.html

时间: 2024-10-24 14:05:01

45. Jump Game II(js)的相关文章

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

【To Read】LeetCode | 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

63. Unique Paths II(js)

63. Unique Paths II A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (

82. Remove Duplicates from Sorted List II(js)

82. Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example

80. Remove Duplicates from Sorted Array II(js)

80. Remove Duplicates from Sorted Array II Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying t

126. Word Ladder II(js)

126. Word Ladder II Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in

137. Single Number II(js)

137. Single Number II Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

JavaScript(JS)之Javascript对象

JavaScript(JS)之Javascript对象 简介: 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的 <script language="javascript"> var aa=Number.MAX_VALUE; //利用数字对象获取可

Scrapy精华教程(六)——自动爬取网页之II(CrawlSpider)

一.目的. 在教程(二)(http://blog.csdn.net/u012150179/article/details/32911511)中使用基于Spider实现了自己的w3cschool_spider,并在items.py中定义了数据结构, 在pipelines.py中实现获得数据的过滤以及保存. 但是以上述方法只能爬取start_url列表中的网页,而网络爬虫如google等搜索引擎爬虫实现的就是对整个互联网的爬取,所以在本教程中研究使用scrapy自动实现多网页爬取功能. 在教程(五)