Skip to content

Commit 4435fac

Browse files
authored
feat: add rust solution to lc problem: No.0345 (doocs#1581)
No.0345. Reverse Vowels of a String
1 parent 78fdf2f commit 4435fac

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

solution/0300-0399/0345.Reverse Vowels of a String/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ function reverseVowels(s: string): string {
175175
}
176176
```
177177

178+
### **Rust**
179+
180+
```rust
181+
impl Solution {
182+
pub fn reverse_vowels(s: String) -> String {
183+
let vowel = String::from("aeiouAEIOU");
184+
let mut data: Vec<char> = s.chars().collect();
185+
let (mut i, mut j) = (0, s.len() - 1);
186+
while i < j {
187+
while i < j && !vowel.contains(data[i]) {
188+
i += 1;
189+
}
190+
while i < j && !vowel.contains(data[j]) {
191+
j -= 1;
192+
}
193+
if i < j {
194+
data.swap(i, j);
195+
i += 1;
196+
j -= 1;
197+
}
198+
}
199+
data.iter().collect()
200+
}
201+
}
202+
```
203+
178204
### **...**
179205

180206
```

solution/0300-0399/0345.Reverse Vowels of a String/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,32 @@ function reverseVowels(s: string): string {
151151
}
152152
```
153153

154+
### **Rust**
155+
156+
```rust
157+
impl Solution {
158+
pub fn reverse_vowels(s: String) -> String {
159+
let vowel = String::from("aeiouAEIOU");
160+
let mut data: Vec<char> = s.chars().collect();
161+
let (mut i, mut j) = (0, s.len() - 1);
162+
while i < j {
163+
while i < j && !vowel.contains(data[i]) {
164+
i += 1;
165+
}
166+
while i < j && !vowel.contains(data[j]) {
167+
j -= 1;
168+
}
169+
if i < j {
170+
data.swap(i, j);
171+
i += 1;
172+
j -= 1;
173+
}
174+
}
175+
data.iter().collect()
176+
}
177+
}
178+
```
179+
154180
### **...**
155181

156182
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn reverse_vowels(s: String) -> String {
3+
let vowel = String::from("aeiouAEIOU");
4+
let mut data: Vec<char> = s.chars().collect();
5+
let (mut i, mut j) = (0, s.len() - 1);
6+
while i < j {
7+
while i < j && !vowel.contains(data[i]) {
8+
i += 1;
9+
}
10+
while i < j && !vowel.contains(data[j]) {
11+
j -= 1;
12+
}
13+
if i < j {
14+
data.swap(i, j);
15+
i += 1;
16+
j -= 1;
17+
}
18+
}
19+
data.iter().collect()
20+
}
21+
}

0 commit comments

Comments
 (0)