您的位置:首页 > 其它

LeetCode 345. Reverse Vowels of a String

2016-05-11 16:51 309 查看
题目大意:将一个字符串中的元音字母逆序并返回

例如: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;
}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode string