LeeCode题目笔记

2019-09-28 反转字符串中的元音字母

2019-09-30  本文已影响0人  Antrn

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"
示例 2:

输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"。

对撞指针法

C++1
class Solution {
public:
    string reverseVowels(string s) {
        int len = s.length();
        int i=0, j=len-1;
        string yy = "aeiou"; 
        while(i<j){
            if(yy.find(tolower(s[i])) == std::string::npos){
                i++;
                continue;
            }
            if(yy.find(tolower(s[j])) == std::string::npos){
                j--;
                continue;
            }
            
            int temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
        return s;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读