编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
while:
public static string ReverseVowels(string s)
{
var ss = s.ToCharArray();
int a = 0, b = s.Length - 1;
while (a<b)
{
while ("aeiouAEIOU".IndexOf(ss[a]) == -1 && a <= b - 1) a++;
while ("aeiouAEIOU".IndexOf(ss[b]) == -1 && b >= a + 1) b--;
if (a == b) return new string(ss);
var h = ss[a];
ss[a++] = ss[b];
ss[b--] = h;
}
return new string(ss);
}

本文介绍了一个实用的编程技巧,通过一个函数实现字符串中元音字母的反转,提供了详细的代码示例,包括如何判断元音、双指针交换等关键步骤。
1986

被折叠的 条评论
为什么被折叠?



