[LeetCode]345. Reverse Vowels of a String

来源:互联网 发布:网络书香过大年 编辑:程序博客网 时间:2024/06/05 08:45

[LeetCode]345. Reverse Vowels of a String

题目描述

这里写图片描述

思路

从前往后,从后往前分别找元音字母,若找到且满足前后的位置关系则交换

代码

#include <iostream>#include <string>#include <algorithm>using namespace std;class Solution {public:    string reverseVowels(string s) {        string vowels = "aeiouAEIOU";        int start = 0, end = s.size() - 1;        while (start < end) {            start = s.find_first_of(vowels, start);            end = s.find_last_of(vowels, end);            if (start < end)                swap(s[start++], s[end--]);        }        return s;    }};int main() {    Solution s;    cout << s.reverseVowels("Aa") << endl;    system("pause");    return 0;}
原创粉丝点击