Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
solution:
把元音字母都存在一个字符串里,然后每遇到一个字符,就到元音字符串里去找,如果存在就说明当前字符是元音字符
code:
class Solution {
public:
string reverseVowels(string s) {
int left = 0, right = s.size() - 1;
string t = "aeiouAEIOU";
while (left < right) {
if (t.find(s[left]) == string::npos) ++left;
else if (t.find(s[right]) == string::npos) --right;
else swap(s[left++], s[right--]);
}
return s;
}
};