Leetcode题55、跳跃游戏(Python题解)华为面试题

it2026-04-01  4

同类问题: 跳跃游戏II 划分字母区间 视频拼接

问题:

题目来源:力扣(LeetCode)

leetcode55.跳跃游戏

难度:中等

分析: 贪心算法来做 以样例[2, 3, 1, 1, 4]为例,第一步可以跳2步,在两步范围内寻找下一步能跳的最远的位置,为第二个数3,然后站在第二个数的位置寻找在3步范围内,下一步跳得最远的位置,可以直接跳到最末端,则可达到最后一个位置,返回True。

解决方法: 1:

#贪心 class Solution: def canJump(self, nums: List[int]) -> bool: n, rightmost = len(nums), 0 for i in range(n): if i <= rightmost: rightmost = max(rightmost, i + nums[i]) if rightmost >= n - 1: return True return False
最新回复(0)