345. Reverse Vowels of a String【easy】

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

解法一:

 1 class Solution {
 2 public:
 3     bool isVowels(char x)
 4     {
 5         if (x >= 'A' && x <= 'Z') {
 6             x += 32;
 7         }
 8         return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
 9     }
10
11     void reverseChar(string &s, int start, int end)
12     {
13         char temp = s[start];
14         s[start] = s[end];
15         s[end] = temp;
16     }
17
18     string reverseVowels(string s) {
19         int start = 0;
20         int end = s.length() - 1;
21
22         while (start < end) {
23             while (start < end && !isVowels(s[start])) {
24                 ++start;
25             }
26
27             while (start < end && !isVowels(s[end])) {
28                 --end;
29             }
30
31             reverseChar(s, start, end);
32             ++start;
33             --end;
34         }
35
36         return s;
37     }
38 };

注意循环的条件

解法二:
 1 class Solution {
 2 public:
 3     string reverseVowels(string s) {
 4         int i = 0, j = s.size() - 1;
 5         while (i < j) {
 6             i = s.find_first_of("aeiouAEIOU", i);
 7             j = s.find_last_of("aeiouAEIOU", j);
 8             if (i < j) {
 9                 swap(s[i++], s[j--]);
10             }
11         }
12         return s;
13     }
14 };

参考@tedbear 的代码。

转载于:https://www.cnblogs.com/abc-begin/p/7583477.html

345. Reverse Vowels of a String【easy】相关推荐

  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 解题报告

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

  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

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

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

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

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

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

  9. 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', ...

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

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

最新文章

  1. 实体类中用基本类型好,还是用包装类型
  2. HTML5 Canvas、内联 SVG、Canvas vs. SVG
  3. android 保存退出之前的页面_项目实战:Qt+Android模拟操作器(模拟操作app,打开,点击,输入,获取验证码等等)...
  4. python自动控制生产线输送线_一个关于自动化装配生产线结构组成案例,易懂干货...
  5. 网易云信三个课堂解决方案,让每个孩子都能享有公平而有质量的教育
  6. 如何知道 CPU 是否支持虚拟化技术(VT)
  7. SAFEARRAY使用方法示例
  8. python tableview添加内容_如何在Python PyQt4 TableView中以编程方式更改/更新数据?
  9. 使用快捷工具搜狗词库转txt和mmseg
  10. 如何使用VideoProc从MKV提取字幕?
  11. [白话解析] Flink的Watermark机制
  12. GoldWave音频混合剪辑教程
  13. Mothur3进阶_Mothur扩增子基因序列处理_数据比对、聚类及其处理评估
  14. Unity 自动化构建方案:一键实现版本管理与打包、压缩
  15. element-ui 执行 npm run build:theme 报错 Replace Autoprefixer browsers option to Browserslist config....
  16. windows合成pfx
  17. 单端阻抗为什么是50欧姆-探究
  18. Python基础篇(三)-- 列表、元组、字典、集合、字符串
  19. ERP生产管理软件系统的主要功能模块是什么?
  20. 想要的资源百度搜不到?6个只有老师傅才知道的网站,悄悄领走

热门文章

  1. Linux shell脚本详解及实战(二)——shell脚本之分支
  2. ubuntu18.04 下安装搜狗输入法
  3. Android调试神器stetho使用详解和改造
  4. 065_VFPage中CallBack回调函数的解释
  5. 阿里云云盾·Web应用防火墙 获“2018网络安全创新产品(技术)评选”一等奖
  6. Linux下用Java获取本机IP
  7. Windows azure中公用云服务的两个虚机FTP的设置
  8. Bash shell中的位置参数$#,$*,$@,$0,$1,$2...及特殊参数$?,$-等的含义 1
  9. 关注程序员健康之——研究显示白天小睡90分钟将有效增强记忆力
  10. silverlight html 传参,Silverlight与html、JavaScript三者交互