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

Note:
You can assume that you can always reach the last index.


【思路】

ret:目前为止的jump数

curRch:从A[0]进行ret次jump之后达到的最大范围

curMax:从0~i这i+1个A元素中能达到的最大范围

当curRch < i,说明ret次jump已经不足以覆盖当前第i个元素,因此需要增加一次jump,使之达到

记录的curMax。


【java代码】

 public class Solution {
public int jump(int[] nums) {
int sc = 0;
int e = 0;
int max = 0;
for(int i=0; i<nums.length-1; i++) {
max = Math.max(max, i+nums[i]);
if( i == e ) {
sc++;
e = max;
}
}
return sc;
}
}

LeetCode OJ 45. Jump Game II的更多相关文章

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

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

  2. 【LeetCode】45. Jump Game II

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

  3. 【一天一道LeetCode】#45. Jump Game II

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  4. 【LeetCode】45. Jump Game II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  5. [leetcode greedy]45. Jump Game II

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

  6. LeetCode OJ:Jump Game II(跳跃游戏2)

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

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

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

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

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

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

随机推荐

  1. awakeFromNib、initWithCoder、initWithFrame三者区别

    (1)awakeFromNib和initWithCoder:差别awakeFromNib 从xib或者storyboard加载完毕就会调用initWithCoder: 只要对象是从文件解析来的,就会调 ...

  2. 2.开启TFTP,NFS,SAMBA,SSH服务

    一.TFTP (1)dpkg -s tftp-hpa查看服务器端是否安装 (2)如果没安装 sudo apt-get install tftpd-hpa sudo apt-get install tf ...

  3. zabbix 布署实践【6 使用微信公众号-消息模版推送告警】

    使用这个服务的前提是,你必须要有一个微信订阅号,或者公众号,并且是通过认证的号 因为认证过后的号才有模版消息和获取用户openid等信息的权限 ,如下,登录微信公众号的登录页后,底下有个接口权限的展示 ...

  4. Hibernate5-课程笔记4

    单表查询:   Hibernate是DAO层技术,对数据的使用,查询是最为重要的.Hibernate的查询技术非常强大,支持原始SQL语句查询,支持QBC查询及Hibernate特有的HQL查询. H ...

  5. #大数加减乘除#校赛D题solve

    #include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...

  6. Hive 执行计划

    执行语句 hive> explain select s.id, s.name from student s left outer join student_tmp st on s.name = ...

  7. .Net 第三方工具包整理

    抓取数据 Jumony (http://www.jumony.net/)是一个基于 .NET 技术,用 C# 编写的一个 HTML 引擎,其可以用来分析解读 HTML 文档中的数据,也可以修改和绑定数 ...

  8. 使用$_SERVER['HTTP_HOST']时需注意的

    在php中,我们一般通过$_SERVER['HTTP_HOST']来活得URL中网站的域名或者ip地址. $_SERVER['HTTP_HOST']在客户的环境里,取得的值总是程序所在的服务器在其局域 ...

  9. python返回null和空的不同

    mysql数据库中有的字段是NULL, 有的字段是空白 写Python脚本,fetchall()得到结果,也是不同. NULL对应的是None, 空白对应的是'' (None, '') 所以根据结果进 ...

  10. window.open实现模式窗口(只弹出一个window.open)

    父页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...