leetcode 345 Reverse Vowels of a String

Java:

class Solution {
    public String reverseVowels(String s) {
        char[] rev = s.toCharArray();
        int l = s.length();
        String ae = "aeiouAEIOU" ;
        int str = 0;
        int end = l - 1;
        while(str < end){
            while(str < end && !ae.contains(rev[str]+"")){
                str++;
            }
            while(str < end && !ae.contains(rev[end]+"")){
                end--;
            }
            char temp = rev[str];
            rev[str] = rev[end];
            rev[end] = temp;
            
            str++;
            end--;
        }
        return new String(rev);
    }
}
Python:

class Solution:
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        #(?i)即匹配时不区分大小写。表示匹配时不区分大小写。
        vowels = re.findall('(?i)[aeiou]', s)
        #re.sub()替换
        return re.sub('(?i)[aeiou]', lambda m: vowels.pop(), s)

猜你喜欢

转载自blog.csdn.net/mrxjh/article/details/79802162