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;
    }
};
相关推荐
雪度娃娃7 分钟前
Effective Modern C++——型别推导
开发语言·c++
Hello eveybody25 分钟前
介绍一下背包DP(C++)
开发语言·c++·动态规划·dp·背包dp
代码中介商32 分钟前
C语言链表完全指南:从单节点到链表管理
c语言·算法·链表
charlie11451419139 分钟前
AwesomeQt:最小的Qt6系列迷你版本教程发布!
linux·c++·qt·c
Run_Teenage1 小时前
Linux:线程互斥,线程锁
运维·开发语言·jvm
小小de风呀1 小时前
de风——【从零开始学C++】(四):类和对象(下)
开发语言·c++·算法
覆东流1 小时前
第10天:python元组
开发语言·后端·python
CSCN新手听安1 小时前
【Qt】系统相关(一)内容简介,事件概念,事件的处理
开发语言·c++·qt
不想写代码的星星1 小时前
重识 std::tuple:一个被低估的编译期异构容器
开发语言·c++
aqiu1111111 小时前
[特殊字符]【算法日记 14】数论入门神题:最大公约数与最小公倍数的“乘积守恒定律”
算法