mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
22 lines
329 B
Go
22 lines
329 B
Go
package leetcode
|
|
|
|
func jump(nums []int) int {
|
|
if len(nums) == 1 {
|
|
return 0
|
|
}
|
|
needChoose, canReach, step := 0, 0, 0
|
|
for i, x := range nums {
|
|
if i+x > canReach {
|
|
canReach = i + x
|
|
if canReach >= len(nums)-1 {
|
|
return step + 1
|
|
}
|
|
}
|
|
if i == needChoose {
|
|
needChoose = canReach
|
|
step++
|
|
}
|
|
}
|
|
return step
|
|
}
|