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;
    }
};
相关推荐
报错小能手21 小时前
ios开发方向——对于实习开发的app(Robopocket)讲解
开发语言·学习·ios·swift
wjs202421 小时前
Swift 方法
开发语言
小肥米21 小时前
分块查找ASL公式推导,为什么是两个ASL之和
数据结构·算法
样例过了就是过了21 小时前
LeetCode热题100 最小栈
数据结构·c++·算法·leetcode
计算机安禾21 小时前
【数据结构与算法】第18篇:数组的压缩存储:对称矩阵、三角矩阵与稀疏矩阵
c语言·开发语言·数据结构·c++·线性代数·算法·矩阵
今儿敲了吗21 小时前
51| 八皇后
c++·笔记·学习·算法·深度优先
华科易迅21 小时前
MybatisPlus乐观锁
java·开发语言·mybatis
Omics Pro21 小时前
端到端单细胞空间组学数据分析
大数据·数据库·人工智能·算法·数据挖掘·数据分析·aigc
迈巴赫车主21 小时前
错位排序算法
开发语言·数据结构·算法·排序算法
玖釉-21 小时前
暴力美学与极致性能:深度解析 Meshoptimizer 的 Sloppy 减面算法
c++·windows·图形渲染