1.题目:https://leetcode-cn.com/problems/reverse-vowels-of-a-string/
2.思路
(1)双指针的方式,需要注意的是:题目并没有告诉原因字母包括大小写,所以元音有10种而不是五种,这次的模板类似于快排的写法
(2)将大小写的元音字母都写在字符串里面
3.代码
参考:https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/c-fan-zhuan-zi-fu-chuan-zhong-de-yuan-yin-zi-mu-by/
https://leetcode-cn.com/problems/reverse-vowels-of-a-string/comments/
方法1:
class Solution {
public:
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
while (i {
while (s[i]!='a' && s[i]!='e' && s[i]!='i' && s[i]!='o' && s[i]!='u' && s[i]!='A' && s[i]!='E' && s[i]!='I' && s[i]!='O' && s[i]!='U'&& i i++;
while (s[j]!='a' && s[j]!='e' && s[j]!='i' && s[j]!='o' && s[j]!='u' && s[j]!='A' && s[j]!='E' && s[j]!='I' && s[j]!='O' && s[j]!='U'&& i j--;
if (i swap(s[i],s[j]);
i++,j--;
}
return s;
}
};
方法2:
class Solution {
public:
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
string s1="aeiouAEIOU";
while (i {
while ((s1.find(s[i])==-1) && i i++;
while ((s1.find(s[j])==-1) && i j--;
if (i swap(s[i],s[j]);
i++,j--;
}
return s;
}
};
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net