Skip to content

Commit ff9fbd5

Browse files
authored
Merge pull request #1188 from 0xff-dev/125
Add solution and test-cases for problem 125
2 parents 47de1d2 + 3b2ad85 commit ff9fbd5

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [125.Valid Palindrome][title]
2+
3+
## Description
4+
A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
5+
6+
Given a string `s`, return `true` if it is a **palindrome**, or `false` otherwise.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: s = "A man, a plan, a canal: Panama"
12+
Output: true
13+
Explanation: "amanaplanacanalpanama" is a palindrome.
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: s = "race a car"
20+
Output: false
21+
Explanation: "raceacar" is not a palindrome.
22+
```
23+
24+
**Example 3:**
25+
26+
```
27+
Input: s = " "
28+
Output: true
29+
Explanation: s is an empty string "" after removing non-alphanumeric characters.
30+
Since an empty string reads the same forward and backward, it is a palindrome.
31+
```
32+
33+
## 结语
34+
35+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
36+
37+
[title]: https://leetcode.com/problems/valid-palindrome
38+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) bool {
4+
var is func(byte) byte
5+
is = func(b byte) byte {
6+
if b >= '0' && b <= '9' {
7+
return b
8+
}
9+
if b >= 'a' && b <= 'z' {
10+
return b
11+
}
12+
if b >= 'A' && b <= 'Z' {
13+
return b + 32
14+
}
15+
return byte(' ')
16+
}
17+
l, r := 0, len(s)-1
18+
var bl, br byte
19+
for l < r {
20+
for ; l < r; l++ {
21+
bl = is(s[l])
22+
if bl != byte(' ') {
23+
break
24+
}
25+
}
26+
for ; l < r; r-- {
27+
br = is(s[r])
28+
if br != byte(' ') {
29+
break
30+
}
31+
}
32+
if l < r {
33+
if bl != br {
34+
return false
35+
}
36+
l, r = l+1, r-1
37+
}
38+
}
39+
return true
540
}

leetcode/101-200/0125.Valid-Palindrome/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "A man, a plan, a canal: Panama", true},
17+
{"TestCase2", "race a car", false},
18+
{"TestCase3", " ", true},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)