您的位置:首页 > 其它

[Leetcode]345. Reverse Vowels of a String

2016-07-08 22:10 471 查看
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".

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