美文网首页
leetcode:125. Valid Palindrome

leetcode:125. Valid Palindrome

作者: 唐僧取经 | 来源:发表于2018-09-20 09:08 被阅读0次

125. Valid Palindrome

Description

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:

Input: "race a car"
Output: false

Answer

package main

import (
    "strings"
    "fmt"
    "unicode"
)

func isPalindrome(s string) bool {
    s = strings.ToLower(s)
    result := true
    for i, j := 0, len(s)-1; i < j; {
        if !unicode.IsLetter(rune(s[i])) && !unicode.IsDigit(rune(s[i])) {
            i++
            continue
        }

        if !unicode.IsLetter(rune(s[j])) && !unicode.IsDigit(rune(s[j])) {
            j--
            continue
        }

        if s[i] == s[j] {
            i++
            j--
            continue
        }

        result = false
        break
    }
    return result
}

func main() {
    fmt.Println(isPalindrome("A man, a plan, a canal: Panama"))
    fmt.Println(isPalindrome("aced race a ecar deac"))

}



相关文章

网友评论

      本文标题:leetcode:125. Valid Palindrome

      本文链接:https://www.haomeiwen.com/subject/hwlgnftx.html