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

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

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

posted @ 2021-07-17 10:38  小Aer  阅读(1)  评论(0编辑  收藏  举报  来源