345. Reverse Vowels of a String

来源:互联网 发布:centos 6.3升级 内核 编辑:程序博客网 时间:2024/06/05 03:09

Problem

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

Solution

class Solution {public:    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');    }    string reverseVowels(string s) {        int i = 0;        int j = s.size()-1;        while(i<j)        {            if(isVowel(s[i]))            {                if(isVowel(s[j]))                {                    swap(s[i],s[j]);                    ++i;                    --j;                }                else                    --j;            }            else if(isVowel(s[j]))            {                if(isVowel(s[i]))                {                    swap(s[i],s[j]);                    ++i;                    --j;                }                else                    ++i;            }            else            {                ++i;                --j;            }        }        return s;    }};
0 0