My 9ms c++ solution


  • 0
    X

    class Solution {
    private:
    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;
    }
    public:
    string reverseVowels(string s) {
    int low = 0,high = s.length()-1;
    while(low < high)
    {
    if(isVowel(s.at(low)) && isVowel(s.at(high)))
    {
    char c = s[low];
    s[low] = s[high];
    s[high] = c;
    low++;
    high--;
    }
    else if(isVowel(s.at(low)))
    {
    high--;
    }
    else if(isVowel(s.at(high)))
    {
    low++;
    }
    else
    {
    low++;
    high--;
    }
    }
    return s;
    }
    };
    If any one has better solution,please share to me,thanks.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.