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;
    }
};
相关推荐
该怎么办呢3 分钟前
Source/Core/Event.js
开发语言·javascript·ecmascript·cesium
做怪小疯子4 分钟前
Leetcode刷题——深度优先搜索(DFS)
算法·leetcode·深度优先
似水明俊德4 分钟前
04-C#.Net-委托和事件-面试题
java·开发语言·面试·c#·.net
大数据AI人工智能培训专家培训讲师叶梓14 分钟前
120B 数学语料 + GRPO 算法,DeepSeekMath 刷新开源大模型推理天花板
人工智能·算法·大模型·推理·deepseek·openclaw·openclaw 讲师
IMPYLH15 分钟前
Linux 的 comm 命令
linux·运维·算法
johnrui20 分钟前
集合与树形结构
开发语言·windows
薛定谔的悦24 分钟前
嵌入式设备OTA升级实战:从MQTT命令到自动重启的全流程解析
linux·算法·ota·ems
杰克尼31 分钟前
知识点总结--01
数据结构·算法
该怎么办呢33 分钟前
Source/Core/DeveloperError.js
开发语言·javascript·ecmascript
小璐资源网35 分钟前
Java 21 新特性实战:虚拟线程详解
java·开发语言·python