345 Reverse Vowels of a String Reverse vowels in a string

Write a function that takes a string as input and reverses the vowels in that string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Note:
vowels do not include "y".
See: https:// leetcode.com/problems/reverse-vowels-of-a-string/description/

C++:

class Solution {
public:
    string reverseVowels(string s)
    {
        int left = 0, right= s.size() - 1;
        while (left < right)
        {
            if (isVowel(s[left]) && isVowel(s[right]))
            {
                swap(s[left++], s[right--]);
            }
            else if (isVowel(s[left]))
            {
                --right;
            }
            else
            {
                ++left;
            }
        }
        return s;
    }
    bool isVowel(char c)
    {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    }
};

 Reference: https://www.cnblogs.com/grandyang/p/5426682.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324400650&siteId=291194637