45.Jump Game II---贪心---2018大疆笔试题
题目大意:与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大疆笔试题的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- 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 ...
- 华为2018软件岗笔试题之第一题python求解分享
闲来无事,突然看到博客园首页上有人写了篇了华为2018软件岗笔试题解题思路和源代码分享.看了下题目,感觉第一题能做出来,就想着用刚刚学的python试着写一下,花费的时间有点长~~,看来又好长时间没练 ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- 【LeetCode】45. Jump Game II
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
随机推荐
- BZOJ4974 字符串大师(kmp)
显然最短循环节长度=i-next[i],则相当于给定next数组构造字符串.然后按照kmp的过程模拟即可.虽然这看起来是一个染色问题,但是由图的特殊性,如果next=0只要贪心地选最小的就可以了,稍微 ...
- BZOJ4922 Karp-de-Chant Number(贪心+动态规划)
首先将每个括号序列转化为三元组(ai,bi,ci),其中ai为左括号-右括号数量,bi为前缀最小左括号-右括号数,ci为序列长度.问题变为在满足Σai=0,bi+Σaj>=0 (j<i)的 ...
- Simpsons’ Hidden Talents HDU - 2594(拓展kmp)
Sample Input clinton homer riemann marjorie Sample Output 0 rie 3 看输出才题意...拓展kmp特征很明显嘛....注意开始就匹配到尾的 ...
- 【BZOJ4516】生成魔咒(后缀自动机)
[BZOJ4516]生成魔咒(后缀自动机) 题面 BZOJ Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔咒串 [1,2]. ...
- 洛谷 3201 [HNOI2009]梦幻布丁 解题报告
3201 [HNOI2009]梦幻布丁 题目描述 \(N\)个布丁摆成一行,进行\(M\)次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为\(1,2,2 ...
- WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271)复现
WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271) -----by ba ...
- linux内核分析 第一周 计算机是如何工作的 20125221银雪纯
我使用的c语言代码是: int g(int x) { return x + 1; } int f(int x) { return g(x); } int main(void) { return f(6 ...
- python基础(5)
使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子 ...
- 【bzoj3173】最长上升子序列
Portal --> bzoj3173 Solution 感觉自己需要智力康复qwq 首先题目给的这个序列肯定是一个\(1-n\)的排列,并且插入的顺序是从小到大 仔细思考一下会发现如果知道了最 ...
- 「Django」rest_framework学习系列-解析器
满足两个要求,request.Post中才有值 1.请求头要求:请求头中的Content-Type为application/x-www-form-urlencoded 2.数据格式要求 name=x& ...