★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9907513.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: 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.

Note:

You can assume that you can always reach the last index.


给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

你的目标是使用最少的跳跃次数到达数组的最后一个位置。

示例:

输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。

说明:

假设你总是可以到达数组的最后一个位置。


16ms

 1 class Solution {
 2     func jump(_ nums: [Int]) -> Int {
 3   if (nums.count == 0 || nums.count == 1) { return 0 }
 4
 5   var res = 0
 6   var mi = 0
 7   for e in 0...nums.count-1 {
 8     if nums[e] == 0 {continue}
 9     var md = 0
10
11     if e < mi {continue}
12     // print(e,mi)
13     for c in e+1...e + nums[e] {
14       if (c >= nums.count-1) { return res + 1}
15       if (c+nums[c] > md) {mi = c}
16       md = max(md,c+nums[c])
17
18     }
19     res += 1
20   }
21   return res
22 }
23 }


80ms

 1 class Solution {
 2     func jump(_ nums: [Int]) -> Int {
 3
 4         var startIndex = 0
 5         var endIndex = 0
 6         var jump = 0
 7         var mostFurther = 0
 8         for i in 0..<nums.count-1{
 9             mostFurther = max(mostFurther, i + nums[i])
10
11             if i == endIndex{
12                 jump += 1
13                 endIndex = mostFurther
14             }
15
16         }
17         return jump
18     }
19 }


96ms

 1 class Solution {
 2     func jump(_ nums: [Int]) -> Int {
 3         var cnt = 0, idx = 0
 4         var cur = 0, pre = 0
 5         while cur < nums.count - 1 {
 6             cnt += 1
 7             pre = cur
 8             while idx <= pre {
 9                 cur = max(cur, idx + nums[idx])
10                 idx += 1
11             }
12         }
13         return cnt
14     }
15 }

转载于:https://www.cnblogs.com/strengthen/p/9907513.html

[Swift]LeetCode45. 跳跃游戏 II | Jump Game II相关推荐

  1. LeetCode——1871. 跳跃游戏 VII(Jump Game VII)[中等]——分析及代码(Java)

    LeetCode--1871. 跳跃游戏 VII[Jump Game VII][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 + 队列 (1)思路 (2)代码 (3)结果 ...

  2. 贪心法—— LeetCode45 跳跃游戏II(跳跃游戏进阶版)

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

  3. leetcode-45 跳跃游戏II

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

  4. 55.跳跃游戏(Jump Game)

    我的GitHub,欢迎捧场:https://github.com/FuGaZn/LeetCode 题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的 ...

  5. LeetCode--45. 跳跃游戏Ⅱ(贪心)

    跳跃游戏Ⅱ(C) 1. 题目描述 2. 题目分析 3. C语言实现 3.1 最大跨越点算法 3.2 贪心算法 1. 题目描述 难度:困难 2. 题目分析 该题目是LeetCode55.跳跃算法的进阶版 ...

  6. leetcode45. 跳跃游戏 II

    一:论语 己所欲 也要勿施于人 ,每个人的经历和阅历都是不同的 你凭啥说你认为的很开心的事情 去要求别人呢 二:题目 三:上码 class Solution {public:int jump(vect ...

  7. leetcode45 跳跃游戏II 秒杀所有答案

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

  8. LeetCode 45. 跳跃游戏 II Jump Game II

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

  9. Leetcode-45. 跳跃游戏Ⅱ

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

最新文章

  1. mysql python 接口_Python中的MySQL接口:PyMySQL MySQLdb
  2. 手机 服务器 推送消息推送消息,推送信息到手机的pushover使用方法及sample code
  3. 索引-linux-技术大钢
  4. leetcode 移动零
  5. spring boot + swagger2
  6. EF架构~将数据库注释添加导入到模型实体类中
  7. 启动不起来_汽车没电发动不起来咋办?老司机平时都用这几种方法,简单有效...
  8. VS生成dump文件和调试dump文件
  9. mysql 优化sql语句的几种方法
  10. 朋友让帮忙写个理发店的会员信息管理系统...
  11. 软件测试---组织架构图和范围测试列表
  12. 哪上班 | 好工作近在咫尺
  13. 【金融财经】金融市场一周简报(2018-03-30)
  14. 【web前端期末大作业】简单的学生网页作业源码 基于html css javascript jquery技术设计的音乐网站(44页)
  15. 计算机监控系统sacad,太阳能热泵多功能复合机(sahpm)计算机监控系统实现方法研究-机械电子工程专业论文.docx...
  16. 2022南理工824专考研经验
  17. 计算机主板上的bios与cmos的关系是,bios cmos 关系是什么?有什么区别?
  18. 顺序栈(含有栈顶指针,栈底指针)的实现以及编写过程中的一些疑惑的解决
  19. 51单片机C语言code定义,51单片机数组的定义方法(code与data的作用)
  20. 软考系统架构设计师范文2:论面向服务的架构及其应用

热门文章

  1. 转载:分享一下免费的ppt网站(好看,免费)
  2. EasyStruct.js轻松创建可填入式html模板结构
  3. 另外一篇关于JS页面跳转代码
  4. 使用Fork/Join框架优化归并排序
  5. CTFshow php特性 web98
  6. poj 3579 Median 中间值(二分搜索)
  7. java list子类_List集合的子类ArrayList、LinkedList、Vector
  8. CUDA编程--实现并行矩阵乘法【80行代码】
  9. 3行代码实现从excel中读取出某列元素为所想要的元素集合中的所有行
  10. 第二章:2.1 微分方程、差分方程求解(举例)