LeetCode : Reverse Vowels of a String

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>ch;
        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')
                    ch.push_back(s[i]);
        }
        reverse(ch.begin(),ch.end());
        int k = 0;
        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')
                    s[i] = ch[k++];
        }
        return s;
    }
};

posted on 2017-03-27 21:36  gechen  阅读(92)  评论(0编辑  收藏  举报

导航