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:
string reverseVowels(string s) {
vector<char>ss;
int *record=new int[1000000];
char *record_temp=new char[1000000];
int count=0;//num_counter
for(int i=0;i<s.length();i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){
ss.push_back(s[i]);
*(record+i)=1;
}
else{
*(record_temp+i)=s[i];
}
}
reverse(ss.begin(),ss.end());
for(int i=0;i<s.length();i++){
if(*(record+i)==1){
*(record_temp+i)=ss[count];
count++;
}
}
return record_temp;
}
};
本文介绍了一种算法,该算法接收一个字符串作为输入,并仅反转字符串中的元音字母,保持辅音字母的位置不变。通过实例展示了如何实现这一功能,如将'hello'转换为'holle','leetcode'转换为'leotcede'。
540

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



