345. 反转字符串中的元音字母

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"
示例 2:

输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string

class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels=['a','e','i','o','u','A','E','I','O','U']
        n=len(s)
        if not n:
            return ''
        s=list(s)
        i=0
        j=n-1
        while i<j:
            a=b=False
            if s[i] in vowels:a=True
            if s[j] in vowels:b=True
            if a and b:s[i],s[j]=s[j],s[i] 
            if not a and b:i+=1
            elif not b and a:j-=1
            else:
                i+=1
                j-=1
        return ''.join(s)

 

posted @ 2020-08-18 14:32  XXXSANS  阅读(174)  评论(0)    收藏  举报