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;
    }
};
相关推荐
兮兮能吃能睡6 分钟前
R语言模型分析(一)(1)
开发语言·r语言
LoveXming12 分钟前
Chapter14—中介者模式
c++·microsoft·设计模式·中介者模式·开闭原则
wuk9982 小时前
基于有限差分法的二维平面热传导模型MATLAB实现
开发语言·matlab·平面
前端炒粉3 小时前
18.矩阵置零(原地算法)
javascript·线性代数·算法·矩阵
im_AMBER3 小时前
数据结构 09 二叉树作业
数据结构·笔记·学习
杨筱毅3 小时前
【C++】【常见面试题】最简版带大小和超时限制的LRU缓存实现
c++·面试
初见无风3 小时前
2.5 Lua代码中string类型常用API
开发语言·lua·lua5.4
做运维的阿瑞4 小时前
用 Python 构建稳健的数据分析流水线
开发语言·python·数据分析
左师佑图4 小时前
综合案例:Python 数据处理——从Excel文件到数据分析
开发语言·python·数据分析·excel·pandas