原题
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.
思路
设定一个变量reachable记录当前能够达到的最远值。然后遍历nums:
当reachable<i时,即已经不能到达该处。返回False。
否则更新reachable,大小为当前能走的最远值以及此处可以走的最远值之间的最大值。
接着判断reachable是否大于nums,若是即可提前返回True跳出。
代码
1 | class Solution: |