Reverse Vowels of a String

来源:互联网 发布:阿里云服务器无法连接 编辑:程序博客网 时间:2024/06/09 02:10

一、问题描述

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;    }};



0 0