[Swift]LeetCode55. 跳跃游戏 | Jump Game

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.

Determine if you are able to reach the last index.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
             jump length is 0, which makes it impossible to reach the last index.


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

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

判断你是否能够到达最后一个位置。

示例 1:

输入: [2,3,1,1,4]
输出: true
解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。

示例 2:

输入: [3,2,1,0,4]
输出: false
解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。

16ms
 1 class Solution {
 2     func canJump(_ nums: [Int]) -> Bool {
 3         var bestPos = 0
 4
 5         var i = nums.count - 1
 6         while (i >= 0 ) {
 7             if (nums[i] + i >= bestPos) {
 8                 bestPos = i
 9             }
10             i -= 1
11         }
12
13         return bestPos == 0
14     }
15 }


20ms

 1 class Solution {
 2     func canJump(_ nums: [Int]) -> Bool {
 3         if nums.isEmpty { return true }
 4         var ind = nums.count - 1
 5         for i in (0..<nums.count).reversed() {
 6             if (i + nums[i]) >= ind {
 7                 ind = i
 8             }
 9         }
10         return ind == 0
11     }
12 }


24ms

 1 class Solution {
 2     func canJump(_ nums: [Int]) -> Bool {
 3         var maxIndex = nums[0]
 4
 5         for (index, val) in nums.enumerated() {
 6
 7             if index > maxIndex {
 8                 return false
 9             }
10
11             maxIndex = max(maxIndex, index + val)
12         }
13
14         return true
15     }
16 }


24ms

 1 class Solution {
 2     func canJump(_ nums: [Int]) -> Bool {
 3         let count = nums.count
 4         if count > 0 {
 5             var index = 1 // 现在检查
 6             var maxIndex = nums.first! // 现在最多去到
 7             if maxIndex >= count - 1 {
 8                 return true
 9             }
10             var newIndex = 0 // 新的最多去到
11
12             while index <= maxIndex {
13                 newIndex = nums[index] + index
14                 if newIndex > maxIndex {
15                     maxIndex = newIndex
16                     if newIndex >= count - 1 {
17                         return true
18                     }
19                 }
20                 index += 1
21             }
22         }
23         return false
24     }
25 }


36ms

 1 class Solution {
 2     func canJump(_ nums: [Int]) -> Bool {
 3         var lastPos = 0
 4         for (index, num) in nums.enumerated() {
 5             if index > lastPos {
 6                 return false
 7             }
 8
 9             lastPos = max(lastPos, num + index)
10         }
11
12         return true
13     }
14 }

原文地址:https://www.cnblogs.com/strengthen/p/9917793.html

时间: 2024-08-28 02:55:48

[Swift]LeetCode55. 跳跃游戏 | Jump Game的相关文章

[Swift]LeetCode45. 跳跃游戏 II | 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

【LeetCode每天一题】Jump Game II(跳跃游戏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.

计蒜客 跳跃游戏

跳跃游戏 给定一个非负整数数组,假定你的初始位置为数组第一个下标. 数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出“true”,否则输出“false” 样例输入 5 2 0 2 0 1 样例输出 true 1 #include

跳跃游戏 II

题目描述:给出一个非负整数数组,你最初定位在数组的第一个位置.数组中的每个元素代表你在那个位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例:给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次) 跟上一道题:"跳跃游戏"(详见:点击打开链接)基本逻辑是一样的,都是贪心法的应用.其实这道题当中,贪心法用得更明显了. 为了能够以最少的步数跳到最后,则每次

LeetCode(45): 跳跃游戏 II

Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2.   从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置. 说明: 假设你总是可以到达数组的最后一个位置. 解题思路: 这题是之前那道Jump Game 跳跃游戏 的延伸,那题是问能不能

[leetcode] 55. 跳跃游戏

55. 跳跃游戏 分明就是45. 跳跃游戏 II的缩水版本嘛..??,难度高的版本居然放在了前面,把像我这种有强迫症的必须按照题号刷题的人不放在眼里么... class Solution { public boolean canJump(int[] nums) { return jump(nums) != Integer.MAX_VALUE; } public int jump(int[] nums) { int f[] = new int[nums.length]; if (nums.leng

[Leetcode]44.跳跃游戏Ⅰ&amp;&amp;45.跳跃游戏Ⅱ

跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: true 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置. 示例 2: 输入: [3,2,1,0,4] 输出: false 解释: 无论怎样,你总会到达索引为 3 的位置.但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置. 思路: 如果只是判断能否跳到

LeetCode第四十五题-跳跃游戏

Jump Game II 问题简介:这是一个跳跃游戏,规则是通过给定一个非负整数数组,最开始就处于数组第一位即索引为0处,数组元素数值代表可以跳跃的最大值,要求通过最小的跳跃次数达到数组最后的索引处 举例: 输入: [2,3,1,1,4] 输出: 2 解释: 这个数组的最小跳跃值是2,第一次从索引0处跳跃1到索引1处,第二次跳跃3到结尾处 解法一: 当数组长度为1即无法跳跃,结果为0,找到每个点可以达到的最右端的值,进行判定 小白刷题之路,请多指教— — 要么大器晚成,要么石沉大海 原文地址:h

跳跃游戏

跳跃游戏 给定一个非负整数数组,假定你的初始位置为数组第一个下标. 数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出“true”,否则输出“false” 样例1 输入: 5 2 0 2 0 1 输出: true #include