天天看點

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轉換為字元串