思路
先过滤掉不合法字符,然后从两端向中间遍历判断是否相等
AC速度不是很理想
code
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
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode