编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
给定 s = "hello", 返回 "holle".
示例 2:
给定 s = "leetcode", 返回 "leotcede".
注意:
元音字母不包括 "y".
详见:https://leetcode.com/problems/reverse-vowels-of-a-string/description/
C++:
class Solution {
public:
string reverseVowels(string s)
{
int left = 0, right= s.size() - 1;
while (left < right)
{
if (isVowel(s[left]) && isVowel(s[right]))
{
swap(s[left++], s[right--]);
}
else if (isVowel(s[left]))
{
--right;
}
else
{
++left;
}
}
return s;
}
bool isVowel(char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
};
参考:https://www.cnblogs.com/grandyang/p/5426682.html