Idea
将s中的元音字母存在字符串sv中,并且使用一个数组依次存储元音字母的下标。
然后将字符串sv进行反转,并遍历元音下标数组,将反转后的字符串sv依次插入到源字符串s中
AC Code
class Solution {
public:string reverseVowels(string s) {int n = s.size();vector<int> index;string sv;for(int i = 0; i < n; i++) {if(s[i] == 'A' || s[i] == 'a' || s[i] == 'E' || s[i] == 'e' || s[i] == 'I' ||s[i] == 'i' || s[i] == 'O' || s[i] == 'o' || s[i] == 'U' || s[i] == 'u') {index.emplace_back(i);sv += s[i];}}reverse(sv.begin(),sv.end());int k = 0;for(int i : index) s[i] = sv[k++];return s;}
};