mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-06-04 02:21:15 +08:00
routine
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package leetcode
|
||||
|
||||
func abs(x int) int {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func closestTarget(words []string, target string, startIndex int) int {
|
||||
ans := len(words)
|
||||
n := len(words)
|
||||
|
||||
for i, word := range words {
|
||||
if word == target {
|
||||
dist := abs(i - startIndex)
|
||||
ans = min(ans, min(dist, n-dist))
|
||||
}
|
||||
}
|
||||
|
||||
if ans < n {
|
||||
return ans
|
||||
}
|
||||
return -1
|
||||
}
|
||||
Reference in New Issue
Block a user