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".
Note:
The vowels does not include the letter "y".
class Solution {
public:
int is(char a){
if(a == 'a' || a == 'e' || a == 'i' || a == 'o' || a=='u')return 1;
if(a == 'A' || a == 'E' || a == 'I' || a == 'O' || a=='U')return 1;
return 0;
}
string reverseVowels(string s) {
int i = 0, j = s.length()-1;
while(i < j){
while(i < j && !is(s[i]))i++;
while(i < j && !is(s[j]))j--;
swap(s[i++],s[j--]);
}
return s;
}
};
本文介绍了一个C++实现的方法,该方法接受一个字符串作为输入,并仅反转其中的元音字母,保持辅音字母的位置不变。通过两个指针从两端向中间遍历的方式,有效地完成了元音字母的反转。
562

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



