题目
思路
做这个题需要先知道aeiou
就是元音字母。和反转字符串类似,元音字母才需要反转。
代码
class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
# print(type(s)) # <type 'unicode'>
cs = 'aeiouAEIOU' # 要考虑大小写,元音字母的大写也写进去
i,j = 0,len(s)-1
s = list(s) # <type 'unicode'>转换为list
while i<j:
while i<j and s[i] not in cs:
i += 1
while i<j and s[j] not in cs:
j -= 1
s[i],s[j] = s[j],s[i]
i += 1
j -= 1
return "".join(s) #list转换为字符串