LeetCode 345 Reverse Vowels of a String

LeetCode 345  Reverse Vowels of a String

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

 

char* reverseVowels(char* s) {
    int n=strlen(s);
    char temp;
    int i=0;
    int j=n-1;
    while(j>i)
    {
        while(j>i && s[i]!='a' &&s[i]!='e' &&s[i]!='i' &&s[i]!='o' &&s[i]!='u'&& s[i]!='A' &&s[i]!='E' &&s[i]!='I' &&s[i]!='O' &&s[i]!='U')
        {
            i++;
        }
        while(j>i && s[j]!='a' &&s[j]!='e' &&s[j]!='i' &&s[j]!='o' &&s[j]!='u'&& s[j]!='A' &&s[j]!='E' &&s[j]!='I' &&s[j]!='O' &&s[j]!='U')
        {
            j--;
        }
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        i++;
        j--;
    }
    return s;
}

使用快速排序的思想,前后设置两个指针,找到元音字母,然后交换。

 

posted @ 2016-04-26 09:34  Walker_Lee  阅读(213)  评论(0编辑  收藏  举报