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;
    }
};
相关推荐
Zach_yuan16 小时前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
我在人间贩卖青春16 小时前
C++之this指针
c++·this
爱敲代码的TOM16 小时前
数据结构总结
数据结构
云姜.16 小时前
java多态
java·开发语言·c++
CoderCodingNo16 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
陳103016 小时前
C++:红黑树
开发语言·c++
大闲在人16 小时前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
一切尽在,你来16 小时前
C++ 零基础教程 - 第 6 讲 常用运算符教程
开发语言·c++
泉-java16 小时前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言
小熳芋16 小时前
443. 压缩字符串-python-双指针
算法