-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path125.验证回文串.go
92 lines (88 loc) · 1.52 KB
/
125.验证回文串.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* @lc app=leetcode.cn id=125 lang=golang
*
* [125] 验证回文串
*
* https://leetcode-cn.com/problems/valid-palindrome/description/
*
* algorithms
* Easy (39.55%)
* Likes: 83
* Dislikes: 0
* Total Accepted: 35.8K
* Total Submissions: 90.5K
* Testcase Example: '"A man, a plan, a canal: Panama"'
*
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
*
* 说明:本题中,我们将空字符串定义为有效的回文串。
*
* 示例 1:
*
* 输入: "A man, a plan, a canal: Panama"
* 输出: true
*
*
* 示例 2:
*
* 输入: "race a car"
* 输出: false
*
*
*/
func isPalindrome(s string) bool {
if len(s) == 0 {
return true
}
for i, j := 0, len(s)-1; i < j; {
if isSkip(s[i]) {
i++
continue
}
if isSkip(s[j]) {
j--
continue
}
if isEqual(s[i], s[j]) {
i++
j--
continue
} else {
return false
}
}
return true
}
func isSkip(b byte) bool {
if b >= 'a' && b <= 'z' {
return false
}
if b >= 'A' && b <= 'Z' {
return false
}
if b >= '0' && b <= '9' {
return false
}
return true
}
func isEqual(a, b byte) bool {
if a == b {
return true
}
if isLowLetter(a) && isUpperLetter(b) {
return a-('a'-'A') == b
}
if isLowLetter(b) && isUpperLetter(a) {
return b-('a'-'A') == a
}
return false
}
func isLowLetter(b byte) bool {
return b >= 'a' && b <= 'z'
}
func isUpperLetter(b byte) bool {
return b >= 'A' && b <= 'Z'
}
func isDigit(b byte) bool {
return b >= '0' && b <= '9'
}