美文网首页
LeetCode*345. Reverse Vowels of

LeetCode*345. Reverse Vowels of

作者: _Xie_ | 来源:发表于2017-07-26 21:58 被阅读0次

    LeetCode题目链接

    题目:

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

    Note:
    The vowels does not include the letter "y".

    答案:

    class Solution {
    public:
        string reverseVowels(string s) {
            string sv = "aeiouAEIOU";
            int i = 0;
            int j = s.length() - 1;
            
            while (i < j) {
                /* std::string::npos Means => static const size_t npos = -1; */
                while ((sv.find(s[i]) == std::string::npos) && (i < j)) {
                    i++;
                }
                while ((sv.find(s[j]) == std::string::npos) && (i < j)) {
                    j--;
                }
                
                if ((s[i] != s[j]) && (i < j)) {
                    swap(s[i], s[j]);
                }
                
                i++;
                j--;
            }
            return s;
        }
    };
    

    相关文章

      网友评论

          本文标题:LeetCode*345. Reverse Vowels of

          本文链接:https://www.haomeiwen.com/subject/eznwkxtx.html