Skip to content

Commit 079089a

Browse files
authored
Merge pull request #1135 from 0xff-dev/917
Add solution and test-cases for problem 917
2 parents 57b3cf5 + 99dcf12 commit 079089a

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

leetcode/901-1000/0917.Reverse-Only-Letters/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [917.Reverse Only Letters][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a string `s`, reverse the string according to the following rules:
5+
6+
- All the characters that are not English letters remain in the same position.
7+
- All the English letters (lowercase or uppercase) should be reversed.
8+
9+
Return `s` after reversing it.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: s = "ab-cd"
15+
Output: "dc-ba"
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Reverse Only Letters
23-
```go
2420
```
21+
Input: s = "a-bC-dEf-ghIj"
22+
Output: "j-Ih-gfE-dCba"
23+
```
24+
25+
**Example 3:**
2526

27+
```
28+
Input: s = "Test1ng-Leet=code-Q!"
29+
Output: "Qedo1ct-eeLg=ntse-T!"
30+
```
2631

2732
## 结语
2833

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func ok(b byte) bool {
4+
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
5+
}
6+
7+
func Solution(s string) string {
8+
bs := []byte(s)
9+
l, r := 0, len(bs)-1
10+
for l < r {
11+
for ; l < r && !ok(bs[l]); l++ {
12+
13+
}
14+
for ; r > l && !ok(bs[r]); r-- {
15+
}
16+
bs[l], bs[r] = bs[r], bs[l]
17+
l, r = l+1, r-1
18+
}
19+
return string(bs)
520
}

leetcode/901-1000/0917.Reverse-Only-Letters/Solution_test.go

Lines changed: 7 additions & 7 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
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "ab-cd", "dc-ba"},
17+
{"TestCase2", "a-bC-dEf-ghIj", "j-Ih-gfE-dCba"},
18+
{"TestCase3", "Test1ng-Leet=code-Q!", "Qedo1ct-eeLg=ntse-T!"},
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)