【leetcode】345. Reverse Vowels of a String

problem

345. Reverse Vowels of a String

 

class Solution {
public:
    string reverseVowels(string s) {
        int left = 0, right =s.size()-1;
        char chl, chr;
        while(left<right)
        {
            if(isVowel(s[left]) &&isVowel(s[right]))
            {
                char ch = s[left];
                s[left++] = s[right];
                s[right--] = ch;
            }
            else if(!isVowel(s[left]))
            {
                left++;
            }
            else if(!isVowel(s[right]))
            {
                right--;
            }
            
        }
        return s;
        
        
    }
    bool isVowel(char ch)
    {
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') return true;
        else return false;
    }
};
View Code

注意元音字母不仅有小写,还有大写的。

 

 

参考

1. Leetcode_345. Reverse Vowels of a String;

posted on 2019-02-25 09:13  鹅要长大  阅读(151)  评论(0编辑  收藏  举报

导航