题目链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string/
题目描述
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。
代码
class Solution {
public:
string reverseVowels(string s) {
int l = 0, r = s.size();
unordered_set<char> m = {'a','e','i','o','u','A','E','I','O','U'};
while(l < r){
while(l<r && m.count(s[l]) == 0) ++l;
while(l<r && m.count(s[r]) == 0) --r;
if(l < r)
swap(s[l++],s[r--]);
}
return s;
}
};