题目描述:
LeetCode 345. Reverse Vowels of a String
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".
题目大意:
编写函数输入一个字符串,将其中的元音字母逆置。
测试用例如题目描述。
解题思路:
双指针法(Two Pointers)
Python代码:
class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        VOWELS = ('a', 'e', 'i', 'o', 'u')
        size = len(s)
        left, right = 0, size - 1
        ls = list(s)
        while True:
            while left < size and s[left].lower() not in VOWELS:
                left += 1
            while right >= 0 and s[right].lower() not in VOWELS:
                right -= 1
            if left >= right: break
            ls[left], ls[right] = ls[right], ls[left]
            left, right = left + 1, right - 1
        return ''.join(ls)
精简版Python代码,详见LeetCode Discuss:https://leetcode.com/discuss/99073/1-2-lines-python-ruby
def reverseVowels(self, s):
    vowels = re.findall('(?i)[aeiou]', s)
    return re.sub('(?i)[aeiou]', lambda m: vowels.pop(), s)
 本文链接:http://bookshadow.com/weblog/2016/04/23/leetcode-reverse-vowels-string/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。
 
			  
正则没有看明白...
(?i)是什么意思,让后面的枚举忽略大小写吗
是的,意思是大小写不敏感:case-insensitive