每日一题 【每日一题】反转一个字符串中的元音字母-Python-20211115

Jack · 2021年11月16日 · 最后由 Jack 回复于 2021年11月17日 · 56 次阅读
本帖已被设为精华帖!

写一个方法,反转字符串中的元音字符

Jack 将本帖设为了精华贴 11月16日 22:17

参考代码:

class Solution:
    def reverseVowels(self, s):
        vowels = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"])
        res = list(s)
        start, end = 0, len(res) = -1
        while start <= end:
            while start <= end and res[start] not in vowels:
                start += 1
            while start <= end and res[end] not in vowels:
                end -= 1
            if start <= end:
                res[start], res[end] = res[end], res[start]
                start += 1
                end -= 1
        return "".join(res)

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册