leetcode 345. Reverse Vowels of a String 反转字符串中的元音字母 python re.findall()、 re.sub() 、''.join()

class Solution:
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        # 相当于一个有限制的字符串逆序,只对字符串中的元音字母进行逆序。

        # Approach #1
        # vowels = ('a','e','i','o','u','A','E','I','O','U')
        # ss = [i for i in s if i in vowels]
        # return ''.join([ss.pop() if i in vowels else i for i in list(s)  ])


        # Approach #2
        import re
        ss = re.findall('[aeiouAEIOU]',s)
        return re.sub('[aeiouAEIOU]',lambda m: ss.pop() ,s)


        # Approach #3
        # import re
        # return re.sub('(?i)[aeiou]', lambda m, v=re.findall('(?i)[aeiou]', s): v.pop(), s)

猜你喜欢

转载自blog.csdn.net/huhehaotechangsha/article/details/80903837