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;
    }
};
相关推荐
2401_838472511 分钟前
自定义操作符重载指南
开发语言·c++·算法
v_for_van3 分钟前
单片机内存分配管理笔记
开发语言·c++·笔记·vscode·stm32·单片机·嵌入式硬件
鹿角片ljp6 分钟前
力扣136.只出现一次的数字-异或和HashMap
java·数据结构·算法·leetcode
W_a_i_T7 分钟前
【Coding日记】菜鸟编程C语言100例——第三题⚠️
c语言·开发语言·经验分享·算法·菜鸟编程
TracyCoder1238 分钟前
LeetCode Hot100(5/100)——11. 盛最多水的容器
算法·leetcode
weixin_452159559 分钟前
多协议网络库设计
开发语言·c++·算法
十五年专注C++开发9 分钟前
浅谈C++插件机制的设计要点以及实现方案
开发语言·c++·系统架构·插件机制
Hello World . .10 分钟前
C语言printf: VT100 终端控制码
c语言·开发语言·jvm
爱装代码的小瓶子10 分钟前
【C++与Linux基础】文件篇 -语言特性上的文件操作
linux·开发语言·c++
C+-C资深大佬12 分钟前
C++优化
开发语言·c++