Reverse Vowels

Reverse Vowels

题目描述:

逆序给定字符串中的元音字母

例子:

具体描述看LeetCode345

解题思路:

我们利用vector保存下字符串中是元音的字符的对应位置。然后对字符串中的这些字符进行逆序即可。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
string reverseVowels(string s) {
set<char> hash = {'a','e','i','o','u','A','E','I','O','U'};
//记录元音字符集合
vector<int> vec = {};
//保存下元音字符的位置
for (int i = 0; i < s.size(); i++){
if (hash.find(s[i]) != hash.end()){
vec.push_back(i);
}
}
for (int j = 0; j < vec.size() / 2; j++){
swap(s[vec[j]], s[vec[vec.size() - j - 1]]);
}
return s;
}
};