[LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
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".
这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o,u,需要注意的是大写的也算,所以总共有十个字母。我们写一个isVowel的函数来判断当前字符是否为元音字母,如果两边都是元音字母,那么我们交换,如果左边的不是,向右移动一位,如果右边的不是,则向左移动一位,参见代码如下:
解法一:
class Solution {
public:
string reverseVowels(string s) {
int left = , right= s.size() - ;
while (left < right) {
if (isVowel(s[left]) && isVowel(s[right])) {
swap(s[left++], s[right--]);
} else if (isVowel(s[left])) {
--right;
} else {
++left;
}
}
return s;
}
bool isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
};
或者我们也可以用自带函数find_first_of和find_last_of来找出包含给定字符串中任意一个字符的下一个位置进行交换即可:
解法二:
class Solution {
public:
string reverseVowels(string s) {
int left = , right = s.size() - ;
while (left < right) {
left = s.find_first_of("aeiouAEIOU", left);
right = s.find_last_of("aeiouAEIOU", right);
if (left < right) {
swap(s[left++], s[right--]);
}
}
return s;
}
};
我们也可以把元音字母都存在一个字符串里,然后每遇到一个字符,就到元音字符串里去找,如果存在就说明当前字符是元音字符,参见代码如下:
解法三:
class Solution {
public:
string reverseVowels(string s) {
int left = , right = s.size() - ;
string t = "aeiouAEIOU";
while (left < right) {
if (t.find(s[left]) == string::npos) ++left;
else if (t.find(s[right]) == string::npos) --right;
else swap(s[left++], s[right--]);
}
return s;
}
};
类似题目:
参考资料:
https://leetcode.com/discuss/99048/easy-to-understand-c-solution
https://leetcode.com/discuss/99047/super-clean-solution-using-find_first_of-and-find_last_of
https://leetcode.com/discuss/99062/java-two-pointers-solution-easy-understand-finish-interview
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母的更多相关文章
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- [Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...
- LeetCode:反转字符串中的元音字母【345】
LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...
随机推荐
- word2vec 中的数学原理详解
word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包,它简单.高效,因此引起了很多人的关注.由于 word2vec 的作者 Tomas Miko ...
- 史上最全、JavaScript基础篇
本章内容: 简介 定义 注释 引入文件 变量 运算符 算术运算符 比较运算符 逻辑运算符 数据类型 数字 字符串 布尔类型 数组 Math 语句 条件语句(if.switch) 循环语句(for.fo ...
- SparkStreaming实现Exactly-Once语义
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 译自:http://blog.cloudera.com/blog/2015/03/exactly ...
- 实践 HTML5 的 CSS3 Media Queries
先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...
- input输入
只能输入数字onkeyup='this.value=this.value.replace(/\D/gi,"")'限制文本框只能输入正数,小数onkeyup="value= ...
- Hive学习笔记(一)
摘要: Hive 是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据的机制.H ...
- 《连载 | 物联网框架ServerSuperIO教程》- 12.服务接口的开发,以及与云端双向交互
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- 5.1 JS中Object类型
1.Object类型是引用类型中的一种. 2.创建Object实例(对象)的方式: 方式1:使用new操作符,后面跟上Object构造函数.如: var obj = new Object();//创建 ...
- SharePoint Server2016的User Profile Services服务
前言SharePoint Server 的早期版本具有内置的 ForeFront Identity Manager (FIM) 副本,可在 SharePoint Server 服务器产品内运行.具 ...
- simplestyle
simplestyle-spec A simple specification for styling GeoJSON data. Versions 1.1.0 Adds properties to ...