345. Reverse Vowels of a String(C++)

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

 

题目大意:

编写函数输入一个字符串,将其中的元音字母逆置。

 

解题方法:

双指针法(Two Pointers)

 

注意事项:


C++代码:

class Solution {
public:
    string reverseVowels(string s) {
        int left=0;
        int right=s.size()-1;
        string t="aeiouAEIOU";
        while(left<right)
        {
            if(t.find(s[left])==string::npos)
            {
                ++left;
            }
            else if(t.find(s[right])==string::npos)
            {
                --right;
            }
            else
            {
                swap(s[left++],s[right--]);
            }
        }
        return s;
    }
};

 

posted @ 2016-05-14 17:53  19Q3  阅读(117)  评论(0编辑  收藏  举报