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;
    }
};
相关推荐
Dxy12393102167 分钟前
Python在图片上画圆形:从入门到实战
开发语言·python
桌面运维家27 分钟前
IDV云桌面vDisk机房部署方案模板特性解析
java·开发语言·devops
CS_Zero28 分钟前
无人机路径规划算法——EGO-planner建模总结—— EGO-planner 论文笔记(一)
论文阅读·算法·无人机
杰梵28 分钟前
聚酯切片DSC热分析应用报告
人工智能·算法
飞翔的SA30 分钟前
从6.75%到100%!大模型Function Calling终极方案:Harness工程如何驯服
开发语言·ai·llm·harness
兵哥工控31 分钟前
MFC中return和break用法示例
c++·mfc
@BangBang36 分钟前
leetcode (4): 连通域/岛屿问题
算法·leetcode·深度优先
耿雨飞43 分钟前
Python 后端开发技术博客专栏 | 第 09 篇 GIL 深度解析与并发编程实战 -- 多线程、多进程、协程的选型
开发语言·python
Stark-C44 分钟前
NAS音乐必备神器,全平台音乐收割机!极空间部署『Go Music DL』
开发语言·后端·golang
Ulyanov1 小时前
像素迷宫:路径规划算法的可视化与实战
大数据·开发语言·python·算法