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".
描述:把字符串中的元音反转,input "hello",我的代码返回的还是hello,找不到问题在哪里,求解答!!谢谢!
class Solution {
public:
string reverseVowels(string s) {
if(s.size() == 0)
return s;
int i = 0, j = s.size() - 1;
while(i < j){
if((toupper(s[i])==('A'||'E'||'I'||'O'||'U')) && (toupper(s[j])==('A'||'E'||'I'||'O'||'U'))){
if(s[i]!=s[j]){
swap(s[i],s[j]);
}
i++,j--;//都是元音,不管是否交换,处理过后都要i++,j--
}
if(i>=j)
break;
if(toupper(s[i])!=('A'||'E'||'I'||'O'||'U'))
i++;
if(toupper(s[j])!=('A'||'E'||'I'||'O'||'U'))
j--;
if(i>=j)
break;
}
return s;
}
};