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;
    }
};
相关推荐
starzy19901 小时前
深入理解Scala隐式类:扩展方法机制
开发语言·人工智能·scala
M--Y1 小时前
LangGraph 工作流模式
开发语言·javascript·python·langchain·大模型·llm·langgraph
forest_092 小时前
多 Agent 协作第二课:交叉验证≠独立验证
人工智能·python·算法·机器学习·prompt·agi
九英里路3 小时前
protobuf快速上手
网络·c++·中间件·json·谷歌·序列化·protobuf
吴可可1233 小时前
WScript Shell操作注册表详解
开发语言
纪念 2293 小时前
C++ 算法基础 (差分)
开发语言·c++·算法
jyOverQ6 小时前
从零学 JVM(六):垃圾回收算法详解——标记清除、标记复制与标记整理
jvm·算法
码农小河668 小时前
基于PHP的电商后台蜜罐系统
开发语言·php
starzy19908 小时前
深入理解Scala柯里化函数:函数链式变换
开发语言·scala
ShineWinsu8 小时前
对于Qt:QTreeWidget常见属性以及QTreeWidgetItem类的解析
开发语言·c++·qt·gui·客户端·多叉树·qtreewidget