Skip to content

Add solution and test-cases for problem 917 #1135

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

Merged
merged 1 commit into from
Mar 7, 2025
Merged
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
31 changes: 18 additions & 13 deletions leetcode/901-1000/0917.Reverse-Only-Letters/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# [917.Reverse Only Letters][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given a string `s`, reverse the string according to the following rules:

- All the characters that are not English letters remain in the same position.
- All the English letters (lowercase or uppercase) should be reversed.

Return `s` after reversing it.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "ab-cd"
Output: "dc-ba"
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Reverse Only Letters
```go
```
Input: s = "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
```

**Example 3:**

```
Input: s = "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
```

## 结语

Expand Down
19 changes: 17 additions & 2 deletions leetcode/901-1000/0917.Reverse-Only-Letters/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package Solution

func Solution(x bool) bool {
return x
func ok(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
}

func Solution(s string) string {
bs := []byte(s)
l, r := 0, len(bs)-1
for l < r {
for ; l < r && !ok(bs[l]); l++ {

}
for ; r > l && !ok(bs[r]); r-- {
}
bs[l], bs[r] = bs[r], bs[l]
l, r = l+1, r-1
}
return string(bs)
}
14 changes: 7 additions & 7 deletions leetcode/901-1000/0917.Reverse-Only-Letters/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
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "ab-cd", "dc-ba"},
{"TestCase2", "a-bC-dEf-ghIj", "j-Ih-gfE-dCba"},
{"TestCase3", "Test1ng-Leet=code-Q!", "Qedo1ct-eeLg=ntse-T!"},
}

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

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

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