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;
    }
};
相关推荐
Elivs2 分钟前
RMSNorm函数
人工智能·算法·机器学习
看浪的路人19 分钟前
第3讲:代码补全引擎
开发语言·windows·python
tudousisi22227 分钟前
P4447 [AHOI2018初中组] 分组 题解复盘
算法
SNAKEpc1213828 分钟前
OpenGL(十一)- 变换管线
c语言·c++·算法·矩阵·图形渲染
小小龙学IT38 分钟前
Day 26-27 项目实战:从零构建一个高并发聊天室(epoll + 线程池)
linux·服务器·c语言·开发语言·网络
牢姐与蒯1 小时前
c++高级数据结构之图(基本概念,存储结构,BFS,DFS,最小生成树)
数据结构·c++·
我就是妖怪1 小时前
【无标题】
开发语言
Tongzhi20261 小时前
考勤数据本地化存储:通芝科技无感考勤一体机的数据安全逻辑
大数据·数据结构·科技·均值算法·数据库开发·推荐算法
c238561 小时前
MySQL 基础用法(下):查询进阶与核心特性
android·c语言·c++·mysql
0566461 小时前
Python高级——迭代器
开发语言·python·学习