[leetcode]345. Reverse Vowels of a String

[leetcode]345. Reverse Vowels of a String


Analysis

周五啦,端午小长假要开始啦~—— [嘻嘻~]

Write a function that takes a string as input and reverse only the vowels of a string.
跟上一题差不多,但是这题只翻转元音字母,注意大小写。

Implement

方法一:(space complexity O(N))

class Solution {
public:
    string reverseVowels(string s) {
        int len = s.length();
        int i, j;
        i = 0;
        j = len - 1;
        char t;
        while(i<j){
            if(isVowels(s[i]) && isVowels(s[j])){
                t = s[i];
                s[i] = s[j];
                s[j] = t;
                i++;
                j--;
            }
            if(!isVowels(s[i]))
                i++;
            if(!isVowels(s[j]))
                j--;
        }
        return s;
    }
    bool isVowels(char c){
        if(c=='a' || c=='e' || c=='i' || c=='o' || c == 'u' || c=='A' || c=='E' || c=='I' || c=='O' || c == 'U')
            return true;
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/80706728