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;
    }
};
相关推荐
天远云服12 分钟前
天远企业司法认证API对接实战:PHP构建B2B供应链合规防火墙
大数据·开发语言·后端·node.js·php
空空kkk18 分钟前
Java基础——代理
java·开发语言
赵谨言19 分钟前
基于YOLOv5的植物目标检测研究
大数据·开发语言·经验分享·python
野生技术架构师19 分钟前
互联网大厂必备 Java 面试八股文真题解析
java·开发语言·面试
不光头强26 分钟前
IO流知识点
开发语言·python
老约家的可汗26 分钟前
C++篇之类和对象下
java·开发语言·c++
Mr_WangAndy26 分钟前
C++数据结构与算法_排序算法
c++·排序算法·基础排序·高级排序
水月wwww28 分钟前
Rust的安装与卸载 | windows
开发语言·windows·rust
niuniudengdeng34 分钟前
六面独立转动魔方还原机器人设计与实现
数学·算法·机器人
ghie909035 分钟前
基于MATLAB的A*算法避障路径规划实现
人工智能·算法·matlab