Go语言ReverseVowelsofaString
主要是判断是不是元音字符,没啥难度,记得实际写的时候要去掉魔鬼数字
package main
import (
"fmt"
)
//注意考虑大小写
func isVowel(vowel rune) bool {
if vowel == 97 || vowel == 97-32 {
return true
}
if vowel == 101 || vowel == 101-32 {
return true
}
if vowel == 105 || vowel == 105-32 {
return true
}
if vowel == 111 || vowel == 111-32 {
return true
}
if vowel == 117 || vowel == 117-32 {
return true
}
return false
}
func reverseVowels(s string) string {
runeString := []rune(s)
flag := false
for from, to := 0, len(s)-1; from < to; from, to = from+1, to-1 {
for {
flag = isVowel(runeString[from])
if flag || from >= to{
break
}
from++
}
for {
flag = isVowel(runeString[to])
if flag || from >= to{
break
}
to--
}
runeString[from], runeString[to] = runeString[to], runeString[from]
}
return string(runeString)
}
func main(){
//ans := reverseVowels("helalo")
//ans := reverseVowels("hll")
//ans := reverseVowels("hello")
ans := reverseVowels("Aa")
fmt.Println(ans)
}
有疑问加站长微信联系(非本文作者)