反转字符串中的元音字母 -- LeetCode -- 8.19

反转字符串中的元音字母

给你一个字符串 s ,仅反转字符串中的所有元音字母,并返回结果字符串。

元音字母包括 'a''e''i''o''u',且可能以大小写两种形式出现。

 

示例 1:

输入:s = "hello"
输出:"holle"

示例 2:

输入:s = "leetcode"
输出:"leotcede"

使用C++库函数find();

find()

  str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置,否则返回 string::npos,时间复杂度O(N);

刚开始做,忽略了大写元音字母,狗贼阴我;

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

  

posted @ 2021-08-19 10:50  荣荣荣荣荣荣  阅读(47)  评论(0)    收藏  举报