您的位置:首页 > 编程语言 > Python开发

[LeetCode] Reverse Vowels of a String Python 题解

2016-08-10 06:23 393 查看
Reverse Vowels of a String

"""

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".

"""

class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
ll = list(s)
i = 0
j = len(s) - 1
vl = ['a','e','i', 'o','u', 'A','E','I','O','U']
while i < j:
if ll[i] in vl and ll[j] in vl:
ll[i], ll[j] = ll[j], ll[i]
j = j - 1
i = i + 1
elif ll[i] in vl:
j = j - 1
else:
i = i + 1
return ''.join(ll)

s = Solution()

print s.reverseVowels("leetcode")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: