题目描述
https://leetcode-cn.com/problems/jump-game-ii/
解
package main
func jump(nums []int) int {
if len(nums) <= 1 {
return 0
}
var r, i = 0, 0
var maxIndex, maxResource int
for i < len(nums) {
v := nums[i]
r++
if i+v >= len(nums)-1 {
return r
}
maxResource = 0
maxIndex = 0
for j := i + 1; j <= i+v; j++ {
// 找到最大的index
if j+nums[j] > maxResource {
maxResource = j + nums[j]
maxIndex = j
}
}
if maxIndex == 0 || maxResource == 0 {
return 0
}
i = maxIndex
}
return 0
}
思路
每次往最大的位置调,这提是hard模式,中间注意边界条件!
网友评论