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;
    }
};
相关推荐
Amor风信子5 分钟前
华为OD机试真题---跳房子II
java·数据结构·算法
我是陈泽8 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
Mr.Z.41115 分钟前
【历年CSP-S复赛第一题】暴力解法与正解合集(2019-2022)
c++
优雅的小武先生19 分钟前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
Death20019 分钟前
使用Qt进行TCP和UDP网络编程
网络·c++·qt·tcp/ip
戊子仲秋22 分钟前
【LeetCode】每日一题 2024_10_2 准时到达的列车最小时速(二分答案)
算法·leetcode·职场和发展
邓校长的编程课堂24 分钟前
助力信息学奥赛-VisuAlgo:提升编程与算法学习的可视化工具
学习·算法
虽千万人 吾往矣25 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
创作小达人28 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
郭二哈30 分钟前
C++——list
开发语言·c++·list