Skip to content

Add solution and test-cases for problem 125 #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions leetcode/101-200/0125.Valid-Palindrome/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# [125.Valid Palindrome][title]

## Description
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.

Given a string `s`, return `true` if it is a **palindrome**, or `false` otherwise.

**Example 1:**

```
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
```

**Example 2:**

```
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
```

**Example 3:**

```
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/valid-palindrome
[me]: https://github.com/kylesliu/awesome-golang-algorithm
39 changes: 37 additions & 2 deletions leetcode/101-200/0125.Valid-Palindrome/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) bool {
var is func(byte) byte
is = func(b byte) byte {
if b >= '0' && b <= '9' {
return b
}
if b >= 'a' && b <= 'z' {
return b
}
if b >= 'A' && b <= 'Z' {
return b + 32
}
return byte(' ')
}
l, r := 0, len(s)-1
var bl, br byte
for l < r {
for ; l < r; l++ {
bl = is(s[l])
if bl != byte(' ') {
break
}
}
for ; l < r; r-- {
br = is(s[r])
if br != byte(' ') {
break
}
}
if l < r {
if bl != br {
return false
}
l, r = l+1, r-1
}
}
return true
}
12 changes: 6 additions & 6 deletions leetcode/101-200/0125.Valid-Palindrome/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "A man, a plan, a canal: Panama", true},
{"TestCase2", "race a car", false},
{"TestCase3", " ", true},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading