题目
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。
解题思路
public String reverseVowels(String s) {
//双指针
char[] charList = s.toCharArray();
int left = 0;
int right = charList.length-1;
char[] metaList = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
while (left < right){
// System.out.println(left + " " + right);
int retSearchLeft = Arrays.binarySearch(metaList, charList[left]);
int retSearchRight = Arrays.binarySearch(metaList, charList[right]);
// System.out.println(retSearchLeft + "+" + retSearchRight);
if (retSearchLeft >= 0 && retSearchRight >= 0){
char temp = charList[left];
charList[left] = charList[right];
charList[right] = temp;
left++;
right--;
}
else if (retSearchLeft < 0){
left++;
}
else {
right--;
}
}
s = String.valueOf(charList);
return s;
}