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;
    }
};
相关推荐
say_fall几秒前
C语言编程实战:每日一题:用队列实现栈
c语言·开发语言·redis
ゞ 正在缓冲99%…1 分钟前
2025.9.24华为软开
java·算法·华为
董世昌411 分钟前
前端跨域问题:原理、8 种解决方案与实战避坑指南
开发语言·前端·javascript
liupenglove1 分钟前
go-echarts基础使用方法
开发语言·golang·echarts
十五年专注C++开发4 分钟前
sigslot: 一个轻量级实现观察者模式的C++开源库
c++·观察者模式·开源
Dev7z4 分钟前
基于MATLAB实现SVM和集成特征的水稻病害图像识别系统
算法·机器学习·支持向量机
AI科技星5 分钟前
时空的几何之歌:论统一场论动量公式 P = m(C - V) 的完备重构、量化哲学诠释与终极验证
数据结构·人工智能·算法·机器学习·计算机视觉·重构
千千道6 分钟前
QT上位机作为FTP客户端上传多文件
c++·qt
Tony Bai8 分钟前
Go 2025云原生与可观测年度报告:底层性能革新与生态固防
开发语言·后端·云原生·golang
屿筱8 分钟前
vscode 关于C/C++的环境配置
c++·ide·vscode