看能否达到最后的条件是
- 能达到当前位置i
- nums[i]+i能跳到最后
所以用step记录当前还能跳几步,如果当前的步数nums[i]比step大,则更新当前的step
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
l1=len(nums)
if l1 <= 1:
return True
step = nums[0]
for i in range(1,l1):
if step + i>=l1:
return True
if step > 0:
step -= 1
step = max(step, nums[i])
else:
return False
return True

本文深入解析了一种判断数组中是否存在一种方案,使得从第一个位置开始,每次跳跃的长度不超过当前位置数值,最终能够到达最后一个位置的算法。通过动态更新可达最远距离的策略,实现了高效的解决方案。
1008

被折叠的 条评论
为什么被折叠?



