-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathReverseVowelsSln.cs
66 lines (56 loc) · 1.69 KB
/
ReverseVowelsSln.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoPointers.Lib
{
// Write a function that takes a string as input and reverse only the vowels of a string.
//Example 1:
//Given s = "hello", return "holle".
//1
//1
//Example 2:
//Given s = "leetcode", return "leotcede".
public class ReverseVowelsSln
{
public class Solution
{
private List<char> vowels = new List<char> { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' };
private char[] chars;
public string ReverseVowels(string s)
{
chars = s.ToCharArray();
int lo = 0;
int hi = s.Length - 1;
while (lo < hi)
reverseVowels(ref lo, ref hi);
return new string(chars);
}
public void reverseVowels(ref int lo, ref int hi)
{
for (int i = lo; i <= hi; i++, lo++)
{
if (vowels.Contains(chars[i]))
break;
}
for (int i = hi; i >= lo; i--, hi = i)
{
if (vowels.Contains(chars[i]))
break;
}
if (lo < hi)
{
if (chars[lo] != chars[hi])//交换字符
{
char tmp = chars[lo];
chars[lo] = chars[hi];
chars[hi] = tmp;
}
++lo;
--hi;
}
}
}
}
}