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;
    }
};
相关推荐
紫金修道5 小时前
【DeepAgent】概述
开发语言·数据库·python
Via_Neo5 小时前
JAVA中以2为底的对数表示方式
java·开发语言
书到用时方恨少!5 小时前
Python multiprocessing 使用指南:突破 GIL 束缚的并行计算利器
开发语言·python·并行·多进程
cch89185 小时前
PHP五大后台框架横向对比
开发语言·php
天真萌泪5 小时前
JS逆向自用
开发语言·javascript·ecmascript
野生技术架构师6 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
袋鼠云数栈6 小时前
集团数字化统战实战:统一数据门户与全业态监管体系构建
大数据·数据结构·人工智能·多模态
Q一件事6 小时前
R语言制图-相关性及关系网络图
开发语言·r语言
坊钰6 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
小月球~6 小时前
天梯赛 · 并查集
数据结构·算法