#coding=utf- def reverseVowels(s):
"""
:type s: str
:rtype: str
"""
sStr = list(s)
voList = {'a':, 'A':, 'e':, "E":, 'i':, "I":, 'o':, 'O':, 'u':, 'U':}
front =
length = len(sStr)
back = length -
while front < back:
while front < length and sStr[front] not in voList:
front +=
while back >= and sStr[back] not in voList:
back -=
if front < back:
sStr[front], sStr[back] = sStr[back], sStr[front]
front +=
back -=
return "".join(sStr) str="hello world!"
print(reverseVowels(str))
输出
hollo werld!
或
#coding=utf- def reverseVowels(s):
"""
:type s: str
:rtype: str
"""
sStr = list(s)
voList = {'a':, 'A':, 'e':, "E":, 'i':, "I":, 'o':, 'O':, 'u':, 'U':}
front =
length = len(sStr)
back = length -
while():
while front < length and sStr[front] not in voList:
front +=
while back >= and sStr[back] not in voList:
back -=
if front < back:
sStr[front], sStr[back] = sStr[back], sStr[front]
front +=
back -=
else:
break
return "".join(sStr) str="hello world!"
print(reverseVowels(str))
参考: