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;
    }
};
相关推荐
safestar2012几秒前
React 19实战:Action、并发与性能,一次告别“意大利面状态”的升级
开发语言·javascript·vue.js
Ashore11_9 分钟前
蓝桥杯16届Java研究生组
java·算法·蓝桥杯
一只幸运猫.9 分钟前
Rust实用工具特型-Clone
开发语言·后端·rust
6Hzlia12 分钟前
【Hot 100 刷题计划】 LeetCode 76. 最小覆盖子串 | C++ 滑动窗口题解
c++·算法·leetcode
像素猎人16 分钟前
蓝桥杯OJ2049蓝桥勇士【动态规划】【dp[n]不是符合题意的答案,只是以an结尾的子问题的答案】
c++·算法·蓝桥杯·动态规划·区间dp
羊小猪~~17 分钟前
LLM--SFT简介
python·考研·算法·ai·大模型·llm·微调
0xDevNull21 分钟前
Java BigDecimal 完全指南:从入门到精通
java·开发语言·后端
桌面运维家21 分钟前
交换机环路排查:STP配置实战与网络故障精确定位
开发语言·php
XiYang-DING22 分钟前
【Java】从源码深入理解LinkedList
java·开发语言
837927397@QQ.COM23 分钟前
个人理解无界原理
开发语言·前端·javascript