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;
    }
};
相关推荐
魏码不凡2 小时前
CURL报错:未找到SSL证书文件问题
开发语言·php·ssl
tudousisi2222 小时前
P4667 [BalticOI 2011] Switch the Lamp On复盘
c++
geovindu2 小时前
go:loghelper
开发语言·后端·golang
啊真真真3 小时前
ArgoCD:我的GitOps探索之旅与未来展望
java·算法·argocd
听雨入夜3 小时前
zero.zhang
开发语言·python
江屿风3 小时前
【C++笔记】【二叉搜索树】流食般投喂
开发语言·数据结构·c++·笔记
1001101_QIA3 小时前
VRChat 插件开发方式
开发语言·vr
海绵天哥4 小时前
LeetCode Hot 100 | 链表(下)· 分组翻转与设计(C++ 题解)
c++·leetcode·链表
choumin4 小时前
行为型模式——中介者模式
c++·设计模式·中介者模式·行为型模式