反转字符串中的元音字母

给你一个字符串 s ,仅反转字符串中的所有元音字母,并返回结果字符串。
元音字母包括 'a'、'e'、'i'、'o'、'u',且可能以大小写两种形式出现不止一次。
输入:s = "hello" 输出:"holle"
public static String reverseVowels(String s){  
    int n=s.length();  
    char[] arr=s.toCharArray();  
    int i=0,j=n-1;  
    while (i < j){  
        while (i<n && !isVowel(arr[i])){  
            ++i;  
        }  
        while (j>0 && !isVowel(arr[j])){  
            --j;  
        }  
        if(i<j){  
            swap(arr,i,j);  
            ++i;  
            --j;  
        }  
    }  
    return new String(arr);  
}
private static boolean isVowel(char c) {  
    return "aeiouAEIOU".indexOf(c)>=0;  
}  
public static void swap(char[] arr,int i,int j){  
    char temp=arr[i];  
    arr[i]=arr[j];  
    arr[j]=temp;  
}

阿芯爱编程
0 声望1 粉丝

php ,java,nodejs