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】的更多相关文章

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

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

  3. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

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

  5. 344. Reverse String【easy】

    344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...

  6. 345. Reverse Vowels of a String - LeetCode

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

  7. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  8. 【一天一道LeetCode】#345. Reverse Vowels of a String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

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

随机推荐

  1. luogu P1126 机器人搬重物

    题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机 ...

  2. [CF819B]Mister B and PR Shifts

    题意:定义一个排列$p_{1\cdots n}$的“偏移量”$D=\sum _{i=1}^n\left|p_i-i\right|$ 求它所有的轮换排列中偏移量最小的是多少,要求输出轮换序数 暴力就是求 ...

  3. 【并查集】bzoj1015 [JSOI2008]星球大战starwar

    倒着处理删点,就变成了加点,于是并查集. #include<cstdio> using namespace std; #define N 400001 int fa[N],kill[N], ...

  4. 前端基础-HTML简介及发展史

    一 HTML简介 二 HTML发展史 一. HTML简介 用户使用浏览器打开网页看到结果的过程就是:浏览器将服务端的文本文件(即网页文件)内容下载到本地,然后打开显示的过程. 而文本文件的文档结构只有 ...

  5. 摄氏度和华氏度之间的额转换 Exercise06_08

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:摄氏度和华氏度之间的额转换 * */ public class Exercise06_08 { public static v ...

  6. 键盘事件OnKeyListener

    1.效果图:就是在文本框中输入A,则显示A,输入B,则显示B.... (1)activity_main.xml <?xml version="1.0" encoding=&q ...

  7. access 数据更新语句

    UPDATE YS_POINT AS a, YS_LINE AS b SET a.管线高程 = b.SELEV1WHERE (((a.物探点号)=[b].[起点号]));

  8. oracle 11g 大量废连接占满数据库连接问题处理

    问题描述: 数据库不断出现大量无用连接,超过数据库最大连接数,导致新的连接无法建立,访问不通数据库 问题分析: 服务器netstat连接数,大量连接来自办公网连接,不断在增加,通过服务器spid查看数 ...

  9. Linux 动态库 静态库

    什么是库 本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行.由于windows和Linux的本质不同,因此二者库的二进制是不兼容的.Linux操作系统支持的库函数分为静态库和动态库 ...

  10. <img>元素底部为何有空白及其解决方案

    一.为什么<img>元素底部会有空白? 要理解这个问题,首先要弄明白CSS对于 display: inline 元素的 vertical-align 各个值的含义.vertical-ali ...