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的更多相关文章

  1. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  2. 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 ...

  3. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  4. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  5. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  6. 【LeetCode】45. Jump Game II

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  7. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  8. [LeetCode] 45. Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. Leetcode 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. MACOS,LINUX,IOS上可用的毫秒级精度时间获取

    二话不说,先上代码 void outputCurrentTime(uint32_t seq,const char* type){ struct timeval t_curr; gettimeofday ...

  2. C++ Primer : 第十二章 : 动态内存之动态数组

    动态数组的分配和释放 new和数组 C++语言和标准库提供了一次分配一个对象数组的方法,定义了另一种new表达式语法.我们需要在类型名后跟一对方括号,在其中指明要分配的对象的数目. int* arr ...

  3. C函数及指针学习2

    1.break  永久终止循环,continue 结束当前循环 2.switch 每个case标签要有唯一值,(且为常量或常量表达式) 不加break 时执行流会贯穿整个case 标签 3 赋值操作符 ...

  4. 笨小猴 2008年NOIP全国联赛提高组

    题目描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设m ...

  5. PHP的一些要点

    1.用户评论的内容应当使用htmlspecialchars()函数进行过滤,如htmlspecialchars($_POST['content']);再写入数据库,防止用户评论中含有JS和HTML代码 ...

  6. 自我提升mysql

    1.某字段更新 自增 1 update table set a=a+1 2.修改某一字段的数据类型 alter table "tablename" modify  "co ...

  7. java多线程:jdk并发包的总结(转载)

    转载地址:http://blog.csdn.net/yangbutao/article/details/8479520 1.java 高并发包所采用的几个机制(CAS,volatile,抽象队列同步) ...

  8. GitHub windows客户端拉代码和提交代码

    1,拉别人的代码, 比如https://github.com/greenrobot/EventBus 这个库,先用浏览器访问,然后git账号登陆,点击fock到自己的库里 点击Fork 然后打开Git ...

  9. c++文件操作相关

    file operation API functions HANDLE CreateFile(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttr ...

  10. mysql学习之-字符集选定,修改。

    基础概念:字符(Character)是指人类语言中最小的表义符号.例如'A'.'B'等:编码(Encoding)是指给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符.例如,我们给字符'A ...