编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
//元音字母:a e i o u
class Solution {
public String reverseVowels(String s) {
char[] chr = s.toCharArray();
int left = 0;
int right = chr.length-1;
String s1 = "aeiouAEIOU";
while(left <right){
while(left < right && !s1.contains(chr[left]+"")){
left ++;
}
while(left < right && !s1.contains(chr[right]+"")){
right--;
}
if(left > right){
break;
}
char tmp = chr[left];
chr[left] = chr[right];
chr[right] = tmp;
left++;
right--;
}
return String.valueOf(chr);
}
}
class Solution {
public String reverseVowels(String s) {
char[] chr = s.toCharArray();
int left = 0;
int right = chr.length-1;
while(left <right){
while(left<right &&chr[left] != 'a' && chr[left] != 'e' && chr[left] != 'i' && chr[left] != 'o' && chr[left] != 'u' && chr[left] != 'A' && chr[left] != 'E' && chr[left] != 'I' && chr[left] != 'O' && chr[left] != 'U'){
left ++;
}
while(left<right &&chr[right] != 'a' && chr[right] != 'e' && chr[right] != 'i' && chr[right] != 'o' && chr[right] != 'u'&&chr[right] != 'A' && chr[right] != 'E' && chr[right] != 'I' && chr[right] != 'O' && chr[right] != 'U'){
right --;
}
if(left > right){
break;
}
char tmp = chr[left];
chr[left] = chr[right];
chr[right] = tmp;
left++;
right--;
}
return String.valueOf(chr);
}
}