美文网首页
Reverse Vowels of a String

Reverse Vowels of a String

作者: 再见小浣熊 | 来源:发表于2016-06-23 21:09 被阅读0次

    Difficulty: Easy

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

        def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        vowels = ['a', 'e', 'i', 'o', 'u']
        s = list(s)
        begin = 0
        end = len(s) - 1
        while begin < end:
            while begin < end and s[begin].lower() not in vowels:
                begin += 1
            while begin < end and s[end].lower() not in vowels:
                end -= 1
            s[begin], s[end] = s[end], s[begin]
            begin += 1
            end -= 1
                
        return ''.join(s)

    相关文章

      网友评论

          本文标题:Reverse Vowels of a String

          本文链接:https://www.haomeiwen.com/subject/dqkodttx.html