【LeetCode】345. Reverse Vowels of a String

来源:互联网 发布:网络推广维护 编辑:程序博客网 时间:2024/06/18 07:35

题解:翻转元音字母,只需两指针,注意大小写,这里可以用string的方法find_first_of来查找元音位置

class Solution {public:    string reverseVowels(string s) {        int i = 0, j = s.size() - 1;        while (i < j) {            i = s.find_first_of("aeiouAEIOU", i);            j = s.find_last_of("aeiouAEIOU", j);            if (i < j) {                swap(s[i++], s[j--]);            }        }        return s;    }};


原创粉丝点击