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;
    }
};
相关推荐
KaMeidebaby4 分钟前
卡梅德生物技术快报|实操手册:CXCL4 蛋白原核表达全套工艺,两步层析去除蛋白多聚体附完整电泳数据
人工智能·算法·机器学习·架构·spark
浩瀚之水_csdn9 分钟前
Python 3 网络编程详解:从原理到实战
开发语言·网络·python
geovindu10 分钟前
java: Singleton Pattern
java·开发语言·后端·单例模式·设计模式·创建型模式
AIGCmagic社区11 分钟前
Unlimited OCR 论文精读:R-SWA 如何实现一次性长文档解析
人工智能·算法·aigc
AI行业学习19 分钟前
2026 版 Notepad++ 完整图文安装指南|官方渠道无捆绑,一键切换中文界面
开发语言·人工智能·python·html·notepad++
汉克老师27 分钟前
GESP2026年6月认证C++一级( 第一部分选择题(8-15))精讲
c++·continue·运算符·while·break·gesp1级·模运算
鹏易灵32 分钟前
C++——7.类与对象,掌握封装、继承、多态.详解
开发语言·c++·算法
水无痕simon34 分钟前
5 多表操作
java·开发语言·数据库
初阳78535 分钟前
【Qt】系统相关(2)——文件
开发语言·qt
大P哥阿豪37 分钟前
Go panic & recover:深入Go语言的异常拯救之道
开发语言·后端·golang·go