题目大意:将一个字符串中的元音字母逆序并返回
例如:Given s = "hello", return "holle".
Given s = "leetcode", return "leotcede".
元音字母总共有"aeiouAEIOU"这5个,分析一下便可知,如果只将一个字符串中的元音字母拿出来看,比如“leetcode”,那就是“eeoe”,逆序之后为“eoee”,将这个逆序的元音字符串按序插入到原字符串就可以得到“leotcede”
下面是代码
class Solution {
public:
string reverseVowels(string& s) {
int len = s.length();
char * arr = new char[len];
int count = 0;
string Vowels = "aeiouAEIOU";
for(int i = 0; i < len; i++){
if(Vowels.find(s[i]) != string::npos){
arr[count] = s[i];
count++;
}
}
for(int i = 0; i < len; i++){
if(Vowels.find(s[i]) != string::npos){
s[i] = arr[count-1];
count--;
}
}
return s;
}
};