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;
    }
};
相关推荐
牛奔1 小时前
Go 如何打印调试深层或嵌套的结构体
开发语言·后端·golang
m沐沐2 小时前
【深度学习】YOLOv2目标检测算法——改进点、网络结构与聚类先验框解析
人工智能·pytorch·深度学习·算法·yolo·目标检测·transformer
不会就选b2 小时前
算法日常・每日刷题--<链表>3
数据结构·算法·链表
geovindu2 小时前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
学逆向的2 小时前
汇编——JCC指令
开发语言·汇编·网络安全
mayaairi3 小时前
JS循环语句深度解析:嵌套for、while与do...while
开发语言·前端·javascript
kite01213 小时前
Go语言Map深度解析与最佳实践
开发语言·后端·golang
稚南城才子,乌衣巷风流4 小时前
块状链表:数据结构详解与实现
数据结构·链表
Zachery Pole4 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米4 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展