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;
    }
};
相关推荐
gugucoding13 分钟前
2. 【Java】搭建Java开发环境
java·开发语言
多加点辣也没关系13 分钟前
JavaScript|第9章:对象 — 基础
开发语言·javascript·ecmascript
2601_9498180920 分钟前
Vector从入门到应用(C++ STL动态数组万字全解
开发语言·c++
汉克老师44 分钟前
GESP2026年6月认证C++八级( 第三部分编程题(1、线网建设))精讲
c++·最小生成树·排序·kruskal·并查集·gesp8级
胖大和尚1 小时前
Linux 内核工程师、HPC工程师、C++工程师
c++·kernel·hpc
alphaTao1 小时前
LeetCode 每日一题 2026/7/6-2026/7/12
算法·leetcode
想吃火锅10051 小时前
【leetcode】56.合并区间js
算法·leetcode·职场和发展
imuliuliang1 小时前
可合并堆在多任务调度中的优势与实现技巧7
算法
学究天人1 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷6)
网络·算法·数学建模·动态规划·几何学·图论·拓扑学
汤米粥1 小时前
Python爬虫中Xpath用法之——normalize-space()
开发语言·python