Description:

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.

For example:
Given array A = [2,3,1,1,4]

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.)

Code:

 int jump(vector<int>& nums) {
if (nums.size() <= )
return ; int jumpNum = , curDis = , maxDis = ;
for (int i = ; i < nums.size(); ++i)
{
if (curDis < i)
{
jumpNum++;
curDis = maxDis;
}
maxDis = max(maxDis, nums[i]+i);
}
return jumpNum;
}

Jump Game II的更多相关文章

  1. 57. Jump Game && Jump Game II

    Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...

  2. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  3. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  4. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. 【LeetCode】45. Jump Game II

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  7. Leetcode 55. Jump Game & 45. Jump Game II

    55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...

  8. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  9. [leetcode解题记录]Jump Game和Jump Game II

    Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...

  10. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

随机推荐

  1. Oracle找出非数字

    可以这样判断: select translate('99999999999999', '\1234567890', '\') from dual; 返回的是空 ​ select translate(' ...

  2. hbase regionserver IO问题

    regionserver日志: java.io.IOException: Connection reset by peer         at sun.nio.ch.FileDispatcherIm ...

  3. python 中time.sleep没有作用

    很简单的一个程序: # -*- coding: utf-8 -*- import MySQLdb,os,json,time #from wsgiref.simple_server import mak ...

  4. reactjs入门到实战(十)----one-first_app

    index <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!-- ...

  5. IDEA打war包

    一 第一步创建一个web application:expolded 选择当前项目 二 新建一个web application: Archive 选择刚刚新建的Expoded  “For ‘...... ...

  6. python学习笔记五 模块下(基础篇)

    shevle 模块 扩展pickle模块... 1.潜在的陷进 >>> import shelve>>> s = shelve.open("nb" ...

  7. COGS502. 长路上的灯

    502. 长路上的灯 ☆   输入文件:light.in   输出文件:light.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 在一条无限长的路上,有一排无限长的路 ...

  8. 命令行下运行php的方法和技巧

    linux中直接用"php"命令来执行php文件 一般在linux命令行下运行php文件的代码: XML/HTML代码 linux下执行:#php安装路径 -f php文件路径 例 ...

  9. linux 切换多个jdk脚本

    1,编写脚本 jdkswitch.sh #!/bin/sh # usage: . this_file [argvs] openjdk7_home=/usr/lib/jvm/java--openjdk- ...

  10. 【MySQL】MySQL复制表结构、表数据

    平常,复制.备份表,一般都直接操作IDE完成.但有时,一些初始化数据的脚本,在操作数据前,最好备份下操作表的结构.数据,不至于出错了被置于为难的境地. 所以复制表结构.表数据的语句就派上用场. > ...