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;
    }
};
相关推荐
大黄说说9 分钟前
TensorRTSharp 实战指南:用 C# 驱动 GPU,实现毫秒级 AI 推理
开发语言·人工智能·c#
执着2599 分钟前
力扣hot100 - 144、二叉树的前序遍历
数据结构·算法·leetcode
范纹杉想快点毕业13 分钟前
嵌入式系统架构之道:告别“意大利面条”,拥抱状态机与事件驱动
java·开发语言·c++·嵌入式硬件·算法·架构·mfc
陳103014 分钟前
C++:map和set的使用
开发语言·c++
苏宸啊16 分钟前
list底层实现
c++·list
近津薪荼17 分钟前
递归专题(4)——两两交换链表中的节点
数据结构·c++·学习·算法·链表
2501_9403152617 分钟前
【无标题】2390:从字符串中移除*
java·开发语言·算法
乐观勇敢坚强的老彭19 分钟前
c++寒假营day01下午
c++·算法
lly20240623 分钟前
jEasyUI 树形菜单添加节点
开发语言
AI职业加油站26 分钟前
Python技术应用工程师:互联网行业技能赋能者
大数据·开发语言·人工智能·python·数据分析