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;
    }
};
相关推荐
罗湖老棍子几秒前
最优乘车(travel)(信息学奥赛一本通- P1377)
算法·图论·bfs·最短路·字符串流·单向边
旺仔小拳头..4 分钟前
Java ---变量、常量、类型转换、默认值、重载、标识符、输入输出、访问修饰符、泛型、迭代器
java·开发语言·python
副露のmagic9 分钟前
更弱智的算法学习 day36
学习·算法
玖釉-13 分钟前
[Vulkan 学习之路] 09 - 显卡的流水线工厂:图形管线概览 (Graphics Pipeline)
c++·windows·图形渲染
lsx20240622 分钟前
Vue3 自定义指令
开发语言
core51222 分钟前
SVD 算法详解:给数据做个“CT扫描”
算法·svd·图片压缩·目标函数
有一个好名字24 分钟前
力扣-确定两个字符串是否接近
算法·leetcode·职场和发展
牛奔31 分钟前
Go语言中结构体转Map优雅实现
开发语言·后端·macos·golang·xcode
wujj_whut35 分钟前
【Conda实战】从0到1:虚拟环境创建、多Python版本管理与环境切换全指南
开发语言·python·conda
蜗牛^^O^37 分钟前
java中的JUC
java·开发语言