您的位置:首页 > 其它

345.[LeetCode]Reverse Vowels of a String

2016-05-13 00:44 337 查看

题意:

反转字符串中的元音字母 aeiou 带 大写

我的思路:

有点鸡贼

遍历一遍字符串,并且把其中元音字母都换成 不常用字符‘~’,并且用temp倒序存储这些字符

再遍历一遍,从temp中取出字符放入 ‘~’ 的位置

class Solution {
public:
string reverseVowels(string s) {
//这里也可以使用set来排除aeiou
char ch[] = {'a','e','i','o','u','A','E','I','O','U'};
set<char> c(ch,ch+10);
string temp = "";
int size = s.size();
for(int i = 0; i<size; i++)
if(c.count(s[i]) == 1) {
temp = s[i]+temp;
s[i] = '~';
}
int j = 0;
for(int i = 0; i<size; i++)
if(s[i] == '~') {
s[i] = temp[j++];
}
return s;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: