Skip to content

Latest commit

 

History

History
20 lines (20 loc) · 508 Bytes

55跳跃游戏.md

File metadata and controls

20 lines (20 loc) · 508 Bytes
func canJump(nums []int) bool {
    if len(nums) <= 1 {
        return true
    }
    var max int
    // 在能跳跃到的范围内,每一次都取最大的步数更新最大范围
    for i := 0; i <= max; i++ {
        if i + nums[i] > max {
            max = i + nums[i]
        }
        // 当最大范围大于等于结尾时,成功
        if max >= len(nums) - 1 {
            return true
        }
    }
    // 最大范围不足以跳到最后一个下标,失败
    return false
}