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

描述

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

 

示例 1:

输入:"hello"
输出:"holle"
示例 2:

输入:"leetcode"
输出:"leotcede"
 

提示:

元音字母不包含字母 "y" 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string/

求解

    class Solution {
    private:
        std::unordered_set<char> vowelsRec{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

    public:
        // 方法一,申请额外的空间存储元音字母,时间复杂度O(N), 空间复杂度O(K),其中K < N
        string reverseVowels_1e(string s) {
            string strVowels;
            const int n = s.size();
            for (int i = 0; i < n; ++i) {
                char lc = tolower(s[i]);  //转换为小写字母确定是否是元音字母,减少条件判断
                if (lc == 'a' || lc == 'e' || lc == 'i' || lc == 'o' || lc == 'u') {
                    strVowels.push_back(s[i]);
                }
            }
            std::reverse(strVowels.begin(), strVowels.end());

            int k = 0;
            for (int i = 0; i < n; ++i) {
                char lc = tolower(s[i]);
                if (lc == 'a' || lc == 'e' || lc == 'i' || lc == 'o' || lc == 'u') {
                    s[i] = strVowels[k++];
                }
            }
            return s;
        }

        // 方法二,指针对撞,直接原地修改,时间复杂度O(N), 空间复杂度O(1)
        string reverseVowels_2e(string s) {
            const int n = s.size();
            int low = 0;
            int high = n - 1;
            while (low < high) {
                char llc = tolower(s[low]);
                while (low < n - 1 && (llc != 'a' && llc != 'e' && llc != 'i' && llc != 'o' && llc != 'u')) {
                    llc = tolower(s[++low]);
                }

                char lhc = tolower(s[high]);
                while (high > 0 && (lhc != 'a' && lhc != 'e' && lhc != 'i' && lhc != 'o' && lhc != 'u')) {
                    lhc = tolower(s[--high]);
                }

                if (low > high) {
                    return s;
                }

                std::swap(s[low], s[high]);
                ++low;
                --high;
            }
            return s;
        }

        // 方法二优化版,将元音字母的条件修改为查询,指针对撞,直接原地修改,时间复杂度O(N), 空间复杂度O(1)
        string reverseVowels(string s) {
            const int n = s.size();
            int low = 0;
            int high = n - 1;
            while (low < high) {
                while (low < n - 1 && (vowelsRec.count(s[low]) == 0)) {
                    ++low;
                }

                while (high > 0 && (vowelsRec.count(s[high]) == 0)) {
                    --high;
                }

                if (low > high) {
                    return s;
                }

                std::swap(s[low], s[high]);
                ++low;
                --high;
            }
            return s;
        }
    };

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值