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 ...
随机推荐
- 334. Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- Jpush教材
http://docs.jpush.cn/pages/viewpage.action?pageId=3309574
- c++变量的引用---5
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 1.引用变量的主要用途: 用作函数的形参,通过将引用变量用作参数,函数将使用原始数据而不是其拷贝. 2. ...
- ..c++中用c语言的输入法
题目: 竞选时,要求选民在n个候选人中选择,n个人的名字为 A,B,C,D--连续n个大写字母,如果选择n个人名字之外的人员,则为废票. 统计时以输入'#'为结束标记.请按候选人的得票数目从大到小 ...
- poj1128 拓扑序(DFS)
题意:给出一张图,它是由一系列字母框按一定顺序从下到上摆放,因此上面的字母框会覆盖一部分下面的字母框,确保每个字母框的四条边都至少会出现一个点,要求输出所有可行的摆放顺序,字典序从小到大输出. 首先可 ...
- 关于setInterval()里的this和细节
setInterval(fn,t);里的fn中,要使用外部类的this,则需要先将this保存起来,再使用保存的this,不能直接使用this,里面的this是指向window对象,记住setInte ...
- UNDERSTANDING POSTGRESQL.CONF: CHECKPOINT_SEGMENTS, CHECKPOINT_TIMEOUT, CHECKPOINT_WARNING
While there are some docs on it, I decided to write about it, in perhaps more accessible language – ...
- 【转】c# winform 打包部署 自定义界面 或设置开机启动
方法一: 创建安装部署这部分就不用说了,添加安装部署项目后,鼠标右键安装项目->视图->注册表, 要使软件在开机就运行,可以在HKEY_CURRENT_USER\Software\Micr ...
- ajax重构XMLHttpRequest
//重构XMLHttpRequestvar net = new Object(); //名字空間對象 net.READY_STATE_UNINITIALIZED = 0; net.READY_STAT ...
- C#开发BHO程序(引)
一 C#也可以编写BHO程序 因为eve的官方论坛封掉了签名图功能,所以想自己写个BHO插件来“伪造”签名图.研究了几个C++程序,但是始终对响应WebBrowser内的 HTML事件不得其门.这样就 ...