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;
    }
};
相关推荐
码云数智-大飞7 小时前
PHP 接口开发规范:统一返回格式、异常处理与参数校验
开发语言·php
浮江雾8 小时前
Flutter第十七节-----路由管理(3)
android·开发语言·前端·javascript·flutter·入门
我不是懒洋洋8 小时前
从零实现一个分布式监控:Prometheus的核心设计
c++
admin and root8 小时前
「移动安全」安卓APP 反编译&frida脱壳技巧分享
android·开发语言·python·web安全·微信小程序·移动安全·攻防演练
踏月的造梦星球8 小时前
DM8 DSC 单机双实例部署
运维·开发语言·数据库
An_s8 小时前
c++对接pdfium(一)win系统篇
开发语言·c++
Zwarwolf8 小时前
Rust零散知识点项目汇总
开发语言·rust
-银雾鸢尾-8 小时前
C#中HashTable相关方法
开发语言·c#
众少成多积小致巨8 小时前
C++ 规范参考(上)
c++
茯苓gao8 小时前
嵌入式开发笔记:Qt信号槽机制深度解析——从原理到实战的全方位指南
开发语言·笔记·嵌入式硬件·qt·学习