「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每日一题」——300. 最长上升子序列
300. 最长上升子序列 链接:https://leetcode-cn.com/problems/longest-increasing-subsequence/难度:中等 题目 给…
14/03/2020 -
「LeetCode每日一题」——322. 零钱兑换
322. 零钱兑换 链接:https://leetcode-cn.com/problems/coin-change/难度:中等 题目 给定不同面额的硬币 coins 和一个总金额 …
08/03/2020 -
「LeetCode每日一题」—— 1248. 统计「优美子数组」
1248. 统计「优美子数组」 链接:https://leetcode-cn.com/problems/count-number-of-nice-subarrays/难度:中等 题…
21/04/2020 -
「LeetCode每日一题」225. 用队列实现栈
LeetCode每日一题 周五跟大家预告了LeetCode每日一题的活动,今天活动已经开始了。打开leetcode中文版,你可以在题库中看到制定的题目,行动起来吧。 在这里帖下打卡…
01/03/2020 -
「LeetCode每日一题」——892. 三维形体的表面积
892. 三维形体的表面积 链接:https://leetcode-cn.com/problems/surface-area-of-3d-shapes/难度:简单 题目 在&nbs…
25/03/2020 -
「LeetCode每日一题」—— LCCI.16.03.交点
LCCI.16.03.交点 链接:https://leetcode-cn.com/problems/intersection-lcci/难度:困难 题目 点击原文链接跳转查看题目 …
12/04/2020 -
「LeetCode每日一题」—— 202. 快乐数
202. 快乐数 链接:https://leetcode-cn.com/problems/happy-number/难度:简单 题目 点击原文链接跳转查看题目 思路 今天是四月最后…
30/04/2020 -
「LeetCode每日一题」—— 912. 排序数组
912. 排序数组 链接:https://leetcode-cn.com/problems/sort-an-array/难度:中等 题目 给你一个整数数组 nums,请你…
31/03/2020 -
「LeetCode每日一题」—— 22. 括号生成
22. 括号生成 链接:https://leetcode-cn.com/problems/generate-parentheses/难度:中等 题目 数字 n 代表生成括…
09/04/2020 -
「LeetCode每日一题」—— 542. 01 矩阵
542. 01 矩阵 链接:https://leetcode-cn.com/problems/01-matrix/难度:中等 题目 点击原文链接跳转查看题目 思路 这道题求的就是每…
15/04/2020
您必须登录才能发表评论。