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

个人博客:http://www.cnblogs.com/wdfwolf3/

这道题在逆置字符串的基础上加入了限制,只做原音(aeiouAEIOU)的逆置,不理会非原因的字符。在这里我主要针对如何判断原因讲述几个方法,至于字符串逆置不再讨论,可以看我专门写Reverse String的博文http://www.cnblogs.com/wdfwolf3/p/5484675.html

1.调用函数判断(16ms)

这个最基本最容易想到实现,没什么难度。

class Solution {
public:
string reverseVowels(string s) {
int i=,j=s.length()-;
while(i<j)
{
while((isVowel(s[i])==false)&&(i<j))
{
i++;
}
while((isVowel(s[j])==false)&&(i<j))
{
j--;
}
swap(s[i],s[j]);
i++;
j--;
}
return s;
}
bool isVowel(char c)
{
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U'))
return true;
return false;
}
};

2.利用数组模拟哈希表的方式(12ms)

class Solution {
public:
string reverseVowels(string s) {
int dict[] = {};
dict['a']=;
dict['A']=;
dict['e']=;
dict['E']=;
dict['i']=;
dict['I']=
dict['o']=;
dict['O']=;
dict['u']=;
dict['U']=;
int i=,j=s.length()-;
while(i<j)
{
while((dict[s[i]]==)&&(i<j))
{
i++;
}
while((dict[s[j]]==)&&(i<j))
{
j--;
}
swap(s[i],s[j]);
i++;
j--;
}
return s;
}
};

3.利用字符串查找函数string.find()或者string.find_first_of()。关于这两个函数在最后面详细介绍。

class Solution {
public:
string reverseVowels(string s) {
string vowel="aeiouAEIOU";
int i=,j=s.length()-;
while(i<j)
{
while((vowel.find(s[i])==string::npos)&&(i<j))
{
i++;
}
while((vowel.find(s[j])==string::npos)&&(i<j))
{
j--;
}
swap(s[i],s[j]);
i++;
j--;
}
return s;
}
};

(12ms)

string vowel="aeiouAEIOU";
int i=,j=s.length()-;
while(i<j)
{
i=s.find_first_of(vowel,i);
j=s.find_last_of(vowel,j);
if(i>=j)
break;
swap(s[i],s[j]);
i++;
j--;
}
return s;

(13ms)

P.S.

1.str1.find(str2, , )

作用是在str1中查找str2的位置,str2可以是单个字符,字符串变量或者字符串;第二个参数是str1的起始查找位置,即从str1的哪个位置开始查找,默认为0;第三个参数是只查找str2的前多少个字符,默认是str2的全部。可以没有后两个参数。返回的是str2首个字符在str1的位置,如果没有找到的话返回string::npos,它有几层含义,本身它是一个常量-1,当作为返回值时表示查找失败;在使用下标时,它大于任何下标(逻辑上概念),可以看作字符串的尽头或者说结尾。

2.string.find_first_of(str, , )

参数作用同上。但是函数作用是不同的,返回的值是str中任何一个字符首次在string中出现的位置。上一个函数相当于匹配,这个更像筛选。

3.string.find_last_of(str, , )

参数作用同上,作用也同上。区别是这个是向前查找的,从第二个参数位置开始向前找到str中任何一个字符首次在string中出现的位置,自然默认参数为string::npos。

leetcode345——Reverse Vowels of a String(C++)的更多相关文章

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

  2. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

  3. 【leetcode80】Reverse Vowels of a String(元音字母倒叙)

    题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: Write a function that takes a ...

  4. LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)

    这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...

  5. LeetCode | Reverse Words in a String(C#)

    题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...

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

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

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

  8. LeetCode_345. Reverse Vowels of a String

    345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only ...

  9. 345. Reverse Vowels of a String - LeetCode

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

随机推荐

  1. C语言之Static

    1.全局静态变量 在全局变量之前加上关键字static,全局变量就被定义成为一个全局静态变量. 1)内存中的位置:静态存储区(静态存储区在整个程序运行期间都存在) 2)初始化:未经初始化的全局静态变量 ...

  2. HDOJ-ACM1022(JAVA)

    这道题:是模拟出栈,判断出栈顺序的可能性. 基本上大家的做法都是直接模拟栈的出栈入栈并将顺序用0,1序列来表示,我暂时没想到什么好的思路. import java.util.*; import jav ...

  3. Jetty 8长连接上的又一个坑

    Jetty 8 长连接的超时断开连接的机制:超时连接机制针对IO传输过程中的数据阻塞时间超过一定阈值时,断开该连接.阻塞指当前处于数据传输阶段,但是连续指定时间内都没有发出或者接收到任何数据时,Jet ...

  4. 问题-[Access]“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法

    问题现象:ado.net oledb方式访问Access数据库文件时报错“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法  问题处理:1.数据库名称不能命名为:Syste ...

  5. poj 3984 迷宫问题【bfs+路径记录】

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10103   Accepted: 6005 Description ...

  6. js常用内置对象、Dom对象、BOM对象

    11.html元素事件属性中,如onclick="",双引号里可以是方法条用,可以是js代码(无需加<script>标签) 12.JavaScript内置 对象.属性和 ...

  7. spring的有状态BEAN和无状态BEAN

    有状态会话bean   :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”:一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一 ...

  8. 【转】ST05

    一. SQL Trace 通过SQL跟踪,可以具体查询数据来源于哪些数据库表, 例如:可以查询某个交易(或几个交易)所涉及的数据库表. 为了减少在最终查询结果的工作量,要在屏幕显示你所要显示的数据的前 ...

  9. [React Native] State and Touch Events -- TextInput, TouchableHighLight

    In React, components manage their own state. In this lesson, we'll walk through building a component ...

  10. 深入理解jQuery插件开发(转)

    如果你看到这篇文章,我确信你毫无疑问会认为jQuery是一个使用简便的库.jQuery可能使用起来很简单,但是它仍然有一些奇怪的地方,对它基本功能和概念不熟悉的人可能会难以掌握.但是不用担心,我下面已 ...