Skip to content

Commit 0ea7caf

Browse files
反转字符串中的元音字母
1 parent b9bb493 commit 0ea7caf

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

345.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Descripttion: 反转字符串中的元音字母
3+
version: 1
4+
Author: Jason
5+
Date: 2020-11-23 10:16:45
6+
LastEditors: Jason
7+
LastEditTime: 2020-11-23 10:23:33
8+
'''
9+
10+
11+
class Solution:
12+
def reverseVowels(self, s: str) -> str:
13+
words = list(s)
14+
left = 0
15+
right = len(words) - 1
16+
vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
17+
while left < right:
18+
while left < right and words[left] not in vowels:
19+
left += 1
20+
21+
while left < right and words[right] not in vowels:
22+
right -= 1
23+
24+
if left < right:
25+
words[left], words[right] = words[right], words[left]
26+
left += 1
27+
right -= 1
28+
29+
return "".join(words)
30+
31+
32+
s = Solution()
33+
string = "leetcode"
34+
print(s.reverseVowels(string))

0 commit comments

Comments
 (0)