1. 程式人生 > >驗證迴文字串 go語言

驗證迴文字串 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
}