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;
    }
};
相关推荐
fpcc几秒前
并行编程实战——CUDA编程的pipelines
c++·cuda
Irissgwe2 分钟前
数据结构-排序
数据结构·算法·排序算法
A__tao5 分钟前
告别手写 Go 结构体!推荐一个支持注释解析的 YAML 转 Struct 在线工具
开发语言·后端·golang
小O的算法实验室7 分钟前
2025年IEEE TITS,基于动态聚类粒子群算法的无人机任务分配与路径规划
算法
何以解忧,唯有..8 分钟前
Go 语言语句分隔符详解:分号、换行与代码规范
开发语言·golang·代码规范
Tairitsu_H11 分钟前
[LC优选算法#5] 分治:快排 | 颜色分类 | 排序数组 | 第K大元素
c++·算法·leetcode·排序算法·快速排序
人邮异步社区11 分钟前
C语言进阶的书籍推荐
c语言·开发语言
青山木13 分钟前
Hot 100 --- 滑动窗口最大值
java·数据结构·算法·leetcode·动态规划
青山木14 分钟前
Hot 100 --- 除自身以外数组的乘积
java·数据结构·算法
Frank学习路上18 分钟前
【C++】面试:STL容器与算法
c++·算法·面试