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 ...
随机推荐
- Prepared Java infrastructure for distributed scenarios
code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...
- java的getClass()函数
Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...
- c 函数及指针学习 6
不完整声明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /* 方法一 */ struct tag_a{ ...
- Makefile---make内嵌函数及make命令显示 (九)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubs ...
- svn 安装 、使用(2)
写在前面的话: p.s.有必要读一读,不然可能会浪费你的时间. 该篇是接着上一篇的,上一篇是看了很多人写的文章,汇总的一些可能的情况,最后还是没有成功.此篇是在一个同学的帮助下成功的,也是在自己做好的 ...
- Visual Studio 2012 update3 安装后的问题及解决
安装之后可能遇到的问题: 安装完时,打开Help Viewer时,出现了一个错误提示:”a content file required by the help viewer is missing or ...
- Top Things to Consider When Troubleshooting Complex Application Issues
http://blogs.msdn.com/b/debuggingtoolbox/archive/2011/10/03/top-things-to-consider-when-troubleshoot ...
- python字符串关键点总结
python字符串关键点有下面几点: 1.一些引号分隔的字符 你可以把字符串看出是Python的一种数据类型,在Python单引号或者双引号之间的字符数组或者连续的字符集合.在python中最常用的引 ...
- SpringMVC常用注解,返回方式,路径匹配形式,验证
常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...
- Python_day8_面向对象(多态、成员修饰符、类中特殊方法、对象边缘知识)、异常处理之篇
一.面向对象之多态 1.多态:简而言子就是多种形态或多种类型 python中不支持多态也用不到多态,多态的概念是应用与java/C#中指定传参的数据类型, java多态传参:必须是传参数的数据类型或传 ...