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;
    }
};
相关推荐
xpbobcsdn20 分钟前
JDK 28 值类:省一半内存,慢三成
java·开发语言
承渊政道26 分钟前
【Python编程快速上手—让繁琐工作自动化】(组织文件:用Python批量改名,把整个文件夹打包备份)
开发语言·python·pycharm·自动化·文件整理·组织文件
默 语36 分钟前
HarmonyOS7 组件通用事件(二):onAppear / onDisAppear 挂载卸载事件——组件的“生死“只有 if 说了算
开发语言·php
不在逃避q37 分钟前
用生活里的小场景,一键打开 C++ 编程入门大门
开发语言·c++·生活
Shaoshing40 分钟前
LeetCode 1798. 你能构造出连续值的最大数目
java·数据结构·算法
YSL07012442 分钟前
C++string类详解
开发语言·数据结构·c++
迪康coolmu43 分钟前
基于迪康端点安全一体化管理系统的刻录通道审计:刻录记录、类型识别与追溯路径
运维·开发语言·网络·经验分享·科技·安全·php
大今野44 分钟前
江苏专转本计算机大类基础理论考点:算法的概念与复杂度分析
数据结构·算法·专转本
郝学胜-神的一滴1 小时前
量化交易从入门到精通 04:量化生意的四大特点
开发语言·数据结构·c++·python·算法·pycharm
青少儿编程课堂1 小时前
蓝桥杯青少组图形化真题:校园失物招领智能匹配解析(全国青少年信息素养、电子学会图形化考级同源考点)
c++·python·算法·bfs·信息学竞赛