leetcode (go语言) 验证回文串

package main

import (
    "fmt"
    "strings"
)

func isValidChar(s string) bool {
    return (s >= "a" && s <= "z") || (s >= "0" && s <= "9")
}

func isPalindrome(s string) bool {
    left := 0
    right := len(s) - 1
    s = strings.ToLower(s)

    isPlind := true
    for left < right {
        chLeft := s[left : left+1]
        chRight := s[right : right+1]
        if isValidChar(chLeft) && isValidChar(chRight) {
            if chLeft != chRight {
                isPlind = false
                break
            } else {
                left++
                right--
            }
        } else if !isValidChar(chLeft) {
            left++
        } else if !isValidChar(chRight) {
            right--
        }
    }

    return isPlind
}

func main() {
    fmt.Println(isPalindrome("race a car"))
}

程序输出如下,


image.png

你可能感兴趣的:(leetcode (go语言) 验证回文串)