345. Reverse Vowels of a String

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

Note:
The vowels does not include the letter "y".

 

翻转字符串中的所有元音

 

find_first_of

如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同

 

C++(9ms):

 1 class Solution {
 2 public:
 3     string reverseVowels(string s) {
 4         int i = 0 ;
 5         int j = s.size() - 1 ;
 6         while(i < j){
 7             i = s.find_first_of("aeiouAEIOU",i) ;
 8             j = s.find_last_of("aeiouAEIOU",j) ;
 9             if (i < j){
10                 swap(s[i++],s[j--]) ;
11             }
12         }
13         return s ;
14     }
15 };

 

posted @ 2017-10-27 10:46  __Meng  阅读(118)  评论(0编辑  收藏  举报