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;
    }
};
相关推荐
慧都产品智能化3 分钟前
汽车软件工具鉴定:范过度扩张带来的测试与文档成本
c++·测试工具·单元测试·代码复审
码途行者3 分钟前
每天10分钟学Go:第21课 错误
开发语言·后端·golang
sharemylove36 分钟前
Java 常用类 学习笔记
java·开发语言·经验分享·笔记·学习
青少儿编程课堂1 小时前
蓝桥杯青少组图形化真题:校园无人机编队灯光秀解析(全国青少年信息素养大赛·电子学会12月图形化考级同源考点)
c++·python·算法·bfs·信息学竞赛
Shadow(⊙o⊙)2 小时前
专题六:模拟算法
算法
多加点辣也没关系10 小时前
JavaScript|第41章:性能优化实战
开发语言·javascript·性能优化
Resean022311 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
码途行者12 小时前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang
23图12 小时前
C 语言开散列表(拉链法)拆解:链挂得没错,key=-1 会去读 T[-1]
c语言·开发语言·散列表
feixing_fx13 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架