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 ...
随机推荐
- 【数论】【枚举约数】【友好数】CODEVS 2632 非常好友
O(sqrt(n))枚举约数,根据定义暴力判断友好数. #include<cstdio> #include<cmath> using namespace std; int n; ...
- Problem S: 零起点学算法14——三位数反转
#include<stdio.h> #include<stdlib.h> int main() { int a,b,c,s; scanf("%d",& ...
- cocoapods更新
使用sudo gem install cocoapods更新提示: ERROR: While executing gem ... (Errno::EPERM) Operation not permit ...
- tomcat官网
http://tomcat.jaxmao.org/appdev/index.html 配置 http://www.cnblogs.com/starhu/p/5599773.html
- linux之touch命令修改文件的时间戳
功能:对已经存在文件的时间进行修改,存取时间(access time).修改时间(modification time).对不存在的文件,进行创建新的空白文件. 语法:touch [选项] 文件 ...
- 分析器错误 未能加载类型“XX.WebApiApplication”
解决方案,删除bin目录下内容(有单独使用dll的删除前请先备份) 清理解决方案并重新生成
- Instant Run 的操作影响到了代码,导致Android App启动闪退的问题
转自yuhc163原文android启动应用java.lang.NoClassDefFoundError: Class not found using the boot class loader; n ...
- 常见社工破解WPA2密码方法及防范措施
0×00前言 何为社工?社工是一种通过利用受害者心理弱点,如本能反应.好奇心.同情心.信任.贪婪等进行诸如欺骗.盗取.控制等非法手段的一种攻击方式.在无线安全中也可以利用社工做到许多非法操作.下面举几 ...
- linux下获取占用CPU资源最多的10个进程
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...
- JavaScript里面向对象的继承:构造函数"继承"的六种方法
//现在有一个"动物"对象的构造函数. function Animal(){ this.species = "动物"; } //还有一个"猫" ...