class Solution {
public:
void swap(int l,int r,string& s){
char tmp=s[l];
s[l]=s[r];
s[r]=tmp;
}
string reverseVowels(string s) {
int l=0,r=s.size()-1;
unordered_set<char> map={'a','e','i','o','u'};
while(l<r){
while(l<r && !map.count(tolower(s[l]))){
l+=1;
}
while(l<r && !map.count(tolower(s[r]))){
r-=1;
}
if(map.count(tolower(s[l])) && map.count(tolower(s[r]))){
swap(l,r,s);
}
l+=1;
r-=1;
}
return s;
}
};
LEETCODE 345. 反转字符串中的元音字母
最新推荐文章于 2024-07-25 17:54:26 发布