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;
    }
};
相关推荐
Shadow(⊙o⊙)2 分钟前
System V共享内存详解,shm系列接口,三种共享内存删除机制。System V通信缺点分析
linux·运维·服务器·开发语言·网络·c++
lightqjx2 分钟前
【算法】数据结构_并查集
数据结构·算法·并查集
小雨下雨的雨2 分钟前
鸿蒙PC Electron框架实现流体气泡模拟器
前端·人工智能·算法·华为·electron·鸿蒙
txzrxz3 分钟前
广度优先搜索详解(BFS)
算法·宽度优先
ZC跨境爬虫4 分钟前
跟着 MDN 学JavaScript day_4:如何存储你需要的信息——变量
开发语言·前端·javascript·ui·ecmascript
189228048614 分钟前
NV077固态MT29F16T08ESLCHL6-QAES:C
c语言·开发语言·性能优化
小小de风呀5 分钟前
de风——【从零开始学C++】(十三):优先级队列 priority_queue 全解析 & 仿函数入门
开发语言·c++
8Qi87 分钟前
LeetCode 198:打家劫舍(House Robber)—— 题解 ✅
算法·leetcode·动态规划
糖果店的幽灵7 分钟前
时间序列处理
开发语言·python·pandas
王老师青少年编程10 分钟前
信奥赛C++提高组csp-s之搜索进阶(记忆化搜索案例实践1)
c++·记忆化搜索·搜索·信奥赛·csp-s·提高组·滑雪