mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
33 lines
444 B
Go
33 lines
444 B
Go
package leetcode
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
func isPalindrome(s string) bool {
|
|
s = strings.ToLower(s)
|
|
i, j := 0, len(s)-1
|
|
for i < j {
|
|
for i < j && !isChar(s[i]) {
|
|
i++
|
|
}
|
|
for i < j && !isChar(s[j]) {
|
|
j--
|
|
}
|
|
if s[i] != s[j] {
|
|
return false
|
|
}
|
|
i++
|
|
j--
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 判断 c 是否是字符或者数字
|
|
func isChar(c byte) bool {
|
|
if ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') {
|
|
return true
|
|
}
|
|
return false
|
|
}
|