标签:http 题意 时间 har pop 方法 str margin int
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"
static public string ReverseVowels(string s) {
Stack<char> vowelsStack = new Stack<char>();
for (int i = 0; i < s.Length; i++) {
char c = s[i];
if (c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘o‘ || c == ‘u‘ ||
c == ‘A‘ || c == ‘E‘ || c == ‘I‘ || c == ‘O‘ || c == ‘U‘) {
vowelsStack.Push(c);
}
}
string rsult = "";
for (int i = 0; i < s.Length; i++) {
char c = s[i];
if (c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘o‘ || c == ‘u‘ ||
c == ‘A‘ || c == ‘E‘ || c == ‘I‘ || c == ‘O‘ || c == ‘U‘) {
rsult += vowelsStack.Pop();
} else {
rsult += c;
}
}
return rsult;
}
345. 反转字符串中元音字母的位置 Reverse Vowels of a String
标签:http 题意 时间 har pop 方法 str margin int
原文地址:http://www.cnblogs.com/xiejunzhao/p/f48165e05c104dd1e5fc71f231956abb.html