Problem
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 |
|
Example 2:
1 |
|
Explanation
- First we create a variable
max = 0
to record the maximum position we can reach. Then we iterate the array, updatemax = max(max, i+nums[i])
, ifmax >= nums[len(nums)-1]
, we returntrue
. Else ifi > max
, we break out the loop and returnfalse
.
Solution
1 |
|