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

如题,求到达末位最少的跳的次数。

 class Solution {
private:
int dp[];
public:
int jump(vector<int>& nums) {
int maxPos = ;
dp[] = ;
int pos = ;
int sz = nums.size();
for(int i = ; i <= maxPos; ++i){
pos = i + nums[i];
if(pos >= sz)
pos = sz - ;
if(pos > maxPos){
for(int j = maxPos + ; j <= pos; ++j)
dp[j] = dp[i] + ;
maxPos = pos;
} if(maxPos == sz - )
return dp[sz - ];
} }
};

LeetCode OJ:Jump Game II(跳跃游戏2)的更多相关文章

  1. [LeetCode] 45. Jump Game II 跳跃游戏 II

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

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

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

  3. [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)

    https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...

  4. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

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

  5. [Leetcode] jump game ii 跳跃游戏

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

  6. LeetCode 55. Jump Game (跳跃游戏)

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

  7. 045 Jump Game II 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...

  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】Jump Game I, II 跳跃游戏一和二

    题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...

  10. Leetcode力扣45题 跳跃游戏 II

    原题目: 跳跃游戏 II 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: ...

随机推荐

  1. Extjs 正则表达式 常用的

    extjs正则表达式验证 2011年10月10日 10:36:05 阅读数:7305   在EXT中使用正则表达式验证的方法:fieldLabel : '员工号',name : 'employee.e ...

  2. $ 一步一步学Matlab(3)——Matlab中的数据类型

    小学时候我们就知道,数学中有自然数.整数.分数.小数等等很多种类型的数.到了中学,我们又发现了其实还有无理数.复数这些有些特殊的数.到了大学学了高等数学之后,我们又知道了其实还存在着无穷大.无穷小这样 ...

  3. 【MSDN_C#】C#版本介绍

    C# 1,Visual Studio .NET 2002 首次发布 C# 1.1,Visual Studio .NET 2003 #line 杂注和 xml 文档注释 C# 2,Visual Stud ...

  4. jquery阻止事件冒泡的方法

    $("table tbody").click(function(e) { e.preventDefault(); //阻止自身的事件,并不能阻止冒泡 e.stopPropagati ...

  5. ora-12154:tns:could not resolve the connect identifier specied

    今天在发布网站时,点击登录之后出现了这个问题,修改一下环境变量,该问题就顺利解决了,解决方法如下: 在环境变量里,找到[Administrator的用户变量(U)],在其之下新建一个用户变量: 变量名 ...

  6. MBR主引导记录

    LBA的寻址方式可以让我们支持2TB,这是因为分区相对起始扇区号(分区项08-11个字节)和分区最大扇区数(分区项12-15个字节)的位数都是32bit.也就是0xFFFFFFFF*512/1024/ ...

  7. 20135320赵瀚青LINUX第十八章读书笔记

    概述:调试工作艰难是内核级开发区别于用户级开发的一个显著特点 18.1准备开始 内核调试往往是一个令人挠头不已的漫长过程.幸运的是,在这些费劲的问题中也有不少比较简单而且容易消灭的小bug,运气好你可 ...

  8. 在vim中的复制,剪切,粘贴

    1. 剪切和粘贴 定位鼠标到剪切的开始位置 输入v键开始选择剪切的字符,或者V键是为了选择 整行 移动方向键到结束的地方 d键是剪切,y键是复制 移动鼠标到粘贴的位置 输入P是在鼠标位置前粘贴,输入p ...

  9. 爬虫之Xpath案例

    案例:使用XPath的爬虫 现在我们用XPath来做一个简单的爬虫,我们尝试爬取某个贴吧里的所有帖子,并且将该这个帖子里每个楼层发布的图片下载到本地. # tieba_xpath.py #!/usr/ ...

  10. Android通过soap2访问webservice遇到HTTP request failed, HTTP status: 302的问题

    笔者用C#在服务器端写了一个Webservice,然后再Android客户端通过soap2调用webservice的函数,遇到了HTTP request failed, HTTP status: 30 ...