【leetcode】345. Reverse Vowels of a String

时间:2022-10-17 12:13:58

problem

345. Reverse Vowels of a String

class Solution {
public:
string reverseVowels(string s) {
int left = , right =s.size()-;
char chl, chr;
while(left<right)
{
if(isVowel(s[left]) &&isVowel(s[right]))
{
char ch = s[left];
s[left++] = s[right];
s[right--] = ch;
}
else if(!isVowel(s[left]))
{
left++;
}
else if(!isVowel(s[right]))
{
right--;
} }
return s; }
bool isVowel(char ch)
{
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') return true;
else return false;
}
};

注意元音字母不仅有小写,还有大写的。

参考

1. Leetcode_345. Reverse Vowels of a String;