「LeetCode每日一题」—— 55. 跳跃游戏
55. 跳跃游戏
链接:https://leetcode-cn.com/problems/jump-game/
难度:中等
题目
思路
这题很容易想到从后面开始往前面跳。如果倒数第一可以达到,那最后就可以达到。
其实我们可以换一种想法,如果某一个作为起跳点的格子可以跳跃的距离是3,那么表示后面3个格子都可以作为起跳点。我们可以对每一个能作为起跳点的格子都尝试跳一次,把能跳到最远的距离不断更新。如果可以一直跳到最后,就成功了。
为什么要保持跳到最远距离,这是因为我们认为,如果能跳到这个点,那其实左边的任何点其实都可以跳到。
代码见解决方案。
方案代码
解决方案:
class Solution:
def canJump(self, nums: List[int]) -> bool:
current = 0
for i in range(len(nums)):
if i > current:
return False
current = max(current, i+nums[i])
return True
相关
原创文章,作者:flypython,如若转载,请注明出处:http://flypython.com/algorithm/leetcode/333.html
相关推荐
-
「LeetCode每日一题」——820. 单词的压缩编码
820. 单词的压缩编码 链接:https://leetcode-cn.com/problems/short-encoding-of-words/难度:中等 题目 给定一个单词列表…
28/03/2020 -
「LeetCode每日一题」—— 22. 括号生成
22. 括号生成 链接:https://leetcode-cn.com/problems/generate-parentheses/难度:中等 题目 数字 n 代表生成括…
09/04/2020 -
「LeetCode每日一题」—— 1248. 统计「优美子数组」
1248. 统计「优美子数组」 链接:https://leetcode-cn.com/problems/count-number-of-nice-subarrays/难度:中等 题…
21/04/2020 -
「LeetCode每日一题」——LCCI 10.01. 合并排序的数组
LCCI 10.01. 合并排序的数组 链接:https://leetcode-cn.com/problems/sorted-merge-lcci/难度:简单 题目 给定两个排序后…
03/03/2020 -
「LeetCode每日一题」—— 289. 生命游戏
289. 生命游戏 链接:https://leetcode-cn.com/problems/game-of-life/难度:中等 题目 根据 百度百科 ,生命游…
02/04/2020 -
「LeetCode每日一题」—— 151. 翻转字符串里的单词
151. 翻转字符串里的单词 链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/难度:中等 题目 给定一个…
10/04/2020 -
「LeetCode每日一题」—— 355. 设计推特
355. 设计推特 链接:https://leetcode-cn.com/problems/design-twitter/难度:中等 题目 点击原文链接跳转查看题目 思路 思路见代…
13/04/2020 -
「LeetCode每日一题」——LCOF.59 – II. 队列的最大值
链接:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/难度:中等 题目 请定义一个队列并实现函数 max_v…
07/03/2020 -
「LeetCode每日一题」——695. 岛屿的最大面积
695. 岛屿的最大面积 链接:https://leetcode-cn.com/problems/max-area-of-island/难度:中等 题目 给定一个包含了一些 0 和…
15/03/2020 -
「LeetCode每日一题」—— 46. 全排列
46. 全排列 链接:https://leetcode-cn.com/problems/permutations/难度:中等 题目 点击原文链接跳转查看题目 思路 这题算是一个基础…
25/04/2020
您必须登录才能发表评论。