leetcode,jump game 思考

代码基于贪心算法进行,看起来很简单。

def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        end = 0
        for i in range(len(nums)):
            if end < i:                  #如果这个移动距离比坐标的下标还小,
                                         #则说明这个位置已经不可达
                return False
            end = max(end, i + nums[i])  #求得在第i处的最大移动距离。
        return True

主要是end < i,稍微要绕一点弯子。

你可能感兴趣的:(刷题)