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".
Subscribe to see which companies asked this question
这个也是很简单,不过要注意元音字母还包括大写字母。。。
class Solution(object):
def reverseVowels(self, s):
v = ['a','e','i','o','u','A','E','I','O','U']
vovs = []
s = list(s)
for i in xrange(len(s)):
if s[i] in v:
vovs += s[i],
s[i] = None
#vovs.reverse()
#print vovs,s
for i in xrange(len(s)):
if s[i] == None:
s[i] = vovs.pop()
#print s
return ''.join(s)

本文介绍了一个简单的Python函数,该函数接收一个字符串作为输入,并只反转字符串中的元音字母,保留辅音字母的位置不变。通过两个实例进行演示:'hello'变为'holle','leetcode'变为'leotcede'。

121

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



