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;
    }
};
相关推荐
JieE2125 分钟前
树与二叉树--JS实例
javascript·数据结构
码云骑士5 分钟前
05-Python字典底层原理-Hash表与有序性的真相
开发语言·python·哈希算法
J2虾虾5 分钟前
Android支持Java语言的标准
android·java·开发语言
Cloud_Shy6186 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第六章 Item 44 - 47)
开发语言·人工智能·经验分享·笔记·python
qeen876 分钟前
【C++】类与对象之零散知识点补充(四)
c++·笔记·学习·语法
To_OC7 分钟前
搞懂二叉树递归遍历,我居然是从爬楼梯开始的
前端·javascript·数据结构
mxlwd1688 分钟前
movielen 100k lr模型训练过程
开发语言·python·机器学习
Jerryhut14 分钟前
opencv对齐算法及其应用
人工智能·opencv·算法
AI人工智能+电脑小能手19 分钟前
【大白话说Java面试题 第113题】【并发篇】第13题:说一下乐观锁的优点和缺点?
java·开发语言·面试