leetcode 345 Reverse Vowels of a String

来源:互联网 发布:js中的保留字有哪些 编辑:程序博客网 时间:2024/04/29 04:24

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

Subscribe to see which companies asked this question


class Solution {public:    string reverseVowels(string s) {        if(s=="") return s;vector<int> vowels(260, 0);vowels['a']=1;vowels['e']=1;vowels['i']=1;vowels['o']=1;vowels['u']=1;vowels['A']=1;vowels['E']=1;vowels['I']=1;vowels['O']=1;vowels['U']=1;for(int i = 0, j=s.size()-1; i <= j;) {if(vowels[s[i]]==0) {i++;continue;}if(vowels[s[j]]==0) {j--;continue;}if(vowels[s[i]]==1 && vowels[s[j]]==1) {char ch = s[i];s[i] = s[j];s[j] = ch;i++;j--;}}return s;    }};




0 0
原创粉丝点击