class Solution {
public:
string reverseVowels(string s) {
map<char,bool> mymap;
mymap['a'] = true;
mymap['e'] = true;
mymap['i'] = true;
mymap['o'] = true;
mymap['u'] = true;
mymap['A'] = true;
mymap['E'] = true;
mymap['I'] = true;
mymap['O'] = true;
mymap['U'] = true;
int len = s.length();
int i = 0;
int j = s.size()-1;
while(i<j)
{
while(!mymap[s[i]]&&i<j)
i++;
while(!mymap[s[j]]&&i<j)
j--;
swap(s[i++],s[j--]);
}
return s;
}
};
更改了判断元音字符的方式
class Solution {
public:
bool find(char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
||c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
while(i<j)
{
while(!find(s[i])&&i<j)
i++;
while(!find(s[j])&&i<j)
j--;
swap(s[i++],s[j--]);
}
return s;
}
};