问题:https://leetcode.com/problems/reverse-vowels-of-a-string/?tab=Description
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”.
分析:调用函数,先判断是否为元音字母。头的和尾的调转。
C++代码:
class Solution {
public:
string reverseVowels(string s) {
int i=0;
int j=s.length()-1;
while(i<j){
while(isVowel(s[i])==false && (i<j)){
i++;
}
while(isVowel(s[j])==false && (i<j)){
j--;
}
swap(s[i],s[j]);
i++;
j--;
}
return s;
}
bool isVowel(char c){
if((c=='a') ||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U'))
return true;
else
return false;
}
};