[LeetCode]题解(python):055-Jump Game

时间:2023-12-14 20:02:50

题目来源


https://leetcode.com/problems/jump-game/

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.


题意分析


Input: a list

Output: True or False

Conditions:每一个位表示能向前几位,看能不能到达终点


题目思路


记录每一个位置能最多向前多少,然后不断更新这个最大值,如果能一直循环到最后则说明可以到达


AC代码(Python)


 __author__ = 'YE'

 class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
step = nums[]
for i in range(, len(nums)):
if step > :
step -=
step = max(step, nums[i])
else:
return False
return True nums = [,,,,]
nums1 = [,,,,]
print(Solution().canJump(nums1))