leetcode-345-反转字符串中的元音字母

bool helper(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;
}

void reverse(string &s, int low, int high) {
    char temp;
    if (low >= high) return;
    while (!helper(s[low])) {
        low++;
        if (low >= high) return;
    }
    while (!helper(s[high])) {
        high--;
    }
    temp = s[low];
    s[low] = s[high];
    s[high] = temp;
    reverse(s, low + 1, high - 1);
}

class Solution {
public:
    string reverseVowels(string s) {
        string res;
        //deque<char> vowels;
        //deque<int> index;
        int N = s.length();
        //if (N == 1) return s;
        //if (N == 0) return "";
        reverse(s, 0, N-1);
        res = s;
        return res;

    }
};

相关文章: