您的位置:首页 > 其它

Leetcode 345. Reverse Vowels of a String

2016-05-10 17:14 387 查看
Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Given s = "hello", return "holle".

class Solution {
public:
string reverseVowels(string s) {
string ss;
for(int i=0;i<s.size();i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){
ss.push_back(s[i]);
}
}
int len=ss.size();
int j=1;
for(int i=0;i<s.size();i++){

if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){
s[i]=ss[len-j];
j++;
}
}
return s;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: