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;
    }
};
相关推荐
阿凤217 分钟前
学习java---流程控制篇
java·开发语言·学习
ShineWinsu20 分钟前
对于Qt:QSound的解析
c++·qt·面试·音频·笔试·客户端·qsound
derek199730 分钟前
软考软件设计师:白盒覆盖哪条最弱、McCabe 复杂度怎么数、单元测试最能发现什么错
c语言·c++·计算机网络·软件工程·学习方法
Code_Solitude34 分钟前
c语言:期末最后一道大题模拟
c语言·开发语言
李松桃36 分钟前
Python爬虫基础
开发语言·爬虫·python·get请求使用方法·post请求使用方法
weixin_3077791337 分钟前
Python用正则把 SQL 关键字整词匹配后转成小写
开发语言·python·sql·算法
利威尔xu1 小时前
Go interface 底层:空接口装任何东西,类型断言漏写 ok 直接 panic 拖垮服务
开发语言·golang·interface·类型断言·itab
huaqianzkh1 小时前
WMI 查询入门到实战:从概念、WQL 语法到 C# 采集系统与硬件信息完整示例
开发语言·c#
Code_Solitude1 小时前
c语言:期末复习5(二维数组)
c语言·数据结构·算法
charliejohn1 小时前
计算机考研 408 数据结构 散列表 平均查找长度 ASL
数据结构·散列表