天天看點

c# leetcode 345. 反轉字元串中的元音字母(雙指針)

編寫一個函數,以字元串作為輸入,反轉該字元串中的元音字母。

示例 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);
        }