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"))
}

程序输出如下,


8982195-f01891d1a54b5afd.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_33836874/article/details/91031577