LeetCode345. Reverse Vowels of a String 解题小结

Posted 医生工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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".

 应该算不上有难度。

class Solution {
public:
    string reverseVowels(string s) {
        string str = s;
        for (int i = 0, j = str.size() - 1; i < j; )
        {
            if (isVowel(str[i]) && isVowel(str[j])) {
                char tmp = str[i];
                str[i] = str[j];
                str[j] = tmp;
                i++;
                j--;
            }
            if ( !isVowel(str[i]))++i;
            if ( !isVowel(str[j])) --j;
        }
        return str;
    }

    bool isVowel(char c) {
        if (c == a || c == e || c == i || c == u || c == o         || c == A || c == E || c == I || c == U || c == O) return true;
        return false;
    }
};

 

以上是关于LeetCode345. Reverse Vowels of a String 解题小结的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode #345 Reverse Vowels of a String

LeetCode 345. Reverse Vowels of a String

Leetcode 345. Reverse Vowels of a String

LeetCode_345. Reverse Vowels of a String

[LeetCode]345 Reverse Vowels of a String

leetcode 345. Reverse Vowels of a String