345. Reverse Vowels of a String【easy】
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".
解法一:
class Solution {
public:
bool isVowels(char x)
{
if (x >= 'A' && x <= 'Z') {
x += ;
}
return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
}
void reverseChar(string &s, int start, int end)
{
char temp = s[start];
s[start] = s[end];
s[end] = temp;
}
string reverseVowels(string s) {
int start = ;
int end = s.length() - ;
while (start < end) {
while (start < end && !isVowels(s[start])) {
++start;
}
while (start < end && !isVowels(s[end])) {
--end;
}
reverseChar(s, start, end);
++start;
--end;
}
return s;
}
};
注意循环的条件
class Solution {
public:
string reverseVowels(string s) {
int i = , j = s.size() - ;
while (i < j) {
i = s.find_first_of("aeiouAEIOU", i);
j = s.find_last_of("aeiouAEIOU", j);
if (i < j) {
swap(s[i++], s[j--]);
}
}
return s;
}
};
参考@tedbear 的代码。
345. Reverse Vowels of a String【easy】的更多相关文章
- 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: In ...
- 53. Reverse Words in a String【easy】
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
- 【一天一道LeetCode】#345. Reverse Vowels of a String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 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:Giv ...
随机推荐
- 【数论】【枚举约数】【欧拉函数】bzoj2705 [SDOI2012]Longge的问题
∵∑gcd(i, N)(1<=i <=N) =k1*s(f1)+k2*s(k2)+...+km*s(km) {ki是N的约数,s(ki)是满足gcd(x,N)=ki(1<=x< ...
- CSS box-flex属性,然后弹性盒子模型简介(转)
一.淡淡的开头语 昨天趁着不想工作的时间间隙闲逛24ways,在My CSS Wish List一文中,见到了个新鲜的CSS属性,就是题目中的box-flex,以前没有见过,顿生疑惑,不知是骡子还是马 ...
- golang垃圾回收
常见GC算法 我总结了一下常见的 GC 算法.分别是:引用计数法.Mark-Sweep法.三色标记法.分代收集法. 1. 引用计数法 原理是在每个对象内部维护一个整数值,叫做这个对象的引用计数,当对象 ...
- 在energia中添加新的库
很多时候energia提供的库不能够满足我们的需要,这个时候我们就要自己添加库到energia中.方法如下: 在energia目录下找到hardware目录 选择对应的单片机型号文件夹进入 进入lib ...
- Word绘制跨行表格
如图“用户评价电影数目”,我们需要均分第一行,选中这三个个,设置了表个高度0.5cm,但是发现上面的一个比较考上,我们需要找到水平竖直居中,那么双击表格,打开表格工具,有设计和布局,切换到布局就找到了 ...
- 手把手教你使用FineUI开发一个b/s结构的取送货管理信息系统系列博文索引
近阶段接到一些b/s类型的软件项目,但是团队成员之前大部分没有这方面的开发经验,于是自己选择了一套目前网上比较容易上手的开发框架(FineUI),计划录制一套视频讲座,来讲解如何利用FineUI快速开 ...
- HashMap深度解析(二)
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/16890151,转载请注明. 上一篇比较深入的分析了HashM ...
- 【AS3 Coder】任务六:人物换装(纸娃娃)系统的制作
使用框架:AS3(Flash Professional CS5.0及更高版本 + Flash Buider)任务描述:了解人物换装系统的制作原理难度系数:2 本章源码下载:http://www.iam ...
- 【云计算】Docker监控相关资料
Cloud Insight 是东半球首款次世代系统监控工具:http://www.oneapm.com/ci/docker.html?utm_source=BaiduPaid&utm_medi ...
- 【MySQL】海量量数据查询优化
参考资料: mysql处理海量数据时的一些优化查询速度方法:http://www.cnblogs.com/lingiu/p/3414134.html mysql千万级大数据SQL查询优化:http:/ ...