题目描述:
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
The vowels does not include the letter "y".
把字符串中元音顺序翻转,思路和翻转字符串是一样的,只是需要增加判断的步骤。
class Solution {
public:
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
while(i<=j)
{
while(!is_vowel(s[i])&&i<=j) i++;
while(!is_vowel(s[j])&&i<=j) j--;
if(i>j) break;
char temp=s[i];
s[i]=s[j];
s[j]=temp;
i++;
j--;
}
return s;
}
bool is_vowel(char c)
{
char x[10]={'a','e','i','o','u','A','E','I','O','U'};
for(int i=0;i<10;i++) if(c==x[i]) return true;
return false;
}
};