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".
解法:
双向指针解决,题目意思没说得很清,坑。
class Solution {
public String reverseVowels(String s) {
int len = s.length();
if(len == 0) return "";
int left = 0;
int right = len-1;
char[] c = s.toCharArray();
while(left<right){
if(isVowels(c[left]) && isVowels(c[right])){
char tmp = c[left];
c[left] = c[right];
c[right] = tmp;
left++; //处理完要移位啊!
right--; //处理完要移位啊!
}else if(!isVowels(c[left])){
left++;
}else if(!isVowels(c[right])){
right--;
}
}
return String.valueOf(c);
}
public boolean isVowels(Character c){
return c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U';
}
}