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".
Method:
1.Vowels are 'a' 'e' 'i' 'o' 'u' & 'A' 'E' 'I' 'O' 'U'
2.Use two pointers
C++:
class Solution {
public:
bool isVowels(char c){
c = tolower(c);
return c=='a'||c=='e'||c=='i'||c=='o'||c=='u';
}
string reverseVowels(string s) {
int i=-1,j=s.size();
while(i<j){
while(!isVowels(s[++i])&&i<j);
while(!isVowels(s[--j])&&i<j);
if(i>=j) break;
swap(s[i],s[j]);}
return s;}
};
网友评论