c_cpp 345-2.cpp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 345-2.cpp相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    string reverseVowels(string s) {
        char base[10]={'a','e','i','o','u','A','E','I','O','U'};
        set<char> seting(base, base+10);
        int start = 0, end = s.size() - 1;
        while(start < end) {
            while(start < end && seting.find(s[start]) == seting.end()) {
                start++;
            }
            while(start < end && seting.find(s[end]) == seting.end()) {
                end--;
            }
            if (start < end) {
                swapchar(s, start++, end--);
            }
        }
        return s;
    };
    
    void swapchar(string &str,int pos1,int pos2)  
    {  
        char tmpch=str[pos2];  
        str[pos2]=str[pos1];  
        str[pos1]=tmpch;  
    }  
};
#define checkVowel(c) (c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
class Solution {
public:
    string reverseVowels(string s) {
        char base[10]={'a','e','i','o','u','A','E','I','O','U'};
        set<char> seting(base, base+10);
        for(int start = 0, end = s.size() - 1; start < end;) {
            if (checkVowel(s[start])) {
                if (checkVowel(s[end])) {
                    swap(s[start++], s[end--]);
                } else {
                    end--;
                }
            } else {
                start++;
            }
        }
        return s;
    }; 
};
class Solution {
public:
    string reverseVowels(string s) {
        char base[10]={'a','e','i','o','u','A','E','I','O','U'};
        set<char> seting(base, base+10);
        for(int start = 0, end = s.size() - 1; start < end;) {
            if (seting.find(s[start]) != seting.end()) {
                if (seting.find(s[end]) != seting.end()) {
                    //swapchar(s, start++, end--);
                    swap(s[start++], s[end--]);
                } else {
                    end--;
                }
            } else {
                start++;
            }
        }
        return s;
    };
    
    void swapchar(string &str,int pos1,int pos2)  
    {  
        char tmpch=str[pos2];  
        str[pos2]=str[pos1];  
        str[pos1]=tmpch;  
    }  
};

以上是关于c_cpp 345-2.cpp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *