345. Reverse Vowels of a String

来源:互联网 发布:淘宝打折第二件原价 编辑:程序博客网 时间:2024/05/29 19:02

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:    bool judge(char t){        char v[11]={'a','e','i','o','u','A','E','I','O','U'};        for(int i=0;i<10;i++){            if(t==v[i])                return true;        }        return false;    }    string reverseVowels(string s) {        int len=s.size();        int i=0,j=len-1;        while(i<j){            while(i<j&&judge(s[i])==false)i++;            while(i<j&&judge(s[j])==false)j--;            swap(s[i++],s[j--]);//这里记得要i++,j--;        }        return s;    }};
0 0
原创粉丝点击