Leetcode 345 Reverse Vowels of a String 字符串处理

题意:倒置字符串中的元音字母。

用两个下标分别指向前后两个相对的元音字母,然后交换。

注意:元音字母是aeiouAEIOU。

 1 class Solution {
 2 public:
 3     bool isVowels(char c){
 4         string  Vowels = "aeiouAEIOU";
 5         for(int i = 0; i < Vowels.size(); ++i){
 6             if(c == Vowels[i]) return true;
 7         }
 8         return false;
 9     }
10     string reverseVowels(string s) {
11         int i = 0, j = s.size() - 1;
12         while(i < j){
13             if(!isVowels(s[i])) ++i;
14             if(!isVowels(s[j])) --j;
15             if(isVowels(s[i]) && isVowels(s[j])){
16                 swap(s[i],s[j]);
17                 ++i,--j;
18             }
19         }
20         return s;
21     }
22 };
原文地址:https://www.cnblogs.com/onlyac/p/5464666.html