天天看点

LeetCode刷题——345. 反转字符串中的元音字母

题目

LeetCode刷题——345. 反转字符串中的元音字母

思路

代码

class Solution(object):
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        # print(type(s)) # <type 'unicode'>
        cs = 'aeiouAEIOU' # 要考虑大小写,元音字母的大写也写进去
        i,j = 0,len(s)-1
        s = list(s) # <type 'unicode'>转换为list
        while i<j:
            while i<j and s[i] not in cs:
                i += 1
            while i<j and s[j] not in cs:
                j -= 1
            
            s[i],s[j] = s[j],s[i]
            i += 1
            j -= 1
        
        return "".join(s) #list转换为字符串