双指针法,但一定要注意,等号必须得有,不然就可能漏掉最中间那个倒霉的字母最后不能加到结果中去。
var reverseVowels = function(s) {
var p1=0,p2=s.length-1;
var vowels='aeiouAEIOU';
var res=[];
while(p1<=p2){
var c1=s.charAt(p1),c2=s.charAt(p2);
if(vowels.includes(c1)&&vowels.includes(c2)){
res[p1++]=c2;
res[p2--]=c1;
}
else if(!vowels.includes(c1)&&!vowels.includes(c2)){
res[p1++]=c1;
res[p2--]=c2;
}
else if(vowels.includes(c1)) res[p2--]=c2;
else if(vowels.includes(c2)) res[p1++]=c1;
}
return res.join("");
};