45. Jump Game II
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.)
============
题目:非负整数数组,假定一开始你在数组的第一个位置,数组中元素表示你能jump的最大距离。
你的目标是利用最少的步数 jump到数组末尾
返回最少的次数?
=======
思路:贪心的思路(什么是贪心,特征是什么?)
class Solution {
public:
int jump(vector<int> nums){
int result = ;
int last = ;///the maximum distance that has been reached
int curr = ;///the maximum distance that can be reached by using "ret+1" steps
for(int i = ;i<nums.size();i++){
if(i>last){
result++;
last = curr;
}
curr = max(curr,nums[i]+i);
}
return result;
}
};
45. Jump Game II的更多相关文章
- [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 ...
- 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 ...
- 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 (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- Jdbc 连接MySQL数据库的方法和问题
用eclipse写代码.编译之前,先右键点击项目,选择Build Path->library->add external library, 到MySQL网站下载MySQL的Jdbc驱动,把 ...
- MySQL二进制日志的备份和恢复
二进制日志:记录数据库修改的相关操作,作用是即时点回复,主从复制 可以按时间滚动,也可以按大小滚动 server-id:服务器身份标识 一.二进制文件的删除方法,千万不要手动删除 PURGE BINA ...
- OOP作业
1,定义一个水果类(fruit),水果类中的有[属性]:颜色(color).价格(price).重量(weigth),再定义一个<测试类>,创建一个苹果(apple)的对象, 颜色是&qu ...
- Kali安装
Kali Linux 前身是 BackTrack ,不过Kali Linux是基于Debian 的 Linux 发行版,而BackTrack 则是基于Uubntu的,现在BackTrack 已经不更新 ...
- 布局转换:文档流->绝对定位
布局转换:文档流->绝对定位(详见妙味JS高级教程,运动课程第6课20分钟起)比如一个DIV中有三张图片并排,个数不确定的布局.需要鼠标移上去图片从中心放大,只使用float:left布局在放大 ...
- meta是什么意思?
META标签,是HTML语言head区的一个辅助性标签.在几乎所有的page里,我们都可以看 到类似下面这段html代码: -------------------------------------- ...
- ExtJS参考手册
ExtJS是一个用javascript写的,主要用于创建前端用户界面,是一个与后台技术无关的前端ajax框架.因此,可以把ExtJS用在.Net.Java.Php等各种开发语言开发的应用中.ExtJs ...
- android开源项目---tool篇
本文转载于:http://blog.csdn.net/likebamboo/article/details/19080801 主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库OR ...
- centos启动流程[转]
启动流程概览 在硬件驱动成功后,Kernel 会主动呼叫 init 程序,而 init 会取得 run-level 资讯: init 运行 /etc/rc.d/rc.sysinit 文件来准备软件运行 ...
- Google Java Style Guide
https://google.github.io/styleguide/javaguide.html Table of Contents 1 Introduction 1.1 Terminolog ...