题目:

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 cases.

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.

思路:

要交换位置,明显的双指针。先用哈希set记录下元音,可以只记录全大写或者全小写,在判断的时候多写一些语句也行,不过这里偷懒就把大小写元音都记录下来了。两个指针分别index = 0 和 n - 1,只要左指针的当前字母不在哈希set内,右移;同理右指针的当前字母不在哈希set内就左移,如果当前左指针index小于右指针index,则交换元素并且再次移动指针。最后返回字符串即可。

代码:

class Solution {
public:
    string reverseVowels(string s) {
        unordered_set<char> mp= {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        int n = s.size(), i = 0, j = n - 1;
        while (i < j) {
            while (i < n && !mp.count(s[i]))
                i++;
            while ( j > 0 && !mp.count(s[j]))
                j--;
            if (i < j) {
                swap(s[i], s[j]);
                i++;
                j--;
            }
        }
        return s;
    }
};

345. Reverse Vowels of a String相关推荐

  1. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  2. LeetCode:345. Reverse Vowels of a String

    051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...

  3. LeetCode 345. Reverse Vowels of a String

    题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

  4. 【LeetCode】345. Reverse Vowels of a String 解题小结

    题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

  5. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  6. 345. Reverse Vowels of a String(python+cpp)

    题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

  7. 【LeetCode】345. Reverse Vowels of a String 解题报告

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51429823 Subject 出处:https://leetcode.com/prob ...

  8. Leetcode 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', ...

  9. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. 1 class Solution { 2 public: 3 bool i ...

最新文章

  1. PHP7 学习笔记(八)JetBrains PhpStorm 2017.1 x64 MySQL数据库管理工具的使用
  2. Arduino与NodeMCU——联网
  3. 通过document id和content拿到SAP document的binary data
  4. label标签/标记
  5. PHP和MySQL Web开发pdf
  6. 天津科技大学计算机网络,计算机网络PPT(天津科技大学讲稿-张强)第一章英文对照...
  7. C语言中声明复数用什么字母,用c语言定义复数-20210407134457.docx-原创力文档
  8. MySQL 数据库系统
  9. 我安装java了_我安装了JAVA为什么.......
  10. vue echarts div变化_数据可视化之echarts在Vue中的使用
  11. 昨天发现,博客排名进行了两次
  12. python 波浪号用法_波浪号(~)是什么意思,正规的用法是什么?
  13. 你理解的商业数据分析到底是怎样的?
  14. 【云原生|K8s系列第5篇】:实战使用Service暴露应用
  15. Hive 中的时间加减暨间隔函数INTERVAL
  16. 产品读书《滚雪球:巴菲特和他的财富人生》
  17. 边云协同的优点_云边协同的现实意义
  18. java低位_Java 高位低位
  19. ubuntu的应用商店打不开,闪退
  20. ET篇:master消息机制介绍(一般消息的流转)

热门文章

  1. ibmx340服务器硬盘,IBM3850安装操作系统
  2. IC学习笔记——DRV8840
  3. 硬盘界面发展史:ATA, AHCI, NVMe. PATA, SATA, SAS, Mini PCIE/MSATA, M.2(PCEx2/SATA or PCIEx4)
  4. 短视频系统源代码,自定义圆盘,方向盘
  5. JS新建文件到本地(不弹出保存文件框)
  6. 如何提高网站 Google 排名
  7. 跨境独立站MaTaCart教你怎么查谷歌排名
  8. 敞开心扉,相互依赖,才可能拥有爱情
  9. Node.js使用mongoose操作mongodb
  10. Java bin 目录下的小工具使用与学习