版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412846
先过滤掉不合法字符,然后从两端向中间遍历判断是否相等
AC速度不是很理想
func isPalindrome(s string) bool {
pat := "[,:.@#--?\";!` ]"
re, _ := regexp.Compile(pat)
s = re.ReplaceAllString(s, "")
s = strings.ToLower(s)
if s == "" {
return true
}
j := len(s) - 1
for i := 0; i < len(s)/2; i++ {
if s[i] != s[j] {
return false
}
j--
}
return true
}