Reverse Vowels of a String
Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraints:
1 <= s.length <= 3 * 105
s consist of printable ASCII characters.
思路一:双指针,先定位,然后交换指针字符
public String reverseVowels(String s) {
Set<Character> set = new HashSet<>();
set.add('a');set.add('A');
set.add('e');set.add('E');
set.add('i');set.add('I');
set.add('o');set.add('O');
set.add('u');set.add('U');
char[] array = s.toCharArray();
int left = 0;
int right = array.length - 1;
while (left < right) {
while (left < right && !set.contains(array[left])) {
left++;
}
while (left < right && !set.contains(array[right])) {
right--;
}
if (left < right) {
char t = array[left];
array[left] = array[right];
array[right] = t;
left++; right--;
}
}
return new String(array);
}
标签:set,String,345,add,right,easy,array,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/17035190.html