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 ...
随机推荐
- CF816E-Karen and Supermarket
题目 Description 今天Karen要去买东西. 一共有 \(n\) 件物品,每件物品的价格为\(c_i\),同时每件物品都有一张优惠券,可以对这件物品减价 \(d_i\) . 使用第 \(i ...
- Expect the Expected UVA - 11427(概率dp)
题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...
- The Toll! Revisited UVA - 10537(变形。。)
给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...
- App简介及登录页面
一. APP目录 app目录: -migrations 数据操作记录,是自动创建的.数据修改表结构 -__init__.py #在python3里面可有可无都行 -__init__.py -admin ...
- 浴谷八连测R4题解
一开始出了点bug能看见排行榜,于是我看见我半个小时就A掉了前两题,信心场QAQ T1字符串题就不说了qwq #include<iostream> #include<cstring& ...
- javaweb之request获取referer请求头实现防盗链
package test.request; import java.io.IOException; import javax.servlet.ServletException; import java ...
- Codeforces Round #416 (Div. 2)A B C 水 暴力 dp
A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- TCP/IP地址格式转换API
1.htonl ()和ntohl( ) ntohl( )-----网络顺序转换成主机顺序(长整型) u_long PASCAL FAR ntohl (u_long netlong); htonl ( ...
- sessionStorage和localStorage的使用
不管是sessionStorage,还是localStorage,可使用的API都相同,常用的有如下几个(以localStorage为例): 保存数据:localStorage.setItem(key ...
- MySQL建表时列名同保留字重复问题解决办法
建表时遇到遇到属性名同MySQL的保留字相同导致不能建表的问题,如下SQL语句: CREATE TABLE TBL_ACCOUNT_FROZEN_RECORD ( ID BIGINT NOT NULL ...