Difficulty: Easy
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) {
string vowels="aeiouAEIOU";
int i=0, j=s.size()-1;
char tmp;
while(i<j){
while(i<j && vowels.find(s[i])==-1) i++;
while(i<j && vowels.find(s[j])==-1) j--;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
i++;
j--;
}
return s;
}
};
用两个指针,一个从前到后遍历,一个从后到前遍历,找到元音字母后互换。
注意大写字母的判断。