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. 【单调队列】bzoj2096 [Poi2010]Pilots

    用两个单调队列维护序列中的最大值和最小值即可. poi~ #include<cstdio> #include<algorithm> using namespace std; i ...

  2. Interaction triggers in WPF

    Interaction Class - static class that owns the Triggers and Behaviors attached properties. Handles p ...

  3. 自定义PHP页面跳转函数redirect($url, $time = 0, $msg = '')

    利用PHP的header()函数,可以实现页面跳转,如 header("Location: " . $url); 但它有个缺点,一旦HTTP报头块已经发送,就不能使用 header ...

  4. 【PHP手册】 PHP debug_backtrace() 函数

    定义和用法 PHP debug_backtrace() 函数生成一个 backtrace(回溯信息). 该函数返回一个关联数组.下面是可能返回的元素: 名称 类型 描述 function 字符串 当前 ...

  5. winform groupbox控件放到窗体中间位置

    1. 在Form中放一个控件,让其在启动时始终居中 int gLeft = this.Width / 2 - groupControl1.Width / 2; int gTop = this.Heig ...

  6. Vue 单页应用:记事本

    若文章中存在内容无法加载的情况,请移步作者其他博客. 简书 CSDN 最近在看 Vue 的时候,别人给我安利了一个国外的小案例,通过 Vue 和 Vuex 来实现一个记事本. 仔细剖析下,发现“麻雀虽 ...

  7. Debian、Ubuntu 源列表说明

    转载:http://forum.ubuntu.org.cn/viewtopic.php?t=366506 概貌: 源列表主文件为 /etc/apt/sources.list,另兼取 /etc/apt/ ...

  8. 利用LogParser分析IIS日志

    LogParser是微软官方出品的用于读取分析IIS日志的工具,使用类SQL语句过滤文本日志内容,并可将内容导出到csv.sqlserver作进一步分析    下载地址:http://www.micr ...

  9. C++游戏界面不流畅的问题

    或许是我游戏玩多了,我突然发现,我的C++程序画面画面一顿一顿的,不流畅.肯定哪里不正确,要改. 奇怪啊,为什么我曾经,在我电脑上就不这么卡,就看不出画面一顿一顿的呢? 百度了,狗狗了,必应了,然而, ...

  10. C++发送HTTP请求---亲测可行(转)

    转自:http://hi.baidu.com/benbearlove/item/1671c23017575825b3c0c53f 环境:xp sp3,vs2008,在静态库中使用 MFC #inclu ...