题目链接

题目大意:与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. python的N个小功能(文件内容的匹配替换)

    # -*- coding: utf-8 -*- """ Created on Fri Feb 17 20:25:05 2017 @author: who "&q ...

  2. bzoj4798[CEOI2015] Calvinball championship

    这年头,n方跑1万的题已经不多了... 题意 bzoj4798 不知道怎么叙述这个题意... 分析 如果某个序列字典序小于给定的序列,我们不妨考虑从左到右第一个小于给定的序列的位置,并枚举这个位置的数 ...

  3. Android四大组件之contentProvider(续)

    1.content provider与网页有何相似之处? contentProvider使用authority 同网站的域名类似 contentProvider还可以提供类似于网站的索引方式      ...

  4. 【DP】【CF31E】 TV Game

    传送门 Description 给你一个长度为\(2n\)的数字,每次可以从左侧选一个数字,加入连接到一个数字\(A\)或另一个数字\(B\)后面.\(A,B\)初始为\(0\).\(A\)与\(B\ ...

  5. HDU 2586 倍增法求lca

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. ACE服务端编程5:ACE日志输出和跟踪

    服务器程序经常需要在命令行中显示错误消息.状态或者用来跟踪程序的执行路径,最简单的方法是使用printf. ACE提供了更强大日志设施: 1.可以在编译时启用或禁用宏: 2.可以动态的启用或禁用宏: ...

  7. == 和 equals,equals 与 hashcode,HashSet 和 HashMap,HashMap 和 Hashtable

    一:== 和 equals == 比较引用的地址equals 比较引用的内容 (Object 类本身除外) String obj1 = new String("xyz"); Str ...

  8. zTree使用技巧与详解

    zTree--Jquery 树插件,是在后台管理页面中常使用到的插件. 使用效果图: 核心代码: zTree配置: var setting = { data:{simpleData:{enable:t ...

  9. java项目转换依赖等问题

    最近接手了一个原始的java项目,其实很久没有做了,自从两年前用maven,建立web项目,java project基本上就没有弄个,突然的接手,发现自己好多不足,可能对于一开始就做这样的容易,但是对 ...

  10. LightOJ 1017 - Brush (III) 记忆化搜索+细节

    http://www.lightoj.com/volume_showproblem.php?problem=1017 题意:给出刷子的宽和最多横扫次数,问被扫除最多的点是多少个. 思路:状态设计DP[ ...