Leetcode 345:反转字符串中的元音字母

题目描述

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

示例 1:

输入: "hello"
输出: "holle"

示例 2:

输入: "leetcode"
输出: "leotcede"

说明:
元音字母不包含字母"y"。

解题思路

class Solution {
public:
    inline bool isYuan(char c){
        return (c=='a' || c=='e' || c == 'i' || c=='o' || c=='u');
    }
    string reverseVowels(string s) {
        if(s.length() <= 1) return s;
        int i=0,j=s.length()-1;
        while(i<j){
            bool ji = isYuan(tolower(s[i]));
            bool jj = isYuan(tolower(s[j]));
            if(ji && jj){
                swap(s[i],s[j]);
                i++;j--;
            }else{
                if(!ji) i++;
                if(!jj) j--;
            }
        }
        return s;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_35338624/article/details/90051182