leetcode——345—— Reverse Vowels of a String

来源:互联网 发布:淘宝汉服商家推荐 编辑:程序博客网 时间:2024/04/28 02:13

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".


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

0 0