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;
    }
};
相关推荐
小二李1 分钟前
什么是依赖注入(DI)&控制反转IoC
java·开发语言
智者知已应修善业7 分钟前
【51单片机非精准计时2个外部中断启停】2023-5-29
c++·经验分享·笔记·算法·51单片机
是宇写的啊10 分钟前
SpringIoc和Di
java·开发语言
沐雪轻挽萤12 分钟前
3. C++17新特性-带初始化的 if 和 switch 语句
开发语言·c++
QQ6765800814 分钟前
基于YOLO26算法的智慧农业橙子图像识别 橙子采摘识别 水果采摘识别 高清采摘过程图像识别 YOLO+voc个数据集第10410期
算法·yolo·橙子采摘·水果采摘识别·高清采摘过程图像识别
xianluohuanxiang17 分钟前
2026年深度:高精度气象+新能源,从风速误差到收益偏差,行业赋能正在重构电站盈利模型
大数据·开发语言·人工智能·机器学习
froginwe1124 分钟前
SQL PRIMARY KEY(主键)
开发语言
2401_8858850427 分钟前
视频短信接口集成起来复杂吗?API接入说明
开发语言·php·音视频
潇洒畅想29 分钟前
1.2 希腊字母速查表 + 公式阅读实战
java·人工智能·python·算法·rust·云计算
Thexhy30 分钟前
Java 后端完整成长路线(含项目)
java·开发语言