写一个方法,接受给定字符串作为输入,并且只翻转字符串中的元音字母。
样例 1:
输入 : s = "hello"
输出 : "holle"
样例 2:
输入 : s = "lintcode"
输出 : "lentcodi".
class Solution {
public:
/**
* @param s: a string
* @return: reverse only the vowels of a string
*/
string reverseVowels(string &s) {
// write your code here
int len=s.size();
int j=len-1;
for (int i = 0; i < j; i++) {
/* code */
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')
{
while(j>i)
{
if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u'||s[j]=='A'||s[j]=='E'||s[j]=='I'||s[j]=='O'||s[j]=='U')
{
char temp=s[i];
s[i]=s[j];
s[j]=temp;
j--;
break;
}
j--;
}
}
}
return s;
}
};