[LeetCode] 345. Reverse Vowels of a String

来源:互联网 发布:黑马java基础班测试题 编辑:程序博客网 时间:2024/06/11 02:04

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

class Solution {public:    string reverseVowels(string s) {        vector<int> idxs;        for (int i = 0; i < s.size(); i++) {            if (tolower(s[i]) == 'a' ||                tolower(s[i]) == 'e' ||                tolower(s[i]) == 'i' ||                tolower(s[i]) == 'o' ||                tolower(s[i]) == 'u') {                idxs.push_back(i);            }        }        for (int i = 0, j = (int)idxs.size() - 1; i < j; i++, j--) {            swap(s[idxs[i]], s[idxs[j]]);        }        return s;    }};