您的位置:首页 > 其它

345. Reverse Vowels of a String

2016-04-24 14:58 465 查看
Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:[/b]

Given s = "hello", return "holle".

Example 2:

Given s = "leetcode", return "leotcede".

Subscribe to see which companies asked this question

代码:

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;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: