https://leetcode.com/problems/reverse-vowels-of-a-string/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”.
题目翻译
题目解析
参考答案
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string reverseVowels(string s)
{
int iStart = 0;
int iEnd = s.size() - 1;
while (iStart < iEnd)
{
if (bIsVowels(s[iStart]) && bIsVowels(s[iEnd]))
{
vSwap(s[iStart], s[iEnd]);
iStart++;
iEnd--;
}
else if(!bIsVowels(s[iStart]))
{
iStart++;
}
else if (!bIsVowels(s[iEnd]))
{
iEnd--;
}
}
return s;
}
private:
bool bIsVowels(char c)
{
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
return true;
}
else
{
return false;
}
}
void vSwap(char & a, char & b)
{
char temp = a;
a = b;
b = temp;
}
};
int main()
{
Solution o;
string s = o.reverseVowels("aA");
return 0;
}