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;
    }
};
相关推荐
lsylalalala16 分钟前
常见的排序算法1
数据结构·算法·排序算法
名字还没想好☜23 分钟前
Go 的 unsafe.Pointer 实战:零拷贝 []byte↔string 转换与三条铁律
开发语言·后端·golang·go·unsafe
想吃火锅10051 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
wuminyu1 小时前
JVM利用io_uring优化堆外内存性能原理剖析
java·linux·c语言·jvm·c++
gumichef1 小时前
第二章:C/C++内存管理
c++·内存管理
想吃火锅10051 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
imaol11 小时前
数据结构---队列
java·数据结构·算法
数模竞赛Paid answer1 小时前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
疯狂打码的少年1 小时前
【数据结构】串的模式匹配:KMP算法(重点)
数据结构·笔记·算法
一米阳光86611 小时前
软考(中级)软件设计师核心笔记(8)数据结构——线性结构、数组、矩阵
数据结构·笔记·职场发展·软考·软件设计师·中级职称