* Write a function that takes a string as input and reverse only the vowels of a string
* 意思就是将给定字符串中所有元音字母前后对调
* Given s = "hello", return "holle".sannereoooo
* 意思就是将给定字符串中所有元音字母前后对调
* Given s = "hello", return "holle".sannereoooo
* Given s = "linatcode", return "lenotcadi".
private string ReverseVowels2(string s)
{
List<int> map = new List<int>(); //存储字符串中所有元音字母的位置
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (IsVowels(chars[i]))
map.Add(i);
}
for (int i = 0; i <= map.Count / 2 - 1; i++)
{
char temp = chars[map[i]];
chars[map[i]] = chars[map[map.Count - i - 1]];
chars[map[map.Count - i - 1]] = temp;
}
return new string(chars);
}
private bool IsVowels(char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}