题解 | 跳跃游戏(一)

跳跃游戏(一)

https://www.nowcoder.com/practice/23407eccb76447038d7c0f568370c1bd

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param nums int整型一维数组 
# @return bool布尔型
#
class Solution:
    def canJump(self , nums: List[int]) -> bool:
        # write code here
        n, maxl = len(nums), -1#数组长度、最大可跳跃下标
        for i in range(n):#遍历整个数组
            maxl = max(maxl,i+nums[i])#更新最大可跳跃下标
            if maxl>=n-1:#如果当前可以跳跃到最后一个位置,则返回true
                return True
            if maxl<=i:#如果当前最大可跳跃下标不超过当前下标,则无法跳跃到最后
                return False

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务