题目链接

题目大意:与55题类似,只是这里要求出跳数。

法一(借鉴):贪心。cur表示当前能到达的最远距离,pre表示上一次能到达的最远距离,每到一个位置更新一次最远距离cur,如果当前位置超过了上一次能到达的最远距离,则更新跳数和上一次能到达的最远距离。代码如下(耗时6ms):

     public int jump(int[] nums) {
int cur = 0, res = 0, pre = 0;
for(int i = 0; i < nums.length; i++) {
//如果当前位置超过了上一次可以达到的最远距离,更新跳数和上一次可达到的最远距离
if(i > pre) {
pre = cur;
res++;
}
//更新当前最远距离
cur = Math.max(cur, i + nums[i]);
}
return res;
}

45.Jump Game II---贪心---2018大疆笔试题的更多相关文章

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

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

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

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

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

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

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

  5. 华为2018软件岗笔试题之第一题python求解分享

    闲来无事,突然看到博客园首页上有人写了篇了华为2018软件岗笔试题解题思路和源代码分享.看了下题目,感觉第一题能做出来,就想着用刚刚学的python试着写一下,花费的时间有点长~~,看来又好长时间没练 ...

  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. 55 Jump Game i && 45 Jump Game ii

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

  8. 【LeetCode】45. Jump Game II

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

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

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

随机推荐

  1. [二十三]SpringBoot 之 redis

    本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...

  2. 洛谷 P4503 [CTSC2014]企鹅QQ 解题报告

    P4503 [CTSC2014]企鹅QQ 题目背景 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即 ...

  3. 代码收藏系列--php--加载sql文件并解析成数组

    php加载sql文件,解析成以分号分割的数组.(支持存储过程和函数提取,自动过滤注释) /** * 加载sql文件为分号分割的数组 * <br />支持存储过程和函数提取,自动过滤注释 * ...

  4. 【bzoj2707】走迷宫

    Portal --> bzoj2707 Solution 首先题目有一个十分明显的暗示..强联通分量..那肯定就是要tarjan一波咯 先看看什么情况下会\(INF\),其实就是题目里面讲的两种 ...

  5. 前端基础----jquery

    一.jQuery是什么? <1> jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. <2>jQuery是继 ...

  6. 【目标检测大集合】R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记

    R-FCN.SSD.YOLO2.faster-rcnn和labelImg实验笔记 转自:https://ask.julyedu.com/question/7490 R-FCNpaper:https:/ ...

  7. Codeforces 311.E Biologist

    E. Biologist time limit per test 1.5 seconds memory limit per test 256 megabytes input standard inpu ...

  8. 【mysql】测试方案整理

    http://www.cnblogs.com/callhe/ https://www.digitalocean.com/community/tutorials/how-to-measure-mysql ...

  9. mysql 给用户赋值权限

    解决办法 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 拿 joe 1 登陆 附: ...

  10. bzoj 2820 / SPOJ PGCD 莫比乌斯反演

    那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j= ...