mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-06-04 02:21:15 +08:00
21 lines
274 B
Go
21 lines
274 B
Go
package leetcode
|
|
|
|
import "math"
|
|
|
|
func abs(x int) int {
|
|
if x < 0 {
|
|
return -x
|
|
}
|
|
return x
|
|
}
|
|
|
|
func getMinDistance(nums []int, target int, start int) int {
|
|
ans := math.MaxInt
|
|
for i, v := range nums {
|
|
if v == target {
|
|
ans = min(abs(i-start), ans)
|
|
}
|
|
}
|
|
return ans
|
|
}
|