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".
bool isVowels(char c)
{
if('a' == c || 'o' == c ||'e' == c ||'i' == c ||'u' == c ||'A' == c || 'O' == c ||'E' == c ||'I' == c ||'U' == c)
return true;
else
return false;
}
int swap(char *a,char *b)
{
char tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
char* reverseVowels(char* s)
{
int len = strlen(s);
int dict[256] = {0};
dict['a'] = 1, dict['A'] = 1;
dict['e'] = 1, dict['E'] = 1;
dict['i'] = 1, dict['I'] = 1;
dict['o'] = 1, dict['O'] = 1;
dict['u'] = 1, dict['U'] = 1;
int start = 0;
int end = len - 1;
while(start < end)
{
while((dict[s[start]]==0)&&(start < end))
{
start++;
}
while((dict[s[end]]==0) &&(start < end))
{
end--;
}
if(start >= end)
break;
/*exchange*/
swap(&s[start],&s[end]);
start++;
end--;
}
return s;
}
原本判断是否是原因字母时使用的函数,AC之后看到讨论区有人用dict这种形式,相对于函数调用更清晰,减少了函数调用开销。