题目
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明: 本题中,我们将空字符串定义为有效的回文串。
思路: 回文串同样是使用双指针的思路,只需要每个start等于end就是回文串。但是首先需要对字符串进行预处理,将非数字非字母的字节去除,然后依次遍历start++ end–就行了
代码如下:
func isPalindrome(s string) bool {
start := 0
end := len(s) - 1
s = strings.ToLower(s)
for start < end{
if !dec(s[start]) {
start++
continue
}
if !dec(s[end]) {
end--
continue
}
if s[start] != s[end]{
return false
}
start++
end--
}
return true
}
func dec(b byte) bool {
return b >= 'a' && b <= 'z' || b >= '0' && b <= '9'
}