版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51315651
原文
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = “hello”, return “hello”.
Example 2:
Given s = “leetcode”, return “leotcede”.
翻译
写一个函数,用一个字符串作为参数,将其中的所有元音字母调整为逆序。
代码
string vowel = "aoeiuAOEIU";
bool isVowel(char c) {
for (int i = 0; i < vowel.length(); i++) {
if (c == vowel[i])
return true;
}
return false;
}
string reverseVowels(string s) {
string result;
stack<char> vowels;
for (int i = 0; i < s.length(); i++) {
if (isVowel(s[i])) {
vowels.push(s[i]);
}
}
for (int i = 0; i < s.length(); i++) {
if (isVowel(s[i])) {
result += vowels.top();
vowels.pop();
} else {
result += s[i];
}
}
return result;
}
Copyright
Nomasp by Ke Yuwang is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Permanent Link:nomasp.com , ALL RIGHTS RESERVED.