Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Input: "hello"
Output: "holle"
Example 2:
Input: "leetcode"
Output: "leotcede"
Note:
The vowels does not include the letter "y".
注:
vowels:元音字母
代码:
class Solution {
public String reverseVowels(String s) {
int l=0,r=s.length()-1;
char lChar,rChar;
char[]arry=s.toCharArray();
while(l<r){
lChar = arry[l];
rChar=arry[r];
if(!isVowels(lChar)){
l++;
}else if(!isVowels(rChar)){
r--;
}else{
swap(arry,l++,r--);
}
}
return new String(arry);
}
private void swap(char[]arry,int l,int r){
char temp=arry[l];
arry[l]=arry[r];
arry[r]=temp;
}
private boolean isVowels(char c){
return c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
c=='A'||c=='E'||c=='I'||c=='O'||c=='U';
}
}