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;
    }
};
相关推荐
froginwe117 分钟前
Python3 实例
开发语言
xiaoshuaishuai88 分钟前
C# ZLibrary数字资源分发
开发语言·windows·c#
穿条秋裤到处跑9 分钟前
每日一道leetcode(2026.04.22):距离字典两次编辑以内的单词
算法·leetcode
淘矿人10 分钟前
Claude辅助算法设计与优化
人工智能·python·算法·microsoft·github·bug·pygame
小碗羊肉11 分钟前
【从零开始学Java | 第四十二篇】生产者消费者问题(等待唤醒机制)
java·开发语言
流年如夢11 分钟前
自定义类型进阶:联合与枚举
java·c语言·开发语言·数据结构·数据库·c++·算法
wayz1114 分钟前
Day 10:集成学习进阶(Boosting: AdaBoost, GBDT)
算法·机器学习·集成学习·boosting
Little At Air15 分钟前
C++stack模拟实现
linux·开发语言·c++·算法
张祥64228890425 分钟前
导数与微分有啥区别
算法·数学建模
rayyy925 分钟前
c++, sizeof(string)和string.size()有什么区别
c++