LeetCode75——Day5

文章目录

一、题目

345. Reverse Vowels of a String

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1:

Input: s = "hello"

Output: "holle"

Example 2:

Input: s = "leetcode"

Output: "leotcede"

Constraints:

1 <= s.length <= 3 * 105

s consist of printable ASCII characters.

二、题解

双指针思路,定义左指针left和右指针right

cpp 复制代码
class Solution {
public:
    string reverseVowels(string s) {
        int n = s.length();
        unordered_map<char,int> map;
        map['A'] = 1;
        map['a'] = 1;
        map['E'] = 1;
        map['e'] = 1;
        map['I'] = 1;
        map['i'] = 1;
        map['O'] = 1;
        map['o'] = 1;
        map['U'] = 1;
        map['u'] = 1;
        int left = 0;
        int right = n - 1;
        while(left < right){
            while(left < n && !map.count(s[left])) left++;
            while(right > -1 && !map.count(s[right])) right--;
            if(left < right) swap(s[left++],s[right--]);
        }
        return s;
    }
};
相关推荐
ysa051030几秒前
【板子】树上启发式合并
数据结构·c++·笔记·算法
音符犹如代码2 分钟前
Java动态线程池:避坑原生线程池,吃透Dynamic-TP,手写一个Demo
java·开发语言
喜欢就别9 分钟前
平均风险:Rényi DP 和零集中 DP
开发语言·r语言
Simple_core15 分钟前
cmake模板和问题总结
c++
学究天人19 分钟前
数学公理体系大全:第一章 命题逻辑:真值之舞
人工智能·算法·机器学习·数学建模·动态规划·图论·抽象代数
小O的算法实验室20 分钟前
2025年Neurocomputing,基于LLM的进化优化器:结合精英策略推理
算法
无足鸟ICT32 分钟前
【RHCA+】bash命令
linux·开发语言·bash
方华世界1 小时前
企业级Java AI Agent应用平台
java·开发语言·人工智能
2301_800256111 小时前
数据结构基础期末复习例题
数据结构·算法
劉宇雁2 小时前
C++ ASCII 3D无尽跑酷游戏
c++·游戏·3d