public class Solution {
public String reverseVowels(String s) {
char[] c = s.toCharArray();
int head = 0;
int tail = c.length - 1;
HashSet<Character> set = new HashSet<>(Arrays.asList('a', 'A', 'e', 'E',
'i', 'I', 'o', 'O', 'u', 'U'));
while(head < tail){
//遇到数组都先考虑是否可能有数组下标越界!
while(head < c.length && !set.contains(c[head++]));//head不能超出c.length,都是因为在循环体内的移动
head--;//定位到要交换的字符
while(tail >= 0 && !set.contains(c[tail--]) );//tail不能小于0,都是因为在循环体内的移动
tail++;//定位到要交换的字符
//System.out.println("head: " + head + c[head] + " " + "tail: " + tail + c[tail]);
//开始交换,交换之前还要判断一次head是否小于tail,因为在最外层循环体内还有移动
if(head < tail){
char t = c[head];
c[head] = c[tail];
c[tail] = t;
}
//head,tail往中间挪动
head++;
tail--;
}
return new String(c);
}
}