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;
    }
};
相关推荐
飞Link14 分钟前
什么是 PX4?一文了解 PX4 在空地协同中的作用
算法·无人机
进化中的刀盾兽36 分钟前
从零实现 C++ Json-Rpc(十):把消息交给真实连接——MuduoConnection 与 ConnectionFactory
c++·rpc
一去不复返的通信er7 小时前
力扣-图论-岛屿数量
算法·leetcode·图论
2501_944521597 小时前
Ramda 跨端三方库仓颉适配全流程
开发语言·javascript·flutter·php
行思理7 小时前
go语言学习难度及适合学习的人群
开发语言·学习·golang
青瓦梦滋7 小时前
WebSocket协议
linux·服务器·网络·c++·websocket·网络协议·http
wangchunyu1149 小时前
JAVA CompletableFuture异步编程实战
java·开发语言·python
程序 代码狂人10 小时前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
HDU-Dade10 小时前
【校招笔试真题】简要介绍 Self-Attention 的前沿算法
人工智能·算法
meiying883811 小时前
快递单号批量查询Python脚本:从手动到自动化的完整实现
开发语言·python·自动化