版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412961
先用map保存所有的元音字母,然后遍历交换
func reverseVowels(s string) string {
t := []byte(s)
m := make(map[byte]bool)
m['o'], m['e'], m['a'], m['i'], m['u'] = true, true, true, true, true
m['O'], m['E'], m['A'], m['I'], m['U'] = true, true, true, true, true
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
for i < j && !m[t[i]] {
i++
}
for j > i && !m[t[j]] {
j--
}
if i >= j {
break
}
t[i], t[j] = t[j], t[i]
}
return string(t)
}