#Leetcode# 345. Reverse Vowels of a String

https://leetcode.com/problems/reverse-vowels-of-a-string/

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Input: "hello"
Output: "holle"

Example 2:

Input: "leetcode"
Output: "leotcede"

Note:
The vowels does not include the letter "y".

代码:

class Solution {
public:
    string reverseVowels(string s) {
        int len = s.length();
        string ans = "";
        vector<char> v;
        
        for(int i = 0; i < len; i ++) {
            if(isvowel(s[i]))
                v.push_back(s[i]);
        }
        
        int cnt = v.size() - 1;
        for(int i = 0; i < len; i ++) {
            if(!isvowel(s[i]))
                ans += s[i];
            else {
                ans += v[cnt];
                cnt --;
                continue;
            }
        }
        return ans;
    }
    
    bool isvowel(char c) {
        if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
           c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
            return true;
        
        return false;
    }
};

 

原文地址:https://www.cnblogs.com/zlrrrr/p/10301448.html