【LeetCode】345. 反转字符串中的元音字母

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fuqiuai/article/details/83117461

题目链接https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/

题目描述

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

示例

输入: “hello”
输出: “holle”

输入: “leetcode”
输出: “leotcede”

解决方法

题目较简单

class Solution {
public:
    string reverseVowels(string s) {
        int left=0,right=s.size()-1;
        map<char,int> map1;
        map1['a']=1;map1['e']=1;map1['i']=1;map1['o']=1;map1['u']=1;
        map1['A']=1;map1['E']=1;map1['I']=1;map1['O']=1;map1['U']=1;
        while(left<right){
            while (!map1[s[left]]) left++;
            while (!map1[s[right]]) right--;  
            if (left<right) swap(s[left],s[right]);
            left++;right--;
        }
        return s;
    }
};

猜你喜欢

转载自blog.csdn.net/fuqiuai/article/details/83117461