📜  golang getTimes hackerrank - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:26.105000             🧑  作者: Mango

代码示例1
function getTimes() int64 {
    
   arr := make(map[string]int)
    for i := 1; i <= 26; i++ {
        str := 64 + i
        arr[string(str)] = i - 1

    }
    a := float64(arr["A"])
    b := float64(arr[string(s[0])])

    dist := math.Min(math.Abs(a-b), 26-math.Abs(a-b))

    for i := 1; i < len(s); i++ {
        compareA := float64(arr[string(s[i])])
        compareB := float64(arr[string(s[i-1])])

        if math.Abs(compareA-compareB) > 13 {
            dist += 26 - math.Abs(compareA-compareB)
        } else {
            dist += math.Abs(compareA - compareB)
        }

    }
    return int64(dist)

}