Skip to content

Commit 417baa3

Browse files
committed
chore: add daily leetcode post 345. 反转字符串中的元音字母_translated
1 parent 5fdc2cf commit 417baa3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: 345. Voice letter in the reverse string.md
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- - Python
6+
- - answer
7+
abbrlink: 1c57c22c
8+
---
9+
10+
# topic:
11+
12+
[345. Voice letter in the reverse string.md](https://leetcode.cn/problems/reverse-vowels-of-a-string/description/)
13+
14+
# Thought:
15+
Write two methods,Watch the reminder of the three leaves of the palace water,Can be made with dual pointers,See if both the elements of both ends are vowel letters。 Improved two editions。
16+
# Code:
17+
18+
```python Double pointer
19+
class Solution:
20+
def reverseVowels(self, s: str) -> str:
21+
vowels = 'aeiouAEIOU'
22+
start = 0
23+
end = len(s) - 1
24+
while start < end:
25+
while s[end] not in vowels and start < end:
26+
end -= 1
27+
while s[start] not in vowels and start < end:
28+
start += 1
29+
if s[start] in vowels and s[end] in vowels:
30+
s[start], s[end] = s[end], s[start]
31+
start += 1
32+
end -= 1
33+
return ''.join(s)
34+
```
35+
```python String operation
36+
class Solution:
37+
def reverseVowels(self, s: str) -> str:
38+
s = list(s)
39+
vowels = 'aeiouAEIOU'
40+
ans = []
41+
for i in s:
42+
if i in vowels:
43+
ans.append(i)
44+
a = ''
45+
for i in range(len(s)):
46+
if s[i] in vowels:
47+
a += ans.pop()
48+
else:
49+
a += s[i]
50+
return ''.join(a)
51+
```

0 commit comments

Comments
 (0)