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. jquery Treeview插件的使用及复选框的级联

    本文是对jquery的Treeview插件使用的实例介绍 效果图如下: 文件结构如下:

  2. “凯易迅Calix”实习上机——打折问题

    题目要求: 题目记得不太清楚,大概的意思是一个商店的打折方案如下:设一个客户买了n个商品,价格分别是p1,p2,...,pn (1)第一个商品不打折,即cost=p1; (2)第i个商品的折扣d=mi ...

  3. zookeeper和淘宝dubbo的关系

    Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用:         zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...

  4. Django----Request对象&Response对象

    Django 使用Request 对象和Response 对象在系统间传递状态. HttpRequest 对象: Request.body:一个字节字符串,表示原始HTTP 请求的正文.它对于处理非H ...

  5. 20145216 史婧瑶《Java程序设计》第6周学习总结

    20145216 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1 InputStream与OutputStream 如果要将数据从来源中取出,可以使用输 ...

  6. 20145231熊梓宏 《网络对抗》 实验9 Web安全基础实践

    20145231熊梓宏 <网络对抗> 实验9 Web安全基础实践 基础问题回答 1.SQL注入攻击原理,如何防御? •SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面 ...

  7. localAddress

    $(function(){ <% out.println("/** ip:"+request.getLocalAddr()+"("+request.get ...

  8. ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available

    一.笔者使用ifconfig观察网卡情况如下: root@jello:/# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:0 ...

  9. Mybatis <if>标签使用注意事项

    在<if>标签的test中,不能写成“name !='aa'” , 会报错### Error querying database. Cause: java.lang.NumberForma ...

  10. Why not inherit from List<T>?

    问题: When planning out my programs, I often start with a chain of thought like so: A football team is ...