200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 162. Leetcode 45. 跳跃游戏 II (贪心算法-贪心区间)

162. Leetcode 45. 跳跃游戏 II (贪心算法-贪心区间)

时间:2021-03-15 12:36:38

相关推荐

162. Leetcode 45. 跳跃游戏 II (贪心算法-贪心区间)

class Solution:def jump(self, nums: List[int]) -> int:if len(nums) == 1:return 0# 记录走的最大步数、当前覆盖最远距离下标、下一步覆盖最远距离下标ans, curDistance, nextDistance = 0, 0, 0for i in range(len(nums)-1):# 更新下一步覆盖最远距离下标nextDistance = max(i + nums[i], nextDistance)if i == curDistance: # 遇到当前覆盖最远距离下标curDistance = nextDistance # 更新当前覆盖最远距离下标ans += 1 # 下一步return ans

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。