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;
    }
};
相关推荐
superman超哥几秒前
Rust 函数定义与参数传递:所有权系统下的设计艺术
开发语言·rust·设计艺术·rust函数定义·rust参数传递
2301_789015621 分钟前
C++:set/multiset和map/multimap文档详细解析
c语言·开发语言·c++·vscode·排序算法·set·map
“抚琴”的人2 分钟前
C#上位机策略模式
开发语言·c#·策略模式
CoderCodingNo2 分钟前
【GESP】C++五级真题(数论-素数思想考点) luogu-P10720 [GESP202406 五级] 小杨的幸运数字
开发语言·c++·算法
zmzb01032 分钟前
C++课后习题训练记录Day59
开发语言·c++
黎雁·泠崖3 分钟前
C 语言文件操作进阶:格式化读写 + 二进制读写 + 随机读写进阶全解
c语言·开发语言
小李小李快乐不已4 分钟前
算法技巧理论基础
数据结构·c++·算法·leetcode·hot100
咕咕嘎嘎10246 分钟前
C++仿muduo库onethreadoneloop高并发服务器
服务器·网络·c++
少许极端9 分钟前
算法奇妙屋(二十一)-两个数组或字符串的dp问题(动态规划)
算法·动态规划·两个数组或字符串的dp问题
草莓熊Lotso9 分钟前
《算法闯关指南:递归,搜索与回溯算法--递归》--04. 两两交换链表中的结点 ,05.Pow(x,n)
数据结构·算法·链表