Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff3ab2d

Browse files
committed
Merge pull request #338 from 0xff-dev/master
Add solution and test-cases for problem 345
2 parents 2109786 + 2a84cc3 commit ff3ab2d

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

‎leetcode/301-400/0345.Reverse-Vowels-of-a-String/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# [345.Reverse Vowels of a String][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 only all the vowels in the string and return it.
5+
6+
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
7+
78

89
**Example 1:**
910

1011
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
12+
Input: s = "hello"
13+
Output: "holle"
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Reverse Vowels of a String
23-
```go
2418
```
25-
19+
Input: s = "leetcode"
20+
Output: "leotcede"
21+
```
2622

2723
## 结语
2824

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(ss string) string {
4+
bs := []byte(ss)
5+
s, e := 0, len(bs)-1
6+
for s < e {
7+
shouldExchange := true
8+
if !isVowels(bs[s]) {
9+
s++
10+
shouldExchange = false
11+
}
12+
if !isVowels(bs[e]) {
13+
e--
14+
shouldExchange = false
15+
}
16+
if shouldExchange {
17+
bs[s], bs[e] = bs[e], bs[s]
18+
s, e = s+1, e-1
19+
}
20+
}
21+
return string(bs)
22+
}
23+
24+
func isVowels(b byte) bool {
25+
return b == 'A' || b == 'a' || b == 'e' || b == 'E' || b == 'i' || b == 'I' || b == 'o' || b == 'O' || b == 'u' || b == 'U'
526
}

‎leetcode/301-400/0345.Reverse-Vowels-of-a-String/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", "hello", "holle"},
17+
{"TestCase2", "leetcode", "leotcede"},
18+
{"TestCase3", "dff", "dff"},
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)

AltStyle によって変換されたページ (->オリジナル) /