345. Reverse Vowels of a String
2016-04-24 14:58
465 查看
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:[/b]
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Subscribe to see which companies asked this question
代码:
class Solution {
public:
bool check(char a)
{
if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='A'||a=='E'||a=='I'||a=='O'||a=='U')
return false;
return true;
}
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
while(i<j)
{
while(i<j && check(s[i]))++i;
while(i<j && check(s[j]))--j;
if(i<j)
{
swap(s[i],s[j]);
++i;--j;
}
}
return s;
}
};
Example 1:[/b]
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
Subscribe to see which companies asked this question
代码:
class Solution {
public:
bool check(char a)
{
if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'||a=='A'||a=='E'||a=='I'||a=='O'||a=='U')
return false;
return true;
}
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
while(i<j)
{
while(i<j && check(s[i]))++i;
while(i<j && check(s[j]))--j;
if(i<j)
{
swap(s[i],s[j]);
++i;--j;
}
}
return s;
}
};
相关文章推荐
- cmp
- leetcode-235. Lowest Common Ancestor of a Binary Search Tree
- 关于丑数
- 如何将十进制转换为二进制、八进制、十六进制
- bzoj2653: middle
- Java中的static的使用
- puppet基础配置
- Android App中使用RatingBar实现星级打分功能的教程
- fir2(n,f,m)
- [翻译]Java Garbage Collection Basics Java 垃圾回收基础 之7 总结
- IOS设计模式之责任链模式
- Java千百问_05面向对象(013)_泛型如何使用
- 范围素数的求解算法
- Hadoop Sentry 学习
- 基于s5pv210的uboot总结
- 离散系统频响特性函数freqz()
- [翻译]Java Garbage Collection Basics Java 垃圾回收基础 之6 Java垃圾回收器
- 最长回文子串——Manacher算法
- linux的du命令----查看使用空间
- angular directive指令的复用