leetcode-55 跳跃游戏 Python
2020-03-15 12:14
666 查看
方法一:逆序遍历
class Solution: def canJump(self, nums: List[int]) -> bool: last = len(nums)-1 for i in range(len(nums)-2,-1,-1): if nums[i] >= last-i: last = i if last == 0: return True return False``` 方法二:尽可能到达最远位置(贪心算法) ```python class Solution: def canJump(self, nums: List[int]) -> bool: max_i = 0 #初始化当前能到达最远的位置 for i, jump in enumerate(nums): #i为当前位置,jump是当前位置的跳数 if max_i>=i and i+jump>max_i: #如果当前位置能到达,并且当前位置+跳数>最远位置 max_i = i+jump #更新最远能到达位置 return max_i>=i
方法三:记录所有能到达的位置
class Solution: def canJump(self, nums: List[int]) -> bool: k = 0 for i in range(len(nums)): if i > k: return False k = max(k, i + nums[i]) return True
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- LeetCode 55题 跳跃游戏算法介绍 Python3
- leetcode-跳跃游戏-贪心算法(python)
- Leetcode做题日记:55. 跳跃游戏(PYTHON)
- Leetcode 55 跳跃游戏 【贪心算法】
- 跳跃游戏I---leetcode55
- leetcode-跳跃游戏II-贪心算法(python)
- LeetCode 55. 跳跃游戏 Python
- Leetcode 055 跳跃游戏 python
- Leetcode做题日记:45. 跳跃游戏 II(PYTHON)
- LeetCode-55-跳跃游戏
- 算法学习——leetcode45. 跳跃游戏 II
- leetcode。45. 跳跃游戏 II
- [ LeetCode ] #45. Jump Game II( 跳跃比赛II ) C++ & Python
- [LeetCode] Jump Game 跳跃游戏
- leetcode55. 跳跃游戏
- leetcode 第877题 石子游戏 python解法(用时420ms)
- LeetCode 55. Jump Game 跳跃游戏
- 为你的 Python 平台类游戏添加跳跃功能
- Leetcode 45. 跳跃游戏 II
- leetcode菜狗入门 | 55. 跳跃游戏