LeetCode #55 Jump Game
题目
解题方法
这道题可以用贪心算法的思想解决,类似于Jump Game II。
代码
class Solution:
def canJump(self, nums: List[int]) -> bool:
curEnd = 0
curFarthest = 0
for i in range(len(nums)-1):
curFarthest = max(curFarthest, i + nums[i])
if i == curEnd:
curEnd = curFarthest
if curEnd >= len(nums) - 1:
break
if curEnd >= len(nums) - 1:
return True
else:
return False

浙公网安备 33010602011771号