验证回⽂串

题目描述

验证它是否是回⽂串,只考虑字⺟和数字字符,可以忽略字⺟的⼤⼩写

解题思路

“回⽂串”是⼀个正读和反读都⼀样的字符串,⽐ 如“level”或者“noon”等等就是回⽂串

package program

import (
    "fmt"
    "strings"
    "testing"
)

func TestStr(t *testing.T) {
    s := "A man, a plan, a canal: Panama"
    fmt.Println(isPalindrome(s))
}

func isPalindrome(s string) bool {
    s = strings.ToLower(s)
    i := 0
    j := len(s) - 1
    for i < j {
        if !isValid(s[i]) {
            i++
        } else if !isValid(s[j]) {
            j--
        } else if s[i] != s[j] {
            return false
        } else {
            i++
            j--
        }
    }
    return true
}

func isValid(u uint8) bool {
    if (u >= 'a' && u <= 'z') || (u >= '0' && u <= '9') {
        return true
    }
    return false
}

results matching ""

    No results matching ""