描述
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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
分析
Jump Game的后继问题,这次要求得出最小跳跃步数,我们用两个变量来分别记录现在到达的位置
和下一次最大能到达的未知
,遍历数组,如果下标超出现在到达的位置
,则说明需要跳一步。比较巧妙,需要仔细推敲。
代码
Python
1 | class Solution(object): |