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;
    }
};
相关推荐
rockmelodies4 分钟前
【PHP7内核剖析】-1.1 PHP概述
开发语言·php
又菜又爱玩呜呜呜~15 分钟前
go使用反射获取http.Request参数到结构体
开发语言·http·golang
Lululaurel16 分钟前
机器学习系统框架:核心分类、算法与应用全景解析
人工智能·算法·机器学习·ai·分类
愚润求学20 分钟前
【贪心算法】day8
c++·算法·leetcode·贪心算法
摸鱼仙人~20 分钟前
一文详解 Python 密码哈希库 Passlib
开发语言·python·哈希算法
平生不喜凡桃李23 分钟前
C++ 异常
android·java·c++
小伟童鞋33 分钟前
c++中导出函数调用约定为__stdcall类型函数并指定导出函数名称
开发语言·c++
维C泡泡33 分钟前
C++初认、命名规则、输入输出、函数重载、引用+coust引用
开发语言·c++
递归尽头是星辰38 分钟前
双指针与滑动窗口算法精讲:从原理到高频面试题实战
算法·双指针·滑动窗口·子串/子数组问题