leetcode刷题笔记(Golang)--125. Valid Palindrome

125. Valid Palindrome

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

func isPalindrome(s string) bool {
	if len(s) <= 1 {
		return true
	}
	l := 0
	r := len(s) - 1
	for l < r {
		for l < r && (!unicode.IsLetter(rune(s[l])) && !unicode.IsNumber(rune(s[l]))) {
			l++
		}
		for r > l && (!unicode.IsLetter(rune(s[r])) && !unicode.IsNumber(rune(s[r]))) {
			r--
		}
		fmt.Println(string(s[l]), string(s[r]), unicode.IsLetter(rune(s[l])))
		if unicode.ToLower(rune(s[l])) != unicode.ToLower(rune(s[r])) {
			return false
		}
		l++
		r--
	}
	return true
}
发布了98 篇原创文章 · 获赞 0 · 访问量 1465

猜你喜欢

转载自blog.csdn.net/weixin_44555304/article/details/104386408