leetcodeブラシ問題、まとめ、記録、メモ345

1316 ワード

leetcode345
Reverse Vowels of a String
 
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1: Given s = "hello", return "holle".
Example 2: Given s = "leetcode", return "leotcede".
Subscribe to see which companies asked this question
easy問題は、母音アルファベットのインデックスを記録し、頭と尾をそれぞれ巡り、2つ交換すればいい.
class Solution {
public:
    string reverseVowels(string s) {
        vector<int> vi;
        
        for (int i = 0; i < s.size(); ++i)
        {
            switch(s[i])
            {
                case 'a':
                case 'o':
                case 'e':
                case 'i':
                case 'u':
                case 'A':
                case 'O':
                case 'E':
                case 'I':
                case 'U':
                vi.push_back(i);
                break;
            }
        }
        
        int i = 0;
        int j = vi.size() - 1;
        
        char temp;
        for (; i < j; ++i, --j)
        {
            temp = s[vi[i]];
            s[vi[i]] = s[vi[j]];
            s[vi[j]] = temp;
        }
        
        return s;
    }
};