作者:
Rushia (みけねこ的鼻屎)
2023-02-08 09:32:0545. Jump Game II
給你一個陣列nums,nums[i]表示從這個點可以往後移動幾格,求出從第0格開始,最少
跳幾格可以跳到陣列尾端(題目保證一定可以到陣列尾端)。
Example:
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: 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.
Input: nums = [2,3,0,1,4]
Output: 2
思路:
1.如果當前走最遠的地方(curr)到不了i,讓curr等於下一次的最遠距離(next)且次數+1。
2.否則不斷更新下次可以走的最遠距離。
3.最後返回次數即可。
JavaCode: