Reverse Vowels of a String

利用基本和快排一样格式的双指针进行对换就可以了

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        stringList = list(s)
        n = len(stringList)
        start, end = 0, n - 1
        vowels = set(['a', 'e', 'i', 'o', 'u', 'A', 'E','I', 'O', 'U'])

        while start <= end:
            while start <= end and s[start] not in vowels:
                start += 1
            while start <= end and s[end] not in vowels:
                end -= 1
            if start <= end:
                stringList[start], stringList[end] = stringList[end], stringList[start]
                start += 1
                end -= 1

        return ''.join(stringList)

results matching ""

    No results matching ""