Description:
You are given an integer array nums
. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true
if you can reach the last index, or false
otherwise.
Examples
Example 1
|
|
Example 2
|
|
Thoughts
- Use a maximum variable to store the farthest index I can reach currently.
- Traverse nums and check if $maximum ≧ i$. Indicate whether the jump method can reach the current index.
解法思路
- 用一個maximum存目前可以到達最遠的idx
- 遍歷nums, 檢查maximum是否大於等於i, 表示jump的方法是否可以到達目前的index.
Solution
|
|
O(n)Runtime beats 89.98%, memory usage beats 94.33%