1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| // @Title: 验证回文串 (Valid Palindrome)
// @Author: 15816537946@163.com
// @Date: 2022-03-26 15:00:06
// @Runtime: 0 ms
// @Memory: 2.7 MB
func isPalindrome(s string) bool {
s = strings.ToLower(s)
lo, hi := 0, len(s)-1
for lo < hi {
for lo < hi && !isAlphaNum(s[lo]) {
lo++
}
for lo < hi && !isAlphaNum(s[hi]) {
hi--
}
if s[lo] != s[hi] {
return false
}
lo++
hi--
}
return true
}
func isAlphaNum(b byte) bool {
if b >= 'a' && b <= 'z' || b >= '0' && b <= '9' {
return true
}
return false
}
|