Leetcode Note: Go - Minimum Distance to the Target Element

Minimum Distance to the Target Element - LeetCode
https://leetcode.com/problems/minimum-distance-to-the-target-element/

  • Go 言語で取り組んだメモ

回答

[Go] simple solution - Minimum Distance to the Target Element - LeetCode
https://leetcode.com/problems/minimum-distance-to-the-target-element/solutions/1187614/go-simple-solution/

func getMinDistance(nums []int, target int, start int) int {
    ans := 100000
    for i, n := range(nums) {
        if n == target {
            diff := i - start
            if diff < 0 {
                diff = -diff
            }
            if diff < ans {
                ans = diff
            }
        }
    }
    return ans
}