编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: “hello”
输出: “holle”
示例 2:
输入: “leetcode”
输出: “leotcede”
说明:
元音字母不包含字母”y”。
考察双指针
class Solution {
public String reverseVowels(String s) {
String vowel = "aeiouAEIOU";
char[] vowels = vowel.toCharArray();
char[] array = s.toCharArray();
int i = 0, j = array.length - 1;
while( i < j ){
while( i < j && !vowel.contains(array[i] + " ")){
i++;
}
while( i < j && !vowel.contains(array[j] + " ")){
j--;
}
if (i < j) {
char c = array[i];
array[i++] = array[j];
array[j--] = c;
}}
return new String(array);
}
}