您的位置:首页 > 编程语言 > C语言/C++

Reverse Vowels of a String

2016-07-14 13:41 288 查看
一、问题描述

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".
二、思路

类似题目:Reverse String

三、代码

class Solution {
public:
bool isVowels(char t){
if(t == 'a'|| t == 'o' || t =='e' || t == 'i' || t == 'u'||t == 'A'|| t == 'O' || t =='E' || t == 'I' || t == 'U')
return true;
return false;
}
string reverseVowels(string s) {
if(s == "")
return "";
for(int i = 0,j = s.size() - 1; i < j;){
while(isVowels(s[i]) == false && i < j) ++i;
while(isVowels(s[j]) == false && i < j) --j;
if(isVowels(s[i]) && isVowels(s[j])){
char c = s[i];
s[i] = s[j];
s[j] = c;
++i;
--j;
}
}
return s;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode C++