leetcode:(Python)Write a function that takes a string as input and reverse only the vowels of a string.
(需要注意的是题意为将“所有”元音字母倒序,并且原因字母要考虑到大小写)
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
其实是leetcode上面的一道题目
没有python版本的故来抛砖,经提交可通过
class Solution(object):
def reverseVowels(self, s):
string=list(s)
j=0
n=0
m=[]
k=[]
for i in s:
if i=='a'or i=='e' or i=='i' or i=='o' or i=='u' or i=='A'or i=='E' or i=='I' or i=='O' or i=='U':
m.append(j)
j=j+1
if len(m)>1:
for i in m:
k.append(s[i])
k.reverse()
for i in m:
string[i]=k[n]
n=n+1
string=''.join(string)
return string
本文提供了一种使用Python解决LeetCode上特定问题的方法:反转字符串中的元音字母。通过实例展示如何仅对元音进行倒序排列而不改变其他字符的位置。
392

被折叠的 条评论
为什么被折叠?



