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.
給一非負整數陣列,你一開始在陣列起始的位置。
陣列中的每個元素,代表你在這一格最遠可以跳多遠。
你的目標是跳到陣列的最後一格,請找出你最少只需要跳幾次。
i
格,代表你有能力可以到0~i
的任何一格。i
格,你下次就可以到i+nums[i]
格。利用這點去不停更新最遠距離。last
去紀錄上一次更新到哪裡。LeetCode
C++