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 分钟前
框架--SpringMVC
java·开发语言·servlet
蒙奇D索大21 分钟前
【数据结构】考研数据结构核心考点:平衡二叉树(AVL树)详解——平衡因子与4大旋转操作入门指南
数据结构·笔记·学习·考研·改行学it
怎么没有名字注册了啊23 分钟前
查找成绩(数组实现)
c++·算法
沐怡旸33 分钟前
【算法】725.分割链表--通俗讲解
算法·面试
勤奋菲菲1 小时前
Vue3+Three.js:requestAnimationFrame的详细介绍
开发语言·javascript·three.js·前端可视化
要天天开心啊1 小时前
Java序列化和反序列化
java·开发语言
二宝1521 小时前
黑马商城day1-MyBatis-Plus
java·开发语言·mybatis
im_AMBER2 小时前
数据结构 04 栈和队列
数据结构·笔记·学习
AI+程序员在路上2 小时前
QT6中Combo Box与Combo BoxFont 功能及用法
c++·qt
Porunarufu2 小时前
JAVA·类和对象③封装及包
java·开发语言