题目:Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
给一个 字符串 ,判断是否是回文
思路
先过滤掉不合法字符,然后从两端向中间遍历判断是否相等
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: