验证回文字符串 go语言

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

示例 1:

输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:

输入: "race a car"
输出: false
func isPalindrome(s string) bool {
    t := strings.ToLower(s)
    x := 0
    y := len(t) - 1
    for ; x < y; {
        if !(t[x] > 47 && t[x] < 58 || t[x] > 96 && t[x] < 123) {
	x++
        } else if !(t[y] > 47 && t[y] < 58 || t[y] > 96 && t[y] < 123 ){
	y--
        } else if t[x] != t[y] {
	return false
        } else {
	x++
	y--
        }
    }

    return true
}

猜你喜欢

转载自blog.csdn.net/s15738841819/article/details/83998962