#include <unordered_map>
class Solution {
public:
string reverseVowels(string s) {
int i = 0, j = s.size()-1;
while (i < j) {
if (!is_vowel(s[i])) {
i++;
continue;
}
if (!is_vowel(s[j])) {
j--;
continue;
}
swap(s[i], s[j]);
i++;
j--;
}
return s;
}
bool is_vowel(char& t) {
unordered_map<char, int> vowel_map = {
{'a', 1},
{'A', 1},
{'e', 1},
{'E', 1},
{'i', 1},
{'I', 1},
{'o', 1},
{'O', 1},
{'u', 1},
{'U', 1}
};
auto it = vowel_map.find(t);
if (it != vowel_map.end()) {
return true;
}
return false;
}
};
反转字符串中的元音字母-字符串345-c++
最新推荐文章于 2024-05-21 18:08:55 发布