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;
    }
};
相关推荐
清水白石00814 小时前
从“能装上”到“可复现”:Python 团队如何正确使用 requirements.txt、锁定文件与依赖分组
开发语言·人工智能·python
赏金术士14 小时前
Kotlin 习题集 · 基础篇
android·开发语言·kotlin
jiayong2314 小时前
Python面试题集 - 基础语法与核心概念
开发语言·windows·python
sanqima14 小时前
C++里strcpy()拷贝的3种写法
c++·字符串拷贝
ch.ju14 小时前
Java程序设计(第3版)第三章——数组的遍历
java·开发语言
艾莉丝努力练剑14 小时前
【Linux网络】Linux 网络编程:应用层自定义协议与序列化(2)序列化与反序列化
linux·运维·服务器·c++·网络协议·序列化
凯瑟琳.奥古斯特14 小时前
Django Flask FastAPI 三者对比
开发语言·python·django·flask·fastapi
智者知已应修善业14 小时前
【51单片机一个按键切合初始流水灯按一下对半闪烁按一下显示时间】2023-10-16
c++·经验分享·笔记·算法·51单片机
晚风叙码14 小时前
堆排序建堆策略对比:向上调整与向下调整的时间复杂度分析
算法
青春易逝丶14 小时前
JAVA基础面试题
java·开发语言