Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
在面试的时候就让写了这题。。。
两个指针分别从前和从后遍历,遇到元音则交换字符
public class Solution {
public String reverseVowels(String s) {
char[] ca = s.toCharArray();
String vowels = "aeiouAEIOU";
int left = 0, right = ca.length-1;
while(left<right){
while(left<ca.length && !vowels.contains(ca[left]+"")){
left++;
}
while(right>=0 && !vowels.contains(ca[right]+"")){
right--;
}
if(left>=right) break;
char temp = ca[left];
ca[left++] = ca[right];
ca[right--] = temp;
}
return new String(ca);
}
}