美文网首页
[LeetCode 345]Reverse Vowels of

[LeetCode 345]Reverse Vowels of

作者: FTVBeginning | 来源:发表于2016-04-28 13:49 被阅读0次

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".

Method:
1.Vowels are 'a' 'e' 'i' 'o' 'u' & 'A' 'E' 'I' 'O' 'U'
2.Use two pointers

C++:
class Solution {
public:
bool isVowels(char c){
c = tolower(c);
return c=='a'||c=='e'||c=='i'||c=='o'||c=='u';
}
string reverseVowels(string s) {
int i=-1,j=s.size();
while(i<j){
while(!isVowels(s[++i])&&i<j);
while(!isVowels(s[--j])&&i<j);
if(i>=j) break;
swap(s[i],s[j]);}

    return s;}

};

相关文章

网友评论

      本文标题:[LeetCode 345]Reverse Vowels of

      本文链接:https://www.haomeiwen.com/subject/plqhrttx.html