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.
Example 1:
1 | Input: [2,3,1,1,4] |
Example 2:
1 | Input: [3,2,1,0,4] |
思路:我们可以跟踪每一次跳跃时能够跳到的最远距离。这个问题的关键在于:(1)什么时候当前的位置不能跳到下一个位置;(2)什么时候最大的跳跃距离可以跳到最后一个点。
最远能到的距离可以这样来表示 : i + A[i].
Here is an example:
代码如下:
1 | class Solution { |